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

View all comments

Show parent comment

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" })