Created
March 13, 2013 16:39
-
-
Save karthik/5153903 to your computer and use it in GitHub Desktop.
big butts
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 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 = { | |
/////////////////////////////////////////////////////// | |
// Syntax: 'Search word' : 'Replace word', | |
"big data" : "big butts", | |
/////////////////////////////////////////////////////// | |
"":""}; | |
////////////////////////////////////////////////////////////////////////////// | |
// 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