Apmyp / SC2TV Features

// ==UserScript==
// @name        SC2TV Features
// @namespace   sc2tv
// @description Почему не добавить чуток фич на ск2тв
// @include     http://sc2tv.ru/*
// @version     8
// @grant       none
// @grant       GM_info
// @require     http://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js
// ==/UserScript==

var tyanList = [
  'Parovozzik', 
  'Nika_Shin', 
  'Holly_Forve',
  'vetakrot',
  'dikaro4ka',
  'octopuseva',
  'kittyklawtv',
  'hell_girl',
];

/* GM hax?... */

var GM_getValue = function (key,def) {
    return JSON.parse(localStorage[key] || '""') || def;
};
var GM_setValue = function (key,value) {
    return localStorage[key]=JSON.stringify(value);
};

/* jquery workarounds ? */
if ( window != null && window.jQuery != null ) {
    var $ = window.jQuery; 
} else if( unsafeWindow != null && unsafeWindow.jQuery != null ) {
    var $ = unsafeWindow.jQuery;
}

$.expr[':'].containsIgnoreCase = function (n, i, m) {
  return jQuery(n).text().toUpperCase().indexOf(m[3].toUpperCase()) >= 0;
};

/**/

/* functions */

var setStreamClasses = function () {
  $('#streams_list_div > div').attr('class', 'stream_s');

  $('#streams_list_div > div:visible:eq(0)').attr('class', 'stream_l');
  $('#streams_list_div > div:visible:eq(1)').attr('class', 'stream_l');

  for( var j=2; j<=7; j++ ) {
    $('#streams_list_div > div:visible:eq('+j+')').attr('class', 'stream_m');
  }

  $('#streams_list_div > div').each(function() {
    var modList = $(this).attr('class').match(/stream_(\w)/);
    var mod = 's';
    if( modList != null && modList[1] ) {
      mod = modList[1];
    }
    $(this).find('a > img:first').attr('class', 'p_'+mod);
    $(this).find('a > div').attr('class', 'over_'+mod);
    $(this).find('a > div > div').attr('class', 'o_'+mod+'_bot');
  });

};

var addStreamButtons = function () {
  var ignoredStreams = GM_getValue('ignored_streams_2', []);
  $("#streams_list_div > div").each(function() {

    var isIgnoredStream = $.inArray($(this).find(".st_nm").text().replace(/,\s(.+)$/, ''), ignoredStreams) === -1;

    if( isIgnoredStream && $(this).find(".js-remove-stream").length <= 0 ) {
      $(this).find(".st_nm").append("<span class='js-remove-stream' style='color:red;font-size:8pt;cursor:pointer;' title='Убрать стрим'>&nbsp;&nbsp;&times;</span>");
      $(this).find(".js-add-stream").remove();
    } else if( !isIgnoredStream && $(this).find(".js-add-stream").length <= 0 ) {
      $(this).find(".st_nm").append("<span class='js-add-stream' style='color:green;font-size:8pt;cursor:pointer;' title='Вернуть стрим'>&nbsp;&nbsp;&#10003;</span>");
      $(this).find(".js-remove-stream").remove();
    }

  });
};

var addRemoveCategoryButtons = function () {
  $("#new_filter .g_list li").each(function() {
    if( $(this).find(".js-remove-category").length <= 0 ) {
      $(this).append("&nbsp;<span class='js-remove-category' style='color:red;font-size:12pt;cursor:pointer;' title='Убрать категорию'>&times;</span>");
    }
  });
  $("#hidden_categories_ul li").each(function() {
    if( $(this).find(".js-remove-category").length <= 0 ) {
      $(this).append("&nbsp;<span class='js-remove-category' style='color:red;font-size:12pt;cursor:pointer;' title='Убрать категорию'>&times;</span>");
    }
  });
};

