Advertisement
Guest User

MLPChan filter

a guest
Apr 18th, 2013
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function add_filter(type, filter) {
  2.     // Get the current filters
  3.     var filters = (localStorage.filters !== undefined ? JSON.parse(localStorage.filters) : []);
  4.    
  5.     // Add filter
  6.     filters.push({type : type, filter : filter});
  7.     localStorage.filters = JSON.stringify(filters);
  8. }
  9.  
  10. function remove_filter(type, filter) {
  11.     // Get the current filters
  12.     var filters = (localStorage.filters !== undefined ? JSON.parse(localStorage.filters) : []);
  13.    
  14.     // Loop through filters
  15.     for (var i = 0; i < filters.length; i++) {
  16.         if (filters[i].type == type && filters[i].filter == filter) {
  17.             // Remove any matches
  18.             filters.splice(i, 1);
  19.         }
  20.     }
  21.    
  22.     localStorage.filters = JSON.stringify(filters);
  23. }
  24.  
  25. /**    Simple JavaScript filter
  26.  *  Filters posts that match <rule>
  27.  *
  28.  *  rule format
  29.  *      {
  30.  *        name   : <poster name>
  31.  *        trip   : <poster trip>
  32.  *        email  : <poster email>
  33.  *        subject: <post subject>
  34.  *      }
  35.  *
  36.  * apply_filter(rule)
  37.  *      Hides all posts that match <rule>
  38.  *
  39.  * add_filter(rule)
  40.  *      Applys the filter <rule>, then stores the rule in localStorage
  41.  *
  42.  * apply_all_filters()
  43.  *      Applys all the filters in localStorage, should be used on page load
  44. **/
  45.  
  46. function apply_filter (rule) {
  47.     if (!rule) { return; }
  48.    
  49.     // Loop through all the posts
  50.     $(".postContainer").filter(function (index) {
  51.         // Fetch the post from the DOM
  52.         //      avoiding use of `this' because JSLint won't stop whining
  53.         var post = $(".postContainer")[index];
  54.        
  55.         // If a post matches a rule, hide the post
  56.         if ($(post).find(".name").text() == rule.name) {
  57.             $(post).hide();
  58.         }
  59.         if ($(post).find(".trip").text() == rule.trip) {
  60.             $(post).hide();
  61.         }
  62.         if ($(post).find(".email").attr("href") == "mailto:" + rule.email) {
  63.             $(post).hide();
  64.         }
  65.         if ($(post).find(".subject").text() == rule.subject) {
  66.             $(post).hide();
  67.         }
  68.        
  69.         return;
  70.     });
  71. }
  72.  
  73. // Add a filter to the localStorage
  74. function add_filter (rule) {
  75.     if (!rule) { return; }
  76.    
  77.     // Apply the filter first
  78.     apply_filter(rule);
  79.    
  80.     // Get all of the stored filters
  81.     var filters = [];
  82.     if (localStorage.filters) { filters = JSON.parse(localStorage.filters); }
  83.    
  84.     // Add the new filter and store it in localStorage
  85.     filters.push(rule);
  86.     localStorage.filters = JSON.stringify(filters);
  87.    
  88.     return;
  89. }
  90.  
  91. function remove_filter (rule) {
  92.     if (!rule) { return; }
  93.     if (!localStorage.filters) { return; }
  94.    
  95.     // Get all of the stored filters
  96.     var filters = JSON.parse(localStorage.filters);
  97.     var new_filters = [];
  98.    
  99.     // Loop through the rules
  100.     for  (var i = 0; i < filters.length; i++) {
  101.         // Remove any rules that match
  102.         if (JSON.stringify(filters[i]) == JSON.stringify(rule)) {
  103.             continue;
  104.         }
  105.        
  106.         new_filters.push(filters[i]);
  107.     }
  108.    
  109.     // Put the left over filters in the localStorage
  110.     localStorage.filters = JSON.stringify(new_filters);
  111.    
  112.     return;
  113. }
  114.  
  115. // Apply all filters in the localStorage
  116. function apply_all_filters () {
  117.     if (!localStorage.filters) { return; }
  118.    
  119.     // Get all of the stored filters
  120.     var filters = JSON.parse(localStorage.filters);
  121.    
  122.     // Loop through the rules and apply them
  123.     for (var i = 0; i < filters.length; i++) { apply_filter( filters[i] ); }
  124.    
  125.     return;
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement