-
-
Notifications
You must be signed in to change notification settings - Fork 35
/
toggle-all-comments-and-replies.js
executable file
·87 lines (69 loc) · 2.24 KB
/
toggle-all-comments-and-replies.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
import { getTopLevelComments, getAllComments } from "../libs/dom-utils";
import { paths } from "../libs/paths";
function toggleAllReplies() {
const allComments = [...getAllComments()];
allComments.forEach((comment, id) => {
const currentIndent = comment.querySelector("td.ind img").width / 40;
const nextIndent =
id + 1 === allComments.length
? undefined
: allComments[id + 1].querySelector("td.ind img").width / 40;
if (nextIndent && nextIndent > currentIndent) {
const fontTag = document.createElement("font");
fontTag.setAttribute("size", 1);
const toggleAllBtn = document.createElement("a");
toggleAllBtn.innerHTML = "toggle all replies";
toggleAllBtn.href = "javascript:void(0)";
toggleAllBtn.addEventListener("click", () => {
const n = comment.querySelector("a.togg").getAttribute("n") - 1;
for (let i = id + 1; i <= id + n; i++) {
allComments[i].querySelector("a.togg").click();
}
});
fontTag.append(toggleAllBtn);
const fontTagParent = comment.querySelector("div.reply p");
if (fontTagParent.innerText.includes("reply")) {
fontTagParent.style.fontSize = "10px";
fontTagParent.append(" | ");
}
fontTagParent.append(fontTag);
}
});
}
function toggleAllComments() {
const topLevelComments = getTopLevelComments();
if (topLevelComments.length === 0) {
return false;
}
const target =
document.querySelector("table.fatitem td.subtext") ||
document.querySelector("table.fatitem span.comhead");
const toggleAllBtn = document.createElement("a");
toggleAllBtn.innerHTML = "toggle all comments";
toggleAllBtn.href = "javascript:void(0)";
toggleAllBtn.addEventListener("click", () => {
for (const comment of topLevelComments) {
comment.querySelector("a.togg").click();
}
});
target.append(" | ", toggleAllBtn);
}
function init(metadata) {
if (metadata.options.toggleAllReplies) {
toggleAllReplies();
}
if (metadata.item.isItem) {
toggleAllComments();
}
return true;
}
const details = {
id: "toggle-all-comments-and-replies",
pages: {
include: paths.comments,
exclude: [],
},
loginRequired: false,
init,
};
export default details;