-
Notifications
You must be signed in to change notification settings - Fork 0
/
Page Filter.user.js
55 lines (47 loc) · 1.78 KB
/
Page Filter.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
// ==UserScript==
// @name Page Filter
// @namespace http://tampermonkey.net/
// @version 1.0.1
// @license GNU AGPLv3
// @description Removes links, images, and text which refer to specific keywords. When a keyword is found in an URL of a link or image, the link/image will be removed. When a keyword is found in a text, the whole text in its container element, will be removed.
// @author jcunews
// @match *://*/*
// @grant none
// ==/UserScript==
(function() {
//*** CONFIGURATION BEGIN ***
//Below regular expression will be compared against URLs and text.
//Put anything there to remove them from the page.
var rx = /\b(removeme|deleteme)\b/gi;
//*** CONFIGURATION END ***
function processElement(node, url, nextNode, styles) {
if (rx.test(node.href || node.src) || ((styles = getComputedStyle(node)) && rx.test(styles.backgroundImage))) {
if (rx.test(node.parentNode.textContent)) {
node.parentNode.innerHTML = "";
} else node.remove();
} else {
for (node = node.childNodes[0]; node; node = nextNode) {
nextNode = node.nextSibling;
processNode(node);
}
}
}
function processNode(node) {
switch (node.nodeType) {
case Node.ELEMENT_NODE:
processElement(node);
break;
case Node.TEXT_NODE:
if (rx.test(node.nodeValue)) node.nodeValue = "";
break;
}
}
processNode(document.body);
(new MutationObserver(function(records) {
records.forEach(function(record) {
if (record.type === "characterData") {
if (rx.test(record.target.nodeValue)) record.target.nodeValue = "";
} else record.addedNodes.forEach(processNode);
});
})).observe(document.body, {childList: true, characterData: true, subtree: true});
})();