Skip to content

Instantly share code, notes, and snippets.

@mikeperri
Last active January 2, 2016 18:39
Show Gist options
  • Save mikeperri/8344779 to your computer and use it in GitHub Desktop.
Save mikeperri/8344779 to your computer and use it in GitHub Desktop.
Reddit Enhancement Suite module to show a Loch Ness Monster icon on long posts that end with the tree fiddy thing. Based on https://gist.github.com/Raugturi/1077321
/*
To install, find your reddit_enhancement_suite.user.js file and paste it in after the other modules.
Firefox seems to detect when you tamper with extensions, so for now this is only working in Chrome.
1. WIN: Press Windows+R and paste in "%HOMEDRIVE%%HOMEPATH%\AppData\Local\Google\Chrome\User Data\Default\Extensions" (w/o quotes).
MAC: Open a Finder window, press Command+Shift+G, and paste in "~/Library/Application Support/Google/Chrome/Default/Extensions" (w/o quotes).
2. Paste "reddit_enhancement_suite.user.js" (w/o quotes) into the search bar, and search in the Extensions folder.
3. Open the file in a text editor (if you don't have one, download Sublime Text from sublimetext.com and use that).
Paste the code right after all the other module definitions, which should be around line 21323.
4. Restart your browser.
*/
//------COPY AND PASTE EVERYTHING BELOW THIS LINE INTO YOUR reddit_enhancement_suite.user.js FILE------
modules['treeFiddyWarning'] = {
moduleID: 'treeFiddyWarning',
moduleName: 'Tree Fiddy Warning',
category: 'Filters',
options: {
phraseList: {
type: 'text',
value: '500 feet|500 foot|foot tall|feet tall|story tall|stories tall|crustacean|mesozoic|paleozoic|paleolithic|protozoic|three dollars and fifty cents|three fifty|tree fiddy|treefiddy|3\.50|loch ness monster',
description: 'List of tree fiddy related phrases (separated by |)'
},
threshold: {
type: 'enum',
values: [{
name: '1',
value: 1
}, {
name: '2',
value: 2
}],
value: 2,
description: 'How many tree fiddy related phrases must be present to show the warning?'
},
},
description: 'Displays a warning on posts that may end with Tree Fiddy.',
isEnabled: function() {
return RESConsole.getModulePrefs(this.moduleID);
},
include: Array(
/https?:\/\/([a-z]+).reddit.com\/user\/[-\w\.]+/i,
/https?:\/\/([a-z]+).reddit.com\/[-\w\.\/]+\/comments\/[-\w\.]+/i,
/https?:\/\/([a-z]+).reddit.com\/message\/[-\w\.\_\?=]*/i
),
exclude: Array(
/https?:\/\/([a-z]+).reddit.com\/ads\/[-\w\.\_\?=]*/i,
/https?:\/\/([a-z]+).reddit.com\/[-\w\.\/]*\/submit\/?$/i
),
isMatchURL: function() {
return RESUtils.isMatchURL(this.moduleID);
},
go: function() {
if ((this.isEnabled()) && (this.isMatchURL())) {
// DOMNodeInserted listener and findAllFiddies code are based on / shamelessly copied from Show Images module.
document.body.addEventListener('DOMNodeInserted', function(event) {
if (
((event.target.tagName == 'DIV') && (event.target.getAttribute('id') == 'siteTable')) ||
((event.target.tagName == 'FORM') && (event.target.getAttribute('class') == 'usertext'))
)
{
var isSelfText = false;
if (event.target.tagName == 'FORM') {
isSelfText = true;
}
modules['treeFiddyWarning'].findAllFiddies(event.target, isSelfText);
if (!isSelfText) {
modules['treeFiddyWarning'].waitForScan = setInterval(function() {
if (!(modules['treeFiddyWarning'].scanningForLinks)) {
modules['treeFiddyWarning'].treeFiddyWarning(modules['treeFiddyWarning'].gw, true);
clearInterval(modules['treeFiddyWarning'].waitForScan);
}
}, 100);
}
}
}, true);
this.findAllFiddies();
}
},
checkElementForFiddies: function(index) {
ele = this.allElements[index];
var text = $(ele).text();
console.log(text);
if(text.length > 1000){
console.log('long enough');
var patt = new RegExp(this.options.phraseList.value,'ig');
patt.lastIndex = Math.floor(text.length/2);
var score = 0;
while(patt.exec(text)){
score++;
}
console.log(score)
var threshold = this.options.threshold.value;
if(score >= threshold){
console.log('fiddy');
ele.style.backgroundImage = 'url("http://i.imgur.com/UHrvtTr.png")';
ele.style.backgroundRepeat = 'no-repeat';
ele.style.backgroundPosition = 'right top';
}
}
},
findAllFiddies: function(ele, isSelfText) {
this.scanningForLinks = true;
if (ele == null) {
ele = document.body;
}
// get elements common across all pages first...
// if we're on a comments page, get those elements too...
var commentsre = new RegExp(/comments\/[-\w\.\/]/i);
var userre = new RegExp(/user\/[-\w\.\/]/i);
this.scanningSelfText = false;
if ((commentsre.test(location.href)) || (userre.test(location.href))) {
this.allElements = ele.querySelectorAll('.expando .usertext-body, .content .usertext-body');
} else if (isSelfText) {
// We're scanning newly opened (from an expando) selftext...
this.allElements = ele.querySelectorAll('.usertext-body');
this.scanningSelfText = true;
}
if (this.allElements != null) {
// Make sure it doesn't die if there are no elements to work with, which happens in the message window if there are no messages.
this.allElementsCount=this.allElements.length;
this.allElementsi = 0;
if (RESUtils.pageType() == 'comments') {
(function(){
// we're on a comments page which is more intense, so just scan 15 comments at a time...
var chunkLength = Math.min((modules['treeFiddyWarning'].allElementsCount - modules['treeFiddyWarning'].allElementsi), 15);
for (var i=0;i<chunkLength;i++) {
modules['treeFiddyWarning'].checkElementForFiddies(modules['treeFiddyWarning'].allElementsi);
modules['treeFiddyWarning'].allElementsi++;
}
if (modules['treeFiddyWarning'].allElementsi < modules['treeFiddyWarning'].allElementsCount) {
setTimeout(arguments.callee, 1000);
} else {
modules['treeFiddyWarning'].scanningSelfText = false;
modules['treeFiddyWarning'].scanningForLinks = false;
}
})();
} else {
var chunkLength = modules['treeFiddyWarning'].allElementsCount;
for (var i=0;i<chunkLength;i++) {
modules['treeFiddyWarning'].checkElementForFiddies(modules['treeFiddyWarning'].allElementsi);
modules['treeFiddyWarning'].allElementsi++;
}
modules['treeFiddyWarning'].scanningSelfText = false;
modules['treeFiddyWarning'].scanningForLinks = false;
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment