Advertisement
Guest User

SceneSource Greasemonkey Category Filters

a guest
Mar 25th, 2010
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // ==UserScript==
  2. // @name           SceneSource Category Filters
  3. // @namespace      http://127.0.0.1/
  4. // @description    Allows you to filter content by individual categories (only works with Shadowness).
  5. // @include        http://www.scnsrc.net/*
  6. // ==/UserScript==
  7.  
  8. var $ = unsafeWindow['jQuery'];
  9. var categories = [];
  10. $('ul.categories > li.cat-item > a:first-child').each(function() {
  11.     categories.push($(this).text());
  12. });
  13.  
  14. var filters = new Array(categories.length);
  15. if (document.cookie.length > 0) {
  16.     var start = document.cookie.indexOf('filters=') + 8;
  17.     if (start > 7) {
  18.         var end = document.cookie.indexOf(';', start);
  19.         if (end == -1)
  20.             end = document.cookie.length;
  21.         var str = unescape(document.cookie.substring(start, end));
  22.         if (str.length == filters.length) {
  23.             for (var i = 0; i < filters.length; i++) {
  24.                 if ((filters[i] = (str.charAt(i) == '1' ? '1' : '0')) == 1) {
  25.                     toggle(categories[i], 1);
  26.                 }
  27.             }
  28.         }
  29.     }
  30. }
  31. if (filters[filters.length -1] === undefined) {
  32.     for (var i = 0; i < filters.length; i++) {
  33.         filters[i] = '0';
  34.     }
  35. }
  36.  
  37. $('ul.filters').each(function() {
  38.     $(this).empty();
  39.     for (var i = 0; i < categories.length; i++)  {
  40.         var str = filters[i] == '0' ? 'Disable' : 'Enable';
  41.         $(this).append($('<li />').css({
  42.                 'background': 'url("/wp-content/themes/shadowness/images/pip.gif") no-repeat scroll 0 0 transparent',
  43.                 'padding-left': '8px'}).append($('<a />').text(str + ' ' + categories[i]).attr('id', 'filter_' + i).click(function(event) {
  44.             event.preventDefault();
  45.             var id = $(this).attr('id').substring(7);
  46.             var category = $(this).text().substring($(this).text().indexOf(' ') + 1);
  47.             filters[id] = filters[id] == '0' ? '1' : '0';
  48.             $(this).text((filters[id] == '0' ? 'Disable' : 'Enable') + ' ' + category);
  49.             toggle(category, filters[id]);
  50.             document.cookie = 'filters=' + filters.join('') + ';expires=0';
  51.         })));
  52.     }
  53. });
  54.  
  55. function toggle(category, hide) {
  56.     $('div.post').each(function() {
  57.         var post = $(this);
  58.         $('div.cat.meta span.left a[rel=category tag]', post).each(function() {
  59.             if (category == $(this).text()) {
  60.                 post.css('display', hide == '1' ? 'none' : '');
  61.             }
  62.         });
  63.     });
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement