Last active
December 27, 2015 19:19
-
-
Save clsr/7376276 to your computer and use it in GitHub Desktop.
Implementation of http://xkcd.com/1288/ as a userscript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// ==UserScript== | |
// @name xkcd 1288 substitutions | |
// @namespace http://github.com/mcef | |
// @description Substitutions that make reading the news more fun | |
// | |
// @include http://www.bbc.com/* | |
// @include http://www.cbc.ca/* | |
// @include http://www.theguardian.com/* | |
// @include http://www.telegraph.co.uk/* | |
// @include http://www.theonion.com/* | |
// @include http://www.foxnews.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', | |
} | |
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
Hi! Got here from explainxkcd! I wrote this https://github.com/istepaniuk/xkcd-substitutions/blob/master/xkcd-substitutions.user.js just now, yours sure looks more sophisticated!