forked from dnbard/dou-black-list
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.ts
186 lines (177 loc) · 5.63 KB
/
extension.ts
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
176
177
178
179
180
181
182
183
184
185
186
type Stats = {
registration: string;
activities: Element[];
registrationShort: string;
activitiesShort: string[];
shouldShowLongVersion?: boolean;
};
type StatsProperty = {
stats: Stats | Promise<void>;
};
type DouComment = HTMLElement;
const YEAR = new Date().getFullYear();
const SELECTORS = {
comment: ".comment",
author: ".b-post-author > a",
text: ".comment_text",
};
const STORAGE_KEY = "__dou_black_list__";
const HIDDEN_COMMENT = `<div class="_banned">
<div class="b-post-author">
<a class="avatar">
<img
class="g-avatar"
alt="avatar"
src="https://s.dou.ua/img/avatars/80x80_966.png"
width="25"
height="25"
/>
Banned user
</a>
</div>
<div class="comment_text b-typo">Hidden content, click to show</div>
</div>
`;
const getTextElement = (comment: DouComment) =>
comment.querySelectorAll(SELECTORS.text)[0] as HTMLElement;
const getText = (comment: DouComment) => getTextElement(comment).innerText;
const getAuthorElement = (comment: DouComment) =>
comment.querySelectorAll(SELECTORS.author)[0] as HTMLAnchorElement;
const getAuthor = (comment: DouComment) => {
return getAuthorElement(comment)?.href?.match(/users\/(.+)\//)?.[1];
};
const getStorage = (): Record<string, boolean> =>
JSON.parse(localStorage.getItem(STORAGE_KEY) || "{}");
(() => {
const storage = getStorage();
const index: Record<string, Array<DouComment> & StatsProperty> = {};
const isCommentFromBanned = (comment: DouComment) =>
!!storage[getAuthor(comment)];
function updateStorage(key: string, value) {
storage[key] = value;
localStorage.setItem(STORAGE_KEY, JSON.stringify(storage));
}
function addBanButtonAndInfo(comment: DouComment) {
const author = getAuthorElement(comment);
if (!getAuthor(comment)) {
return;
}
const existingBanButton =
author.parentElement.querySelectorAll("._ban_button")[0];
if (existingBanButton) {
author.parentElement.removeChild(existingBanButton);
}
const existingInfoblock =
author.parentElement.querySelectorAll("._ban_infoblock")[0];
if (existingInfoblock) {
author.parentElement.removeChild(existingInfoblock);
}
const button = document.createElement("button");
button.classList.add("_ban_button");
button.innerText = isCommentFromBanned(comment) ? "😇" : "🤡";
button.title = isCommentFromBanned(comment) ? "unban" : "ban";
button.onclick = (e) => {
e.stopPropagation();
const authorName = getAuthor(comment);
const yes = confirm(
`${isCommentFromBanned(comment) ? "unban" : "ban"} ${authorName}?`
);
if (yes) {
updateStorage(authorName, !storage[authorName]);
const commentsByAuthor = index[authorName];
commentsByAuthor.forEach((comment) => {
addBanButtonAndInfo(comment);
hideContentIfNeeded(comment);
});
console.log(`Updated ${commentsByAuthor.length} comments`);
}
};
author.parentElement.appendChild(button);
const stats = index[getAuthor(comment)].stats as Stats;
if (!stats?.activitiesShort && !stats?.registrationShort) {
return;
}
const infoBlock = document.createElement("span");
let text = [];
if (stats.shouldShowLongVersion) {
infoBlock.innerHTML = stats.registration;
stats.activities.forEach((c) => infoBlock.appendChild(c));
} else {
if (stats.activitiesShort[0]) {
text.push(`${stats.activitiesShort[0]} c.`);
}
if (stats.activitiesShort[1]) {
text.push(`${stats.activitiesShort[1]} t.`);
}
text.push(`${stats.registrationShort} yo.`);
infoBlock.innerText = text.join(" | ");
infoBlock.onclick = (e) => {
e.preventDefault();
stats.shouldShowLongVersion = true;
addBanButtonAndInfo(comment);
};
}
infoBlock.className = "_ban_infoblock cpointer";
infoBlock.title = "click";
author.parentElement.appendChild(infoBlock);
}
function hideContentIfNeeded(comment: DouComment) {
const text = getTextElement(comment);
if (!text) {
return;
}
if (isCommentFromBanned(comment)) {
comment.setAttribute("data-banned", comment.innerHTML);
comment.innerHTML = HIDDEN_COMMENT;
comment.onclick = (e) => {
e.stopPropagation();
const content = comment.getAttribute("data-banned");
if (content) {
// unhide
comment.removeAttribute("data-banned");
comment.innerHTML = content;
addBanButtonAndInfo(comment);
comment.onclick = () => {};
} else {
// hide again
comment.setAttribute("data-banned", comment.innerHTML);
comment.innerHTML = HIDDEN_COMMENT;
}
};
} else {
// unban clicked
const content = comment.getAttribute("data-banned");
if (content) {
comment.removeAttribute("data-banned");
comment.innerHTML = content;
addBanButtonAndInfo(comment);
comment.onclick = () => {};
}
}
}
function indexOne(comment: DouComment) {
const authorName = getAuthor(comment);
if (!authorName) {
return;
}
const text = getText(comment);
if (!text) {
return;
}
if (!index[authorName]) {
index[authorName] = [] as typeof index[string];
}
index[authorName].push(comment);
}
console.time(STORAGE_KEY);
[...document.querySelectorAll(SELECTORS.comment)].forEach(
(comment: DouComment) => {
indexOne(comment);
addBanButtonAndInfo(comment);
hideContentIfNeeded(comment);
}
);
// @ts-ignore
window.__dou_black_list__ = index;
console.timeEnd(STORAGE_KEY);
})();