-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
104 lines (93 loc) · 2.93 KB
/
content.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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
(function() {
let blockedKeywords = [];
let blockedRegex = null;
// Escape special regex characters.
function escapeRegExp(string) {
return string.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
}
// Build a regex that matches any blocked keyword, optionally preceded by '#'.
function updateBlockedRegex() {
if (!blockedKeywords || blockedKeywords.length === 0) {
blockedRegex = null;
return;
}
const escaped = blockedKeywords
.map(k => escapeRegExp(k.trim()))
.filter(k => k.length > 0);
if (escaped.length === 0) {
blockedRegex = null;
return;
}
const pattern = `(?:#)?(?:${escaped.join('|')})`;
blockedRegex = new RegExp(pattern, 'i');
}
// Check if an element's innerText matches the blocked regex.
function checkAndRemoveElement(el) {
if (!el || !el.innerText || !blockedRegex) return false;
if (blockedRegex.test(el.innerText)) {
el.remove();
return true;
}
return false;
}
// Define selectors for posts and comments.
const selectors = [
'div.feed-shared-update-v2',
'div.comments-comment-item',
'span.feed-shared-inline-show-more-text'
];
// Process an element and its descendants matching our selectors.
function processElement(element) {
if (element.matches && selectors.some(selector => element.matches(selector))) {
checkAndRemoveElement(element);
}
selectors.forEach(selector => {
element.querySelectorAll(selector).forEach(child => {
checkAndRemoveElement(child);
});
});
}
// Perform a full scan of the document.
function fullScan() {
selectors.forEach(selector => {
document.querySelectorAll(selector).forEach(el => {
checkAndRemoveElement(el);
});
});
}
// Load keywords from storage.
chrome.storage.sync.get({ keywords: [] }, data => {
blockedKeywords = data.keywords;
updateBlockedRegex();
fullScan();
});
// Update keywords when they change.
chrome.storage.onChanged.addListener((changes, area) => {
if (area === 'sync' && changes.keywords) {
blockedKeywords = changes.keywords.newValue;
updateBlockedRegex();
fullScan();
}
});
// MutationObserver to process newly added nodes.
const observer = new MutationObserver(mutations => {
mutations.forEach(mutation => {
mutation.addedNodes.forEach(node => {
if (node.nodeType === Node.ELEMENT_NODE) {
processElement(node);
}
});
});
});
observer.observe(document.body, { childList: true, subtree: true });
// Scroll listener: run a full scan after scrolling stops.
let scrollTimer;
window.addEventListener('scroll', () => {
clearTimeout(scrollTimer);
scrollTimer = setTimeout(() => {
fullScan();
}, 300);
});
// Fallback: periodic full scan.
setInterval(fullScan, 3000);
})();