Skip to content

Instantly share code, notes, and snippets.

@spacekitcat
Last active October 13, 2018 20:15
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 spacekitcat/d46dbb01facd9a4481b367e52b3fe8ac to your computer and use it in GitHub Desktop.
Save spacekitcat/d46dbb01facd9a4481b367e52b3fe8ac to your computer and use it in GitHub Desktop.
Walks the DOM and performs text replacements. Designed for use with https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver
doReplace = function(doc) {
var treeWalker = document.createTreeWalker(doc, NodeFilter.SHOW_TEXT, null, null)
do {
var tmpnode = treeWalker.currentNode;
if (tmpnode.nodeValue) {
tmpnode.nodeValue = tmpnode.nodeValue.replace(/gamergate/ig, 'misogynist');
tmpnode.nodeValue = tmpnode.nodeValue.replace(/trump/ig, 'misogynist');
tmpnode.nodeValue = tmpnode.nodeValue.replace(/donald trump/ig, 'misogynist');
treeWalker.currentNode = tmpnode;
}
} while (treeWalker.nextNode());
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment