forked from dnbard/dou-black-list
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.js
255 lines (254 loc) · 9.02 KB
/
extension.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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
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) => comment.querySelectorAll(SELECTORS.text)[0];
const getText = (comment) => getTextElement(comment).innerText;
const getAuthorElement = (comment) => comment.querySelectorAll(SELECTORS.author)[0];
const getAuthor = (comment) => {
return getAuthorElement(comment)?.href?.match(/users\/(.+)\//)?.[1];
};
const getStorage = () => JSON.parse(localStorage.getItem(STORAGE_KEY) || "{}");
(() => {
const storage = getStorage();
const index = {};
const isCommentFromBanned = (comment) => !!storage[getAuthor(comment)];
function updateStorage(key, value) {
storage[key] = value;
localStorage.setItem(STORAGE_KEY, JSON.stringify(storage));
}
function addBanButtonAndInfo(comment) {
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;
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) {
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) {
comment.removeAttribute("data-banned");
comment.innerHTML = content;
addBanButtonAndInfo(comment);
comment.onclick = () => { };
}
else {
comment.setAttribute("data-banned", comment.innerHTML);
comment.innerHTML = HIDDEN_COMMENT;
}
};
}
else {
const content = comment.getAttribute("data-banned");
if (content) {
comment.removeAttribute("data-banned");
comment.innerHTML = content;
addBanButtonAndInfo(comment);
comment.onclick = () => { };
}
}
}
function indexOne(comment) {
const authorName = getAuthor(comment);
if (!authorName) {
return;
}
const text = getText(comment);
if (!text) {
return;
}
if (!index[authorName]) {
index[authorName] = [];
}
index[authorName].push(comment);
}
console.time(STORAGE_KEY);
[...document.querySelectorAll(SELECTORS.comment)].forEach((comment) => {
indexOne(comment);
addBanButtonAndInfo(comment);
hideContentIfNeeded(comment);
});
window.__dou_black_list__ = index;
console.timeEnd(STORAGE_KEY);
})();
const SETTINGS_SELECTORS = {
userProfile: ".min-profile",
aboutUser: ".b-user-info",
};
const exportId = STORAGE_KEY + "export";
const importId = STORAGE_KEY + "import";
const importIdFile = STORAGE_KEY + "import_file";
function getSettingsHtml(settings) {
return `
<h3>Dou Block List Settings</h3>
<p>
Currently, there are ${Object.keys(settings).length} folks in the ban list.
</p>
<p>
<label>
<button type="button" id="${exportId}">📩</button>
Export block list as json file
</label>
</p>
<p>
<input type="file" style="display: none" id="${importIdFile}">
<label>
<button type="button" id="${importId}">📤</button>
Import block list from json file
</label>
</p>
`;
}
function isOnSettingsPage() {
const minProfile = document.querySelectorAll(SETTINGS_SELECTORS.userProfile)[0];
return (minProfile.tagName === "A" && minProfile.href === document.location.href);
}
function getOnFileUpload(importFileInput) {
return () => {
const fr = new FileReader();
fr.onload = function (e) {
const result = JSON.parse(e.target.result.toString());
updateStorageWithSettings(result);
};
fr.readAsText(importFileInput.files.item(0));
};
}
function attachEventListeners(injectElement) {
const exportButton = injectElement.querySelector(`#${exportId}`);
const importButton = injectElement.querySelector(`#${importId}`);
const importFileInput = injectElement.querySelector(`#${importIdFile}`);
exportButton.onclick = downloadSettings;
importButton.onclick = openUploadDialog;
importFileInput.onchange = getOnFileUpload(importFileInput);
}
function getInjectElement() {
const aboutUser = document.querySelectorAll(SETTINGS_SELECTORS.aboutUser)[0];
return aboutUser.parentElement;
}
function renderSettings(settings) {
const settingsElement = document.createElement("article");
settingsElement.className = "b-typo";
settingsElement.id = STORAGE_KEY;
settingsElement.innerHTML = getSettingsHtml(settings);
const injectElement = getInjectElement();
const existingSettings = injectElement.querySelectorAll(`#${STORAGE_KEY}`)[0];
if (existingSettings) {
injectElement.removeChild(existingSettings);
}
injectElement.appendChild(settingsElement);
attachEventListeners(injectElement);
}
function openUploadDialog() {
document.getElementById(importIdFile).click();
}
function updateStorageWithSettings(settings) {
if (!ensureSettings(settings)) {
return;
}
localStorage.setItem(STORAGE_KEY, JSON.stringify(settings));
renderSettings(getStorage());
}
function ensureSettings(settings) {
return (!Array.isArray(settings) &&
typeof settings === "object" &&
Object.keys(settings).every((key) => key !== "undefined"));
}
function downloadSettings() {
const storage = getStorage();
const json = JSON.stringify(storage, null, 2);
const blob = new Blob([json], { type: "octet/stream" });
const a = document.createElement("a");
a.href = window.URL.createObjectURL(blob);
a.download = `dou-block-list-${Object.keys(storage).length}-items.json`;
a.setAttribute("style", "display: none;");
a.rel = "noopener noreferrer";
a.click();
}
(() => {
if (!isOnSettingsPage()) {
return;
}
renderSettings(getStorage());
})();