-
Notifications
You must be signed in to change notification settings - Fork 0
/
content.js
48 lines (40 loc) · 1.44 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
const filterTweets = () => {
chrome.storage.sync.get('filter', (filter) => {
const filterText = filter.filter;
if (!filterText || !filterText.length) return;
const tweets = document.querySelectorAll('[data-testid="tweet"]');
tweets.forEach((tweet) => {
const tweetText = tweet.innerText.toLowerCase();
if (tweetText.includes(filterText.toLowerCase())) {
// Hide the tweet by setting display: none
tweet.style.display = 'none';
// Traverse up the DOM tree to find the parent div (3 levels up)
let parentDiv = tweet;
for (let i = 0; i < 3; i++) {
parentDiv = parentDiv.parentNode;
if (!parentDiv) break; // To handle cases where the tree depth is less than 3
}
// Check if the parent div has the attribute "data-testid='cellInnerDiv'"
if (parentDiv && parentDiv.getAttribute('data-testid') === 'cellInnerDiv') {
// Hide the parent div as well
parentDiv.style.display = 'none';
}
}
});
});
};
chrome.runtime.onMessage.addListener((message, sender, sendResponse) => {
if (message.action === 'filter') {
filterTweets();
}
});
const MutationObserver = window.MutationObserver || window.WebKitMutationObserver;
const observer = new MutationObserver((mutations, observer) => {
filterTweets();
});
observer.observe(document, {
subtree: true,
attributes: true
});
// run first time we load
filterTweets();