Skip to content

Instantly share code, notes, and snippets.

@GeorgeHahn
Forked from clsr/1288.user.js
Last active August 29, 2015 13:56
Show Gist options
  • Save GeorgeHahn/8821738 to your computer and use it in GitHub Desktop.
Save GeorgeHahn/8821738 to your computer and use it in GitHub Desktop.
// ==UserScript==
// @name xkcd 1288 substitutions
// @namespace http://github.com/mcef
// @description Substitutions that make reading the news more fun
//
// @include *
// @exclude *github.com/*
// ==/UserScript==
(function() {
var substitutions = {
'witnesses': 'these dudes I know',
'allegedly': 'kinda probably',
'new study': 'Tumblr post',
'new studies': 'Tumblr posts',
'rebuild': 'avenge',
'space': 'spaaace',
'google glass': 'Virtual Boy',
'smartphone': 'Pokédex',
'smartphones': 'Pokédexes',
'electric': 'atomic',
'senator': 'elf-lord',
'senators': 'elf-lords',
'car': 'cat',
'cars': 'cats',
'election': 'eating contest',
'elections': 'eating contests',
'congressional leader': 'river spirit',
'congressional leaders': 'river spirits',
'homeland security': 'Homestar Runner',
'could not be reached for comment': 'is guilty and everyone knows it',
'right': 'riiight',
'now': 'meow'
}
var regexps = [];
for (var search in substitutions) {
regexps.push([new RegExp('\\b' + search.replace(' ', '\\s+') + '\\b', 'gi'), substitutions[search]]);
}
var substitute = function(node) {
var text = node.nodeValue;
for (var i=0; i<regexps.length; i++) {
var search = regexps[i][0];
var replace = regexps[i][1];
text = text.replace(search, function(match) {
if (match == match.toUpperCase()) {
return replace.toUpperCase();
}
var c = match.trim().charAt(0);
if (c == c.toUpperCase()) {
return replace.charAt(0).toUpperCase() + replace.slice(1);
}
return replace;
});
}
node.nodeValue = text;
}
var findNodes = function(node) {
if (node.nodeType == 3) { // text node
substitute(node);
} else {
for (var child=node.firstChild; child; child=child.nextSibling) {
findNodes(child);
}
}
};
findNodes(document.body);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment