Skip to content

Instantly share code, notes, and snippets.

Created June 11, 2013 03:49
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 anonymous/5754378 to your computer and use it in GitHub Desktop.
Save anonymous/5754378 to your computer and use it in GitHub Desktop.
Cloud-to-Butt
// ==UserScript==
// @name s/the cloud/my butt/g
// @description Replaces the word "the cloud" with "my butt".
// @match *://*/*
// ==/UserScript==
function buttize(str) {
return str.replace(/the cloud/g,"my butt")
.replace(/the Cloud/g,"my Butt")
.replace(/The Cloud/g,"My Butt")
.replace(/THE CLOUD/g,"MY BUTT")
.replace(/t[Hh][Ee] c[Ll][Oo][Uu][Dd]/g,"my butt")
.replace(/t[Hh][Ee] C[Ll][Oo][Uu][Dd]/g,"my Butt")
.replace(/T[Hh][Ee] [Cc][Ll][Oo][Uu][Dd]/g,"My Butt")
}
var replacingContent = false
function replaceTextContent(node) {
//flag that content is being replaced so the event it generates
//won't trigger another replacement
replacingContent = true
node.textContent = buttize(node.textContent)
replacingContent = false
}
function changeTextNodes(node) {
var length, childNodes
//If this is a text node, buttize it
if (node.nodeType == Node.TEXT_NODE) {
replaceTextContent(node)
//If this is anything other than a text node, recurse any children
} else {
childNodes = node.childNodes
length = childNodes.length
for(var i=0; i<length; ++i){
changeTextNodes(childNodes[i])
}
}
}
function insertion_listener(event) {
//change any new text nodes in a node that is added to the body
changeTextNodes(event.target)
}
function cdm_listener(event) {
//avoid infinite loop by ignoring events triggered by replacement
if(!replacingContent){
replaceTextContent(event.target)
}
}
changeTextNodes(document.body)
document.title = buttize(document.title)
document.body.addEventListener ("DOMNodeInserted", insertion_listener, false)
document.body.addEventListener ("DOMCharacterDataModified", cdm_listener, false)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment