Advertisement
gavin19

RES - Comment filter

Oct 7th, 2011
399
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. modules['filterComments'] = {
  2.     moduleID: 'filterComments',
  3.     moduleName: 'Filter Comments',
  4.     category: 'Filters',
  5.     options: {
  6.         keywords: {
  7.             type: 'table',
  8.             fields: [{
  9.                 name: 'keyword',
  10.                 type: 'text'
  11.             }],
  12.             value: [],
  13.             description: 'Type in title keywords you want to ignore if they show up in a comment. The comment body will be hidden and replaced with a placeholder.'
  14.         },
  15.         hideComment: {
  16.             type: 'boolean',
  17.             value: false,
  18.             description: 'If a comment is filtered, hide the entire comment.'
  19.         },
  20.         filteredCommentText: {
  21.             type: 'text',
  22.             value: '[Filtered] - Click to reveal',
  23.             description: "When a comment is 'soft' filtered this is the default placeholder."
  24.         },
  25.         filteredCommentTextColour: {
  26.             type: 'text',
  27.             value: 'Red',
  28.             description: 'Change the colour of the placeholder text.'
  29.         }
  30.     },
  31.     description: 'Filter comments containing some text',
  32.     isEnabled: function() {
  33.         return RESConsole.getModulePrefs(this.moduleID);
  34.     },
  35.     include: Array(/https?:\/\/([a-z]+).reddit.com\/[-\w\.\/]+\/comments\/[-\w\.]+/i),
  36.     isMatchURL: function() {
  37.         return RESUtils.isMatchURL(this.moduleID);
  38.     },
  39.     applyCommentFilter: function(ele) {
  40.         var filteredPara;
  41.         for (var i = 0, lene = ele.length; i < lene; i += 1) {
  42.             if (ele[i].innerHTML) {
  43.                 if (this.filterComment(ele[i].innerHTML.toString())) {
  44.                     if (this.options.hideComment.value) {
  45.                         ele[i].parentNode.parentNode.parentNode.parentNode.style.display = "none";
  46.                     }
  47.                     else {
  48.                         filteredPara = document.createElement('p');
  49.                         if(modules['filterComments'].options.filteredCommentText.value){
  50.                         filteredPara.innerHTML = modules['filterComments'].options.filteredCommentText.value;
  51.                         } else {filteredPara.innerHTML="[Filtered] - Click to reveal";};
  52.                         filteredPara.setAttribute('style', 'cursor:pointer;font-weight:bolder !important;');
  53.                         filteredPara.style.color = modules['filterComments'].options.filteredCommentTextColour.value;
  54.                         filteredPara.setAttribute('onclick',';this.style.display="none";this.parentNode.querySelector(".md").style.display="block";');
  55.                         ele[i].parentNode.appendChild(filteredPara);
  56.                         ele[i].style.display="none";
  57.                         }
  58.                     }
  59.                 }
  60.             }  
  61.     },
  62.     go: function () {
  63.         if ((this.isEnabled()) && (this.isMatchURL())) {
  64.             if (this.options.keywords.value) {
  65.             document.body.addEventListener('DOMNodeInserted', function(ev) {
  66.             if ((event.target.tagName == 'DIV') && (/comment/ig.test(event.target.getAttribute('class')))) {
  67.                 var ele = ev.target.querySelectorAll('.md');
  68.                 modules['filterComments'].applyCommentFilter(ele);
  69.             }
  70.         }, false);
  71.         var ele = document.querySelectorAll('.commentarea .sitetable .md:not(.markhelp)');
  72.         this.applyCommentFilter(ele);
  73.         }
  74.         }
  75.        
  76.     },
  77.     filterComment: function(comment) {
  78.         return this.arrayContainsSubstring(this.options.keywords.value, comment.toLowerCase());
  79.     },
  80.     arrayContainsSubstring: function(obj, substring) {
  81.         var re;
  82.         for(var j=0, lenobj = obj.length;j<lenobj;j+=1){
  83.         re = "\\b" + obj[j].toString().toLowerCase() + "\\b";
  84.         if (substring.match(re)) {
  85.             return true;
  86.         }      
  87.         }
  88.         return false;  
  89.         }
  90.    
  91. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement