Skip to content

Instantly share code, notes, and snippets.

@jiaaro
Last active December 27, 2015 18:48
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save jiaaro/7372006 to your computer and use it in GitHub Desktop.
Save jiaaro/7372006 to your computer and use it in GitHub Desktop.
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

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

@Coder5432
Copy link

will this work as a bookmarklet?

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