Created
March 7, 2022 00:43
-
-
Save Noleli/c7402c2455815435ace1a536a99cb088 to your computer and use it in GitHub Desktop.
Billionaire-to-oligarch browser extension
This file contains 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
const re = /billionaire/gi; | |
const replacer = (match) => { | |
if(match === "Billionaire") { | |
return "Oligarch"; | |
} | |
else if(match === "BILLIONAIRE") { | |
return "OLIGARCH"; | |
} | |
else { | |
return "oligarch"; | |
} | |
} | |
const observer = new MutationObserver(mutationList => { | |
mutationList.forEach(mutation => { | |
if(mutation.type === "characterData") { | |
replaceText(mutation.target); | |
} | |
else if(mutation.type === "childList") { | |
processNodes(mutation.target); | |
} | |
}); | |
}); | |
observer.observe(document.body, { | |
subtree: true, | |
characterData: true, | |
childList: true | |
}); | |
const replaceText = node => { | |
if(re.test(node.textContent)) { | |
node.textContent = node.textContent.replaceAll(re, replacer); | |
} | |
}; | |
const processNodes = rootNode => { | |
const nodeIterator = document.createNodeIterator(rootNode, NodeFilter.SHOW_TEXT | |
); | |
while(currentNode = nodeIterator.nextNode()) { | |
replaceText(currentNode); | |
} | |
}; | |
processNodes(document.body); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment