Last active
August 29, 2015 13:55
-
-
Save patch-werk/8722819 to your computer and use it in GitHub Desktop.
RES Module which filters out annoying comments. See starting comments for info.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| This module does basic filtering of comments on reddit. | |
| You have you install it yourself buy copy and pasting this code into the reddit_enchancement_suite.user.js file | |
| For Mac and chrome it is located in one of the folders (the one that is RES's folder which has a folder in it | |
| which is the current RES version 4.1.something) located in ~/User/Library/Application Support/Google/Chrome/Default | |
| Paste the code at the end of the modules around line 21321 after the previous modules "};" | |
| I do not know where the folder is on Windows but if you find it, it should still work on Chrome (I think). | |
| THIS MODULES SETTINGS ARE IN THE FILTERS SECTION OF RES SETTINGS NOT COMMENTS | |
| Any regex can go between the | bars in the avoidList. Be careful not to have an extra | in there or it will kill all comments! | |
| You can open chrome's console to see all the comments it is hiding if you want! The default avoidList is just stuff that I am tired | |
| of seeing, please do not take offense to it! Please be aware that changing the avoidList from within the code required escape | |
| characters for any of the escape characters needed for the regex! For example in order to get rid of "[deleted]" comments | |
| the regex is \[deleted\]. If adding that from this js file that \ needs its own escape \ to appear properly! Or you can just | |
| type it in with one set of escapes from the settings menu. | |
| See the avoidList below to see where I needed to use and escape character. New to regexs? See this part of the wikipedia page for examples! | |
| http://en.wikipedia.org/wiki/Regular_expression#Syntax | |
| Please note I use RES with pretty much ALL the modules off, so you detect an imcompatibility with one of the modules please | |
| let me know! | |
| Current Version: | |
| 1.1 -Allows searching of children. Only works on chrome (only tested on mac; should work on Win). Should work on firefox but you have to | |
| compile the entire RES yourself because you cannot access the reddit_enchancement_suite.user.js file since the firefox addon | |
| is compiled into an XPI file?? I am not sure of this. The firefox compatibility is there in the getDirectChildren() function | |
| you just need to figure out how to get this code into the addon! (It is so easy on chrome!). Not tested on Firefox. | |
| Previous Versions: | |
| 1.0 -Did not search childen. Only worked on chrome | |
| Still need to: | |
| -Test compatibility of webkitMatchesSelector in other browsers | |
| -Implement search of any comments that are made visible by clicking "Load more comments" | |
| Having a better/less-circlejerky reddit experience because of this module? Donate hash math problems here: | |
| BTC: 1G785e71SWssf2EeiuLoHoMDFn2a1q8GAC | |
| LTC: LiUGVcr6Bg6ge2TTTLnThxG3JjtxsZnfoU | |
| */ | |
| modules['commentFiltereddit'] = { | |
| moduleID: 'commentFiltereddit', | |
| moduleName: 'Comment Filter', | |
| category: 'Filters', | |
| options: { | |
| avoidList: | |
| { | |
| type: 'text', | |
| value: 'ITT: |DAE |[Uu]nidan|[Rr]elevant [Uu]sername|\\[deleted\\]|It\'?s [Hh]appening[!?]', | |
| description: 'All the circlejerky non-contributory phrases you never want to see. Seperate them with |. ' + | |
| 'Make sure the list does not end or begin with a | or else it will kill all comments.' | |
| }, | |
| searchChildren: | |
| { | |
| type: 'boolean', | |
| value: true, | |
| description: 'Process children comments as well?' | |
| } | |
| }, | |
| description: 'Hide comments that contain annoying stuff that you are tired of seeing', | |
| isEnabled: function() { | |
| return RESConsole.getModulePrefs(this.moduleID); | |
| }, | |
| include: [ | |
| /^https?:\/\/([a-z]+)\.reddit\.com\/user\/[-\w\.]+/i, | |
| /^https?:\/\/([a-z]+)\.reddit\.com\/message\/comments\/[-\w\.]+/i, | |
| /https?:\/\/([a-z]+).reddit.com\/[-\w\.\/]+\/comments\/[-\w\.]+/i | |
| ], | |
| isMatchURL: function() { | |
| return RESUtils.isMatchURL(this.moduleID); | |
| }, | |
| go: function() { | |
| if ((this.isEnabled()) && (this.isMatchURL())) { | |
| // lets find and kill comments on this page | |
| this.findAndDestroyComments(); | |
| } | |
| }, | |
| findAndDestroyComments: function() | |
| { | |
| //form reg exp | |
| filterExp = new RegExp(this.options.avoidList.value); | |
| //get all the root comments | |
| this.commentContainers = document.querySelectorAll('div.commentarea > div.sitetable > div.thing'); | |
| //make sure we are on a comments page and there are come comments | |
| if (RESUtils.pageType() == 'comments' && this.commentContainers != null) | |
| { | |
| //for every root comment check it for crap | |
| var commentCount = this.commentContainers.length; | |
| for (var i=0; i<commentCount; i++) | |
| { | |
| this.checkCommentForCrap(this.commentContainers[i], filterExp); | |
| } | |
| } | |
| }, | |
| checkCommentForCrap: function(comm, filterExp) | |
| { | |
| //get the comments body and convert it to text. | |
| this.comment = comm.querySelector('.content .usertext-body'); | |
| var text = $(this.comment).text(); | |
| //compare | |
| if(filterExp.exec(text)) | |
| { | |
| console.log('Found crap: ' + text); | |
| //kill the annoying shit | |
| comm.style.display = 'none'; | |
| } | |
| //if we want to search through children | |
| else if(this.options.searchChildren.value) | |
| { | |
| //take childrencontainer | |
| var childrenContainer = comm.querySelector('div.child > div.sitetable'); | |
| //if it has children | |
| if(childrenContainer!=null) | |
| { | |
| //get only its direct child comments | |
| var children = this.getDirectChildren(childrenContainer,'div.thing'); | |
| //loop through em and use recursion | |
| for (var i=0;i<children.length;i++){ | |
| this.checkCommentForCrap(children[i], filterExp); | |
| } | |
| } | |
| } | |
| }, | |
| //gets only direct children since querySelectorAll cant contain itself. only works on chrome and firefox so far | |
| getDirectChildren: function(elm, sel) | |
| { | |
| var ret = []; | |
| for (var x=0; x < elm.childNodes.length; ++x){ | |
| //only tested in chrome!!! | |
| if(BrowserDetect.isChrome() || BrowserDetect.isOpera() || BrowserDetect.isSafari()){ | |
| if (elm.childNodes[x].webkitMatchesSelector(sel)){ | |
| ret.push(elm.childNodes[x]); | |
| } | |
| } | |
| else if (BrowserDetect.isFirefox()){ | |
| if (elm.childNodes[x].mozMatchesSelector(sel)){ | |
| ret.push(elm.childNodes[x]); | |
| } | |
| } | |
| } | |
| return ret; | |
| } | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment