-
Notifications
You must be signed in to change notification settings - Fork 0
/
content-comment.js
86 lines (72 loc) · 2.65 KB
/
content-comment.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
let shouldStopDeleting = false;
let autoScrollTimeout;
chrome.runtime.onMessage.addListener(async (message) => {
if (message.action === 'startComment') {
shouldStopDeleting = false;
await removeComments();
} else if (message.action === 'stopComment') {
shouldStopDeleting = true;
clearTimeout(autoScrollTimeout);
}
});
async function removeComments() {
const commentRemovalDelay = 5000;
const pauseBetweenRemovals = 5000;
const commentSelector = ".YxbmAc";
const scrollThrottleTime = 200;
const autoScrollThreshold = 3;
const promisifiedTimeout = (ms) => new Promise(resolve => setTimeout(resolve, ms));
function debounce(func, wait) {
let timeout;
return function () {
const context = this, args = arguments;
clearTimeout(timeout);
timeout = setTimeout(function () {
timeout = null;
func.apply(context, args);
}, wait);
};
}
const debouncedAutoScroll = debounce(autoScroll, scrollThrottleTime);
function autoScroll() {
let lastScrollTime = 0;
const currentTime = Date.now();
if (currentTime - lastScrollTime >= scrollThrottleTime) {
window.scrollTo({ top: document.body.scrollHeight, behavior: "smooth" });
lastScrollTime = currentTime;
}
if (!shouldStopDeleting) {
autoScrollTimeout = setTimeout(debouncedAutoScroll, scrollThrottleTime);
}
}
async function deleteComment(element) {
if (shouldStopDeleting) {
return;
}
try {
const deleteButton = element.querySelector(".VfPpkd-rymPhb-pZXsl, .VfPpkd-Bz112c-LgbsSe");
if (deleteButton) {
deleteButton.click();
await promisifiedTimeout(commentRemovalDelay);
} else {
console.warn("Delete button not found for the comment:", element);
}
} catch (error) {
console.error("Error deleting comment:", error, element);
}
}
async function removeCommentsSequentially() {
const commentList = document.querySelectorAll(commentSelector);
if (commentList.length === 0) {
return;
}
for (let i = 0; i < commentList.length && !shouldStopDeleting; i++) {
await deleteComment(commentList[i]);
if (i < commentList.length - 1 && (commentList.length - i - 1) <= autoScrollThreshold) {
debouncedAutoScroll();
await promisifiedTimeout(pauseBetweenRemovals);
}
}
}
await removeCommentsSequentially();
}