Skip to content

Instantly share code, notes, and snippets.

@pronto
Last active December 27, 2015 18:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pronto/7373279 to your computer and use it in GitHub Desktop.
Save pronto/7373279 to your computer and use it in GitHub Desktop.
xkcd for today http://xkcd.com/1288/
// ==UserScript==
// @name Replace Text On Webpages
// @namespace http://userscripts.org/users/23652
// @description Replaces text on websites. Now supports wildcards in search queries. Won't replace text in certain tags like links and code blocks
// @include http://*
// @include https://*
// @exclude http://userscripts.org/scripts/review/*
// @copyright JoeSimmons
// @version 1.0.52
// @license http://creativecommons.org/licenses/by-nc-nd/3.0/us/
// @require http://sizzlemctwizzle.com/updater.php?id=41369
// ==/UserScript==
// Note: You can use \\* to match actual asterisks instead of using it as a wildcard!
// The examples below show a wildcard in use and a regular asterisk replacement.
var words = {
///////////////////////////////////////////////////////
//http://xkcd.com/1288/
// Syntax: 'Search word' : 'Replace word',
'witnesses' : ' these dudes i know',
'allegedly' : 'kinda probably',
'new study' : 'tumblr post',
'rebuild' : 'avenge',
'space' : 'spaaaaace',
'google glass' : 'virtual boy',
'smartphone' : 'Pokédex',
'eletric' : 'atomic',
'senator' : 'elf-lord',
'car' : 'cat',
'election' : 'eating contest',
'congressional leaders' : 'river spirits',
'Homeland Security' : 'HomeStar Runner',
'Could not be reached for comment' : 'Is guilty and everyone knows it',
//non xkcd additions
"Google" : "Black Mesa", //https://twitter.com/fakejaku/status/398856295039913984
///////////////////////////////////////////////////////
"":""};
//////////////////////////////////////////////////////////////////////////////
// This is where the real code is
// Don't edit below this
//////////////////////////////////////////////////////////////////////////////
// prepareRegex by JoeSimmons
// Used to take a string and ready it for use in new RegExp()
String.prototype.prepareRegex = function() {
return this.replace(/([\[\]\^\&\$\.\(\)\?\/\\\+\{\}\|])/g, "\\$1");
};
// Function to decide whether a parent tag will have its text replaced or not
function isOkTag(tag) {
return (new RegExp("(," + tag + ",) | (," + tag + "$)", "g").test(",pre,blockquote,code,input,button,textarea")) == false;
}
// Convert the "words" JSON object to an Array
var regexs=new Array(),
replacements=new Array();
for(var word in words) {
if(word != "") {
regexs.push(new RegExp(word.prepareRegex().replace(/(\\)?\*/g, function(e) {return ((e !== "\\*") ? "[^ ]*" : "*");}), "gi"));
replacements.push(words[word]);
}
}
// Do the replacement
var texts = document.evaluate(".//text()[normalize-space(.)!='']",document.body,null,6,null), text="", len=regexs.length;
for(var i=0,l=texts.snapshotLength; (this_text=texts.snapshotItem(i)); i++) {
if(isOkTag(this_text.parentNode.tagName) && (text=this_text.textContent)) {
for(var x=0; x<len; x++) text = this_text.textContent = text.replace(regexs[x], replacements[x]);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment