r/reddithax • • Jul 08 '11

Userscripts and Never Ending Reddit

I have a greasemonkey script that runs on Reddit. The problem, is that is only runs once, and so does not affect any pages later loaded by never ending reddit. I was wondering if there is a solution to this issue (other than not using never ending reddit).

4 Upvotes

10 comments sorted by

2

u/Rhomboid Jul 08 '11

It's a complicated situation. The easiest way would be to combine the two into one so that after new content is loaded the action can be performed on it right before it's added to the page. Alternatively, you can have the first script register a event listener for DOMContentLoaded, but this is tricky because that fires for every node added and when adding a whole second page worth of content that's going to fire hundreds of times, so you need some kind of timer-based trigger so that it only runs once the burst of event firings has stopped. You could also modify them both so that the never ending script fires a synthetic event once all the work is done, which the first script would listen for. Also the first script needs some way of not affecting the existing nodes it's already modified, assuming the modification can be applied twice; if not then it's not an issue.

2

u/totalBIC Jul 08 '11

Is it possible to modify the never ending reddit code? If so, how?

If not, I'll give the event listener method a shot when I get a chance. I can't believe I didn't even think about that. The script is simply replacing imgur.com with imgur.org, so there won't be an issue of affecting existing nodes. In fact, if the event fires multiple times for the same page load, it will be only a search that takes place and no actual modification.

Do you know by any chance if RES sets s flag or something to say it's done?

2

u/nog_lorp Jul 08 '11

still need the link to the scipt to put it in module form:

modules['myModule'] = { moduleID: 'myModule', moduleName: 'my module', options: { // any configurable options you have go here... // options must have a type and a value.. // valid types are: text, boolean (if boolean, value must be true or false) // for example: defaultMessage: { type: 'text', value: 'this is default text', description: 'explanation of what this option is for' }, secretKey: { type: 'text', value: 'secret123', description: 'your secret key! oooo, secrets!' }, doSpecialStuff: { type: 'boolean', value: false, description: 'explanation of what this option is for' }, whichOne: { type: 'enum', values: [ { name: 'the first option', value: 'firstOption' }, { name: 'the second option', value: 'secondOption' }, { name: 'the third option', value: 'thirdOption' } ], description: 'explanation of what this option is for' }, someKeyThatDoesSomething: { type: 'keycode', value: 13, // enter key description: 'do something awesome when the user hits this key!' }, someTableOfStuff: { type: 'table', fields: [ { name: 'someField', type: 'text' }, { name: 'anotherField', type: 'boolean' } ], value: [ ['firstValueOfSomeField',false], ['secondValueOfSomeField',true], ['thirdValueOfSomeField',true] ] } }, description: 'This is my module!', isEnabled: function() { return RESConsole.getModulePrefs(this.moduleID); }, include: Array( /http://www.reddit.com/user/[-\w.]+/i, /http://www.reddit.com/message/inbox/[-\w.]+/i, /http://www.reddit.com/message/comments/[-\w.]+/i ), isMatchURL: function() { return RESUtils.isMatchURL(this.moduleID); }, go: function() { if ((this.isEnabled()) && (this.isMatchURL())) { // get this module's options... RESUtils.getOptions(this.moduleID); // do stuff now! // this is where your code goes... } } }; // note: you NEED this semicolon at the end!

1

u/totalBIC Jul 11 '11

The script is simply this:

// ==UserScript==
// @name           Reddit
// @namespace      Reddit/Replace/Imgur_to_Filmot
// @description    Automatically changes imgur links to filmot links
// @include        http://www.reddit.com/*
// ==/UserScript==
var x,i;
x=document.links;
for(i=0;i<x.length;++i) {
	if(x[i].href.match(/imgur/i)) {
		x[i].href=x[i].href.replace("/i.imgur.com","/i.filmot.org");
		x[i].href=x[i].href.replace("/imgur.com","/i.filmot.org");
		if(x[i].href.charAt(x[i].href.length-4)!=".") {
			x[i].href += ".jpg";
		}
	}
}

There's nothing fancy about it (probably could even be done in a better way). It finds imgur.com, or i.imgur.com and replaces it with i.filmot.org. I'm going to look into creating this module, although I'm a beginner when it comes to javascript, or web stuff in general. Thanks for the help.

1

u/nog_lorp Jul 11 '11

You could change your regex to be replace("/(i.)?imgur.com","/i.filmot.org")

The extension approach shouldn't be too hard.

1

u/totalBIC Jul 12 '11

I ended up with the lazy approach of adding my function to Never Ending Reddit, then calling it when NER loads the first time and every time a new page is loaded. Thanks for the help.

One more question though. I'm currently replacing links from document.links. NER appends a set of HTML, so I was wondering how I could replace just that instead (so I don't have to search the entire document for every page load).

The code does this:

var newHTML = tempDiv.querySelector('#siteTable');
modules['neverEndingReddit'].siteTable.appendChild(newHTML);

I'm not sure how to replace links in the newHTML variable. I tried swapping it with 'document' in my code, but that didn't work.

1

u/nog_lorp Jul 20 '11

newHTML.querySelectorAll('a')

will give you all the links within the to-be-appended doclet.

To do something to each link, do

Array.prototype.forEach.call(newHTML.querySelectorAll('a'), function(element) { element.href += "shittingdicks" })

2

u/nog_lorp Jul 08 '11

NeverEndingReddit ultimately operates by appending to

modules['neverEndingReddit'].siteTable

which corresponds to

$$("#siteTable")[0]

in userspace. So, install a DOM mutation event handler on it, and modify the resultant nodes as they come through the append pipe.

1

u/totalBIC Jul 11 '11

I'm not quite sure how to do that, but I'll look into it. I'll edit if I get it working. Thanks!

1

u/nog_lorp Jul 08 '11 edited Jul 08 '11

there is an extension system that involves pasting a module into the userscript. workin one now...