Skip to content

Instantly share code, notes, and snippets.

@Noleli
Created March 7, 2022 00:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Noleli/c7402c2455815435ace1a536a99cb088 to your computer and use it in GitHub Desktop.
Save Noleli/c7402c2455815435ace1a536a99cb088 to your computer and use it in GitHub Desktop.
Billionaire-to-oligarch browser extension
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