var filterStreams = function () {
  var val = document.getElementById("filter-streams").value;
  if( val.length <= 0 ) {
    $('#streams_list_div > div').show();
  } else {
    $('#streams_list_div > div').hide();
    $('#streams_list_div > div:containsIgnoreCase("'+val+'")').show();
  }
  var ignoredCategories = GM_getValue('ignored_categories_2', []);
  $("#new_filter .g_list li").show();
  $("#hidden_categories_ul li").show();
  $.each(ignoredCategories, function(i, v) {
    $("#new_filter .g_list li a:containsIgnoreCase('"+v+"')").closest("li").hide();
    $("#hidden_categories_ul li a:containsIgnoreCase('"+v+"')").closest("li").hide();
    $("#streams_list_div > div").filter(function(){ return $.trim($(this).find(".st_cat").text()) == v; }).hide();
  });
  updateOtherCatsCount();

  var ignoredStreams = GM_getValue('ignored_streams_2', []);
  $.each(ignoredStreams, function(i, v) {
    $("#streams_list_div > div:containsIgnoreCase('"+v+"')").each(function() {
      $(this).remove().insertAfter($("#streams_list_div > div:last"));
    });
  });

  var tnn = GM_getValue('tnn', 0);
  if( tnn === 1 ) {
    $.each(tyanList, function(i, v) {
      $("#streams_list_div > div:containsIgnoreCase('"+v+"')").hide();
    });
  }

  setStreamClasses();
};

  var setIgnoredCategoriesList = function () {

    var html = '<div class="js-ignored-cats-wrap" style="width: 92%;margin: 0 auto;color: #fff;padding: 10px;"><span class="js-toggle-ignored-cats" style="cursor:pointer">« Игнорирумые категории</span><div class="js-ignored-cats" style="display:none;"></div></div>';

    $("#new_filter").css({ height: "auto", "min-height": "90px" });
    if( $(".js-ignored-cats-wrap").length <= 0 ) {
      $("#new_filter").append(html);
    }

    var ignoredCategories = GM_getValue('ignored_categories_2', []);

    $(".js-ignored-cats").html('Здесь ничего нет :(');
    if( ignoredCategories.length > 0 ) {
      $(".js-ignored-cats").html('');
      $.each(ignoredCategories, function(i, v) {
        $(".js-ignored-cats").append("<div><span class=\"js-cat-title\">"+v+"</span>&nbsp;<span class=\"js-remove-from-ignored\" style='color:green;font-size:12pt;cursor:pointer;' title='Вернуть категорию'>&#10003;</span></div>");
      });
    }

  };

  var balanceCategoriesLists = function() {
    var mainCatsNumber = 5-$(".g_list li:visible").length;
    if( mainCatsNumber > 0 ) {
      $("#hidden_categories_ul li:lt("+mainCatsNumber+")").remove().insertAfter($(".g_list li:last"));
    } else if ( mainCatsNumber < 0 ) {
      var otherNumber = 5-$(".g_list li:visible").length;
      if( otherNumber < 0 ) {
        $(".g_list li:visible").slice(otherNumber).remove().appendTo($("#hidden_categories_ul"));
      }
    }
    updateOtherCatsCount();
  };

  var updateOtherCatsCount = function () {
    $("#hidden_categories_count").text($("#hidden_categories_ul li[style!='display: none;']").length);
  };

  var init = function () {
    $("#filter-streams").keyup();
    balanceCategoriesLists();
    addRemoveCategoryButtons();
    addStreamButtons();
    setIgnoredCategoriesList();
  };


  /**/


  $(function(){


    if( document.getElementById("new_filter") != null ) {

      var filterStreamsValue = GM_getValue('filter_2-streams', "");
      var searchBlock = document.createElement('div');
      searchBlock.style.position = 'absolute';
      searchBlock.style.top = '5px';
      searchBlock.style.left = '40px';
      searchBlock.innerHTML = '<input type="text" value="'+filterStreamsValue+'" id="filter-streams" placeholder="Фильтр стримов.." style="padding:1px 6px;">';

      document.getElementById("new_filter").style.position = 'relative';
      document.getElementById("new_filter").appendChild(searchBlock);

      var tnn = GM_getValue('tnn', 0);
      tyanButtonsHtml = '<div style="position: absolute;right: 10px;top: 10px;color: #fafafa;"><label><input type="checkbox" id="tnn_checkbox"'+(tnn===1?' checked="checked"':'')+'> ТНН</label></div>';
      $("#new_filter").append(tyanButtonsHtml);

      $(document).on("keyup", "#filter-streams", function() {
        filterStreams();
        GM_setValue('filter_2-streams', document.getElementById("filter-streams").value);
      });

      $(document).on("click", ".js-remove-category", function() {
        var ignoredCategories = GM_getValue('ignored_categories_2', []);
        ignoredCategories.push($(this).parent("li").find('a').text().replace(/\s\(\d+\)$/, ''));
        GM_setValue('ignored_categories_2', ignoredCategories);

        init();
      });

      $(document).on("click", ".js-toggle-ignored-cats", function() {
        $(".js-ignored-cats").slideToggle();
      });

      $(document).on("click", ".js-remove-from-ignored", function() {
        var ignoredCategories = GM_getValue('ignored_categories_2', []);
        ignoredCategories = _.without(ignoredCategories, $(this).closest("div").find(".js-cat-title").text());
        GM_setValue('ignored_categories_2', ignoredCategories);

        init();

        $(this).parent("div").fadeOut();
      });

      $(document).on("click", ".js-remove-stream", function() {
        var ignoredStreams = GM_getValue('ignored_streams_2', []);
        ignoredStreams.push($(this).closest(".st_nm").text().replace(/,\s(.+)$/, ''));
        ignoredStreams = $.unique(ignoredStreams);
        GM_setValue('ignored_streams_2', ignoredStreams);

        init();
      });

      $(document).on("click", ".js-add-stream", function() {
        var ignoredStreams = GM_getValue('ignored_streams_2', []);
        ignoredStreams = _.without(ignoredStreams, $(this).closest(".st_nm").text().replace(/,\s(.+)$/, ''));
        ignoredStreams = $.unique(ignoredStreams);
        GM_setValue('ignored_streams_2', ignoredStreams);

        init();
      });

      $(document).on("click", "#tnn_checkbox", function() {
        GM_setValue('tnn', +$(this).is(':checked'));

        init();
      });

      init();

      setInterval(init, 3000);

    }


  });