Skip to content

Instantly share code, notes, and snippets.

View ClientsideTextReplaceSpec.js
describe("ClientsideTextReplace 'doReplace' function", function() {
beforeEach(function() {
});
var createTestDocument = function() {
return document.implementation.createHTMLDocument("test doc");
}
describe("Is able to replace the core targets", function() {
@spacekitcat
spacekitcat / doReplace.js
Last active Oct 13, 2018
Walks the DOM and performs text replacements. Designed for use with https://developer.mozilla.org/en-US/docs/Web/API/MutationObserver
View doReplace.js
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');