Skip to content

Instantly share code, notes, and snippets.

@belkarx
Created May 2, 2024 21:27
Show Gist options
  • Save belkarx/9a8a7670645d3c73fb97c5d0399dc2e2 to your computer and use it in GitHub Desktop.
Save belkarx/9a8a7670645d3c73fb97c5d0399dc2e2 to your computer and use it in GitHub Desktop.
black out words that stress you out
// ==UserScript==
// @name Text Blackout (Efficient)
// @namespace http://tampermonkey.net/
// @version 1.0
// @description Efficiently blacks out text segments from the array "a" on every page
// @match *://*/*
// @grant none
// ==/UserScript==
(function() {
'use strict';
// Array of text segments to black out
var a = ["rape", "rapist"];
// Create a regular expression from the array
var regex = new RegExp("(" + a.join("|") + ")", "gi");
// Function to black out text segments
function blackoutText(textNode) {
var text = textNode.textContent;
var matches = text.match(regex);
if (matches) {
var fragment = document.createDocumentFragment();
var startIndex = 0;
matches.forEach(function(match) {
var index = text.indexOf(match, startIndex);
if (index > startIndex) {
fragment.appendChild(document.createTextNode(text.slice(startIndex, index)));
}
var span = document.createElement('span');
span.style.backgroundColor = 'black';
span.style.color = 'black';
span.textContent = match;
fragment.appendChild(span);
startIndex = index + match.length;
});
if (startIndex < text.length) {
fragment.appendChild(document.createTextNode(text.slice(startIndex)));
}
textNode.parentNode.replaceChild(fragment, textNode);
}
}
// Function to process text nodes efficiently
function processTextNodes(node) {
var walker = document.createTreeWalker(node, NodeFilter.SHOW_TEXT, null, false);
var textNodes = [];
while (walker.nextNode()) {
textNodes.push(walker.currentNode);
}
textNodes.forEach(blackoutText);
}
// Function to run the script periodically
function runScript() {
processTextNodes(document.body);
}
// Run the script initially
runScript();
// Set an interval to run the script every 10 seconds
setInterval(runScript, 1000);
})();
@belkarx
Copy link
Author

belkarx commented May 2, 2024

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment