This repository was archived by the owner on Dec 11, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent.js
More file actions
115 lines (111 loc) · 3.04 KB
/
Copy pathcontent.js
File metadata and controls
115 lines (111 loc) · 3.04 KB
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
function loadStorage(key) {
return new Promise((resolve) => {
chrome.storage.local.get([key], (result) => {
resolve(result[key]);
});
});
}
function start() {
chrome.runtime.sendMessage({
type: "info",
data: "Starting to hide comments...",
});
let count = 0;
loadStorage("config").then((config) => {
const authorsToHide = [];
const commentTextsToHide = [];
if (!config) {
chrome.runtime.sendMessage({
type: "error",
data: "No configured values found",
});
}
if (!Array.isArray(config)) {
chrome.runtime.sendMessage({
type: "error",
data: "Invalid config format, clear existing config and add again",
});
return;
}
config.forEach((item) => {
if (item.type === "name") {
authorsToHide.push(item.text.toLowerCase().trim());
} else if (item.type === "comment") {
commentTextsToHide.push(item.text.toLowerCase().trim());
}
});
document.querySelectorAll(".wpd-comment").forEach((comment) => {
if (comment.style.display === "none") {
return; // Already hidden
}
const authorName = comment
.querySelector(".wpd-comment-author")
.textContent.trim()
.toLowerCase();
const commentText = comment
.querySelector(".wpd-comment-text")
.textContent.trim()
.toLowerCase();
if (
authorsToHide.includes(authorName) ||
commentTextsToHide.includes(commentText)
) {
comment.style.display = "none";
count++;
}
});
chrome.runtime.sendMessage({
type: "info",
data: `Removed ${count} comments.`,
});
});
}
function observeCommentChanges() {
const observer = new MutationObserver((mutations) => {
chrome.runtime.sendMessage({
type: "info",
data: "Comment list changed, hiding comments...",
});
loadStorage("autoHideComments").then((runInBackground) => {
if (!runInBackground) return;
start();
});
});
// Wait for wpd-thread-list to exist
const threadList = document.querySelector(".wpd-thread-list");
if (threadList) {
observer.observe(threadList, {
childList: true,
subtree: true,
});
chrome.runtime.sendMessage({
type: "info",
data: "Observer attached to comment list",
});
} else {
// If not found, wait and try again
setTimeout(observeCommentChanges, 1000);
}
}
function powerUp() {
// Check if running in content script vs popup
if (typeof chrome.runtime !== "undefined") {
// Check if chrome.runtime.sendMessage exists (content script or popup)
try {
// Content scripts can send messages but don't have access to chrome.tabs
if (typeof chrome.tabs === "undefined") {
// This is a content script - run automatically
start();
} else {
loadStorage("autoHideComments").then((runInBackground) => {
if (!runInBackground) return;
start();
});
}
} catch (e) {
start();
}
}
observeCommentChanges();
}
powerUp();