Block sensitive words on a specific news website.
当前为
// ==UserScript==
// @name Block sensitive words
// @namespace http://tampermonkey.net/
// @version 2025-02-16
// @description Block sensitive words on a specific news website.
// @author You
// @match https://www.index.hr/*
// @icon https://www.google.com/s2/favicons?sz=64&domain=index.hr
// @grant none
// @license MIT
// ==/UserScript==
(function() {
'use strict';
const trigger = () => {
const sensitiveWords = [
"magarc",
"magarac",
"konj",
"mačk",
"mačak",
"tovar",
"životinj",
"ljubimac",
"ljubimci",
];
console.log(`Blocking ${sensitiveWords.length} sensitive words`);
try {
if (sensitiveWords.some(x => document.title.toLowerCase().includes(x))) {
document.title = "";
document.querySelector("#comments-container").innerHTML = "";
}
} catch {
//
}
try {
if (sensitiveWords.some(x => document.URL.toLowerCase().includes(x))) {
window.history.pushState('/', 'Title', '/');
}
} catch {
//
}
const isSensitive = (text) => {
try {
return sensitiveWords.some(x => text.toLowerCase().includes(x));
} catch {
console.log("Something failed with checking isSensitive");
return false;
}
}
const elements = document.querySelectorAll('h1,h2,h3,.title,div.summary,a,p,.text-holder');
const length = elements.length;
console.log(`Found ${length} elements`);
for (let i = 0; i < length - 1; i++) {
try {
if (isSensitive(elements[i].innerHTML)) {
elements[i].innerHTML = "";
}
} catch {
//
}
}
const images = document.querySelectorAll('img');
const imgLength = elements.length;
console.log(`Found ${imgLength} images`);
for (let z = 0; z < imgLength - z; z++) {
try {
if (isSensitive(images[z].alt)) {
images[z].parentElement.innerHTML = "";
}
} catch {
//
}
}
};
trigger();
const resizeObserver = new ResizeObserver(() => {
trigger();
});
resizeObserver.observe(document.querySelector("body"));
})();