Strings & Ints

A Greasemonkey script for reddit users.

by elbekko on Dec.24, 2009, under Uncategorized

I wrote this little script to convert imgur links into their fast version, because the imgur site often loads very slowly for me (don’t ask me why, I have no clue).

So, behold:

// ==UserScript==
// @name           Reddit Pics
// @namespace      http://www.reddit.com/r/
// @description    Makes imgur links into the proper link
// @include        http://www.reddit.com/r/
// ==/UserScript==

var entries = document.getElementsByTagName("a");

for(var i = 0; i != entries.length; i++)
{
	var link = entries[i];

	// Is link to imgur
	if(/*link.className.indexOf("title loggedin ") != -1 && */link.href.indexOf("imgur.com") != -1)
	{
		if(link.href.split(".").length < 3)
		{
			link.title = "Original at: " + link.href;
			link.href += ".png";
			link.href = wtf_replace(link.href, "imgur.com", "i.imgur.com");
		}
		else if(link.href.indexOf("i.imgur.com") == -1)
		{
			link.title = "Original at: " + link.href;
			link.href = wtf_replace(link.href, "imgur.com", "i.imgur.com");
		}
	}
}

function wtf_replace(str, text, rwith)
{
	return str.substr(0, str.indexOf(text)) + rwith + str.substr(str.indexOf(text) + text.length);
}

Don't mind the custom replace function, it was to debug the regex, but I figure this could well be faster and I can't be arsed removing it.

Just set it to work on all of Reddit, and every link to imgur will be turned into the fast version.


Leave a Reply