Skip to content

Instantly share code, notes, and snippets.

@o0101
Last active August 28, 2020 10:30
Show Gist options
  • Save o0101/acd50bedeb1335065763e50d4783f711 to your computer and use it in GitHub Desktop.
Save o0101/acd50bedeb1335065763e50d4783f711 to your computer and use it in GitHub Desktop.
"use strict";
{
const forbidden = new Set(["Facebook", "Zuckerberg", "Trump", "alt-left", "Syria", "Trump's", "racist", "liberal", "alt-right"]);
const tw = document.createTreeWalker(document.body, NodeFilter.SHOW_TEXT); const actions = [];
while( tw.nextNode() ) {
const text = tw.currentNode;
if ( text.parentElement.tagName !== 'SCRIPT' && text.parentElement.tagName !== 'STYLE' ) {
actions.push( () => redact( text, forbidden ));
}
}; actions.forEach( a => a() );
function redact( t, e ) {
const p = t.parentElement;
if ( ! p ) {
return;
}
const converted = t.textContent.split(/\s/g).map( word => {
if ( e.has(word) ) {
const s = document.createElement('span');
s.style = "color:black;background:black;";
s.innerText = word + " ";
return s;
} else return word + " ";
});
t.replaceWith( ...converted );
p.normalize();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment