-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontent.js
175 lines (162 loc) · 6.39 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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
let currentAuthor = null;
let minLikes = minComments = minRetweets = 0;
let likesCheckOn = commentsCheckOn = retweetsCheckOn = true;
let nonVerifiedCheckOn = false;
const mutePortuguese = 'este post pertence a uma conta que você silenciou';
const muteEnglish = 'this post is from an account you muted';
const baseXPath = './/article//div[2]/div/div/div[1]/div';
const spanXPath = baseXPath + '/span/span/span';
const textXPath = baseXPath + '/span/text()';
const interactionXPath = './/article/div/div/div[2]/div[2]/div[3]/div/div/div';
const replyInteractionXPath = './/article/div/div/div[2]/div[2]/div[4]/div/div/div';
const replyFocusXPath = './/article/div/div/div[3]/div[5]/div/div/div';
const interactionSpanXPath = '/div/div/div[2]/span/span/span'
const postPattern = /https:\/\/(twitter\.com|x\.com)\/.+\/status\/.*$/;
function removeElementByXPath(xpath) {
const element = document.evaluate(
xpath, document, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null
).singleNodeValue;
return element && removeParent(element, 12);
}
function removeParent(element, levels) {
let currentElement = element;
for (let i = 0; i < levels; i++) {
if (currentElement.parentElement) {
currentElement = currentElement.parentElement;
}
else {
return false;
}
}
currentElement.remove();
return true;
}
function waitUntilDocumentIsReady(callback) {
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', callback);
} else {
callback();
}
}
function findCommentAuthor(element) {
if (element) {
const xpath = './/article//div[2]/div[2]/div[1]//div[1]//div[1]//a'
const anchorElement = document.evaluate(
xpath, element, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null
).singleNodeValue;
if (anchorElement) {
return anchorElement.getAttribute('href');
}
}
return null;
}
function setCurrentPostAuthor(element) {
currentAuthor = findCommentAuthor(element);
}
function getTextByXPath(xpath, node) {
const result = document.evaluate(xpath, node, null, XPathResult.STRING_TYPE, null);
const textContent = result.stringValue.trim();
return textContent;
}
function findNodeByXPath(xpaths, element) {
for (var xpath of xpaths) {
const node = document.evaluate(xpath, element, null, XPathResult.FIRST_ORDERED_NODE_TYPE, null).singleNodeValue;
if (node) {
return node;
}
}
return null;
}
function isInteractionRelevant(node, value) {
const text = node.textContent.trim();
if (/[a-zA-Z]/.test(text)) {
return true;
}
return Number.isInteger(Number(text)) && Number(text) >= value;
}
function getInteractionXPaths(index) {
return [`${interactionXPath}[${index}]${interactionSpanXPath}`,
`${replyInteractionXPath}[${index}]${interactionSpanXPath}`,
`${replyFocusXPath}[${index}]${interactionSpanXPath}`]
}
function getUserPreferences(callback) {
chrome.storage.sync.get(
{ likes: 30,
retweets: 15,
comments: 10,
likesCheckbox: true,
retweetsCheckbox: true,
commentsCheckbox: true,
nonVerifiedCheckbox: false },
(preferences) => {
callback(preferences);
}
);
}
function isCommentRelevant(element) {
const containsPtMuted = element.innerText.toLowerCase().includes(mutePortuguese);
const containsEnMuted = element.innerText.toLowerCase().includes(muteEnglish);
if (containsPtMuted || containsEnMuted) {
return false;
}
if (nonVerifiedCheckOn || element.querySelector('svg[data-testid="icon-verified"]')) {
const commentXpaths = getInteractionXPaths(1);
const retweetXpaths = getInteractionXPaths(2);
const likeXpaths = getInteractionXPaths(3);
const commentText = findNodeByXPath(commentXpaths, element);
const retweetText = findNodeByXPath(retweetXpaths, element);
const likeText = findNodeByXPath(likeXpaths, element);
return (
(!commentsCheckOn && !retweetsCheckOn && !likesCheckOn) ||
(commentsCheckOn && commentText && isInteractionRelevant(commentText, minComments)) ||
(retweetsCheckOn && retweetText && isInteractionRelevant(retweetText, minRetweets)) ||
(likesCheckOn && likeText && isInteractionRelevant(likeText, minLikes))
);
}
return true;
}
waitUntilDocumentIsReady(() => {
try {
getUserPreferences((preferences) => {
minLikes = preferences.likes;
minRetweets = preferences.retweets;
minComments = preferences.comments;
likesCheckOn = preferences.likesCheckbox;
retweetsCheckOn = preferences.retweetsCheckbox;
commentsCheckOn = preferences.commentsCheckbox;
nonVerifiedCheckOn = preferences.nonVerifiedCheckbox;
console.log(
'User Preferences\n' +
`Min Likes: ${likesCheckOn ? minLikes : 'off'}\n` +
`Min Retweets: ${retweetsCheckOn ? minRetweets : 'off'}\n` +
`Min Comments: ${commentsCheckOn ? minComments : 'off'}\n` +
`Include non-verified users: ${nonVerifiedCheckOn ? 'on' : 'off'}`
);
});
observer = new MutationObserver((mutations) => {
if (postPattern.test(window.location.href)) {
for (const mutation of mutations) {
if (mutation.type === 'childList') {
mutationsOccurred = true;
const ancestors = document.querySelectorAll('div[data-testid="cellInnerDiv"]');
setCurrentPostAuthor(ancestors[0]);
ancestors.forEach((ancestor) => {
if (findCommentAuthor(ancestor) != currentAuthor && !isCommentRelevant(ancestor)) {
const child = ancestor.querySelector(':scope > div');
if (child) {
child.remove();
}
}
});
}
}
}
});
observer.observe(document.body, {
childList: true,
subtree: true
});
} catch (error) {
console.error('Error: ', error);
}
});