Last active
December 25, 2021 19:52
-
-
Save 3nt3/cb55d930993c55ac67edcdbcc33c3e0a to your computer and use it in GitHub Desktop.
This is a greasemonkey script that replaces the word "cloud" with "butt" on websites.
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 Cloud to Butt | |
// @namespace https://3nt3.de | |
// @include * | |
// @version 1 | |
// ==/UserScript== | |
(function () { | |
"use strict"; | |
var words = { | |
cloud: "butt", | |
Cloud: "Butt", | |
}; | |
/////////////////////////////////////////////////////////////////////////////// | |
var regexs = [], | |
replacements = [], | |
tagsWhitelist = [ | |
"PRE", | |
"BLOCKQUOTE", | |
"CODE", | |
"INPUT", | |
"BUTTON", | |
"TEXTAREA", | |
], | |
rIsRegexp = /^\/(.+)\/([gm]+)?$/, | |
word, | |
text, | |
texts, | |
i, | |
userRegexp; | |
// prepareRegex by JoeSimmons | |
// used to take a string and ready it for use in new RegExp() | |
function prepareRegex(string) { | |
return string.replace(/([\[\]\^\&\$\.\(\)\?\/\\\+\{\}\|])/g, "\\$1"); | |
} | |
// function to decide whether a parent tag will have its text replaced or not | |
function isTagOk(tag) { | |
return tagsWhitelist.indexOf(tag) === -1; | |
} | |
// convert the 'words' JSON object to an Array | |
for (word in words) { | |
if (typeof word === "string" && words.hasOwnProperty(word)) { | |
userRegexp = word.match(rIsRegexp); | |
// add the search/needle/query | |
if (userRegexp) { | |
regexs.push(new RegExp(userRegexp[1], "g")); | |
} else { | |
regexs.push( | |
new RegExp( | |
prepareRegex(word).replace(/\\?\*/g, function (fullMatch) { | |
return fullMatch === "\\*" ? "*" : "[^ ]*"; | |
}), | |
"g" | |
) | |
); | |
} | |
// add the replacement | |
replacements.push(words[word]); | |
} | |
} | |
// do the replacement | |
texts = document.evaluate( | |
'//body//text()[ normalize-space(.) != "" ]', | |
document, | |
null, | |
6, | |
null | |
); | |
for (i = 0; (text = texts.snapshotItem(i)); i += 1) { | |
if (isTagOk(text.parentNode.tagName)) { | |
regexs.forEach(function (value, index) { | |
text.data = text.data.replace(value, replacements[index]); | |
}); | |
} | |
} | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment