Skip to content

Instantly share code, notes, and snippets.

@jiaaro
Last active Dec 27, 2015
Embed
What would you like to do?
v1 of implementing xkcd substituions: http://xkcd.com/1288/
function replace(txt) {
txt = txt.replace(/witnesses/gi, "dudes I know");
txt = txt.replace(/allegedly/gi, "kinda probably");
txt = txt.replace(/new study/gi, "tumblr post");
txt = txt.replace(/rebuild/gi, "avenge");
txt = txt.replace(/space/gi, "spaaace");
txt = txt.replace(/google glass/gi, "virtual boy");
txt = txt.replace(/smartphone/gi, "pokédex");
txt = txt.replace(/electric/gi, "atomic");
txt = txt.replace(/senator/gi, "elf-lord");
txt = txt.replace(/car/gi, "cat");
txt = txt.replace(/election/gi, "eating contest");
txt = txt.replace(/congressional leaders/gi, "river spirits");
txt = txt.replace(/homeland security/gi, "homestar runner");
txt = txt.replace(/could not be reached for comment/gi, "is guilty and everyone knows it");
return txt;
}
function replace_all_on_page() {
function recurse(element)
{
if (element.childNodes.length > 0)
for (var i = 0; i < element.childNodes.length; i++)
recurse(element.childNodes[i]);
if (element.nodeType == Node.TEXT_NODE) {
window.el = element
element.textContent = replace(element.textContent);
}
}
var html = document.getElementsByTagName('html')[0];
recurse(html);
}
replace_all_on_page();
@daurnimator
Copy link

daurnimator commented Nov 8, 2013

Iterating through document.all might be better (benchmarking required)

@Coder5432
Copy link

Coder5432 commented Nov 8, 2013

will this work as a bookmarklet?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment