-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathban-hn.js
159 lines (137 loc) · 4.32 KB
/
ban-hn.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
// ==UserScript==
// @author dylanarmstrong
// @description Ban people I dislike on HN
// @grant none
// @match https://news.ycombinator.com/*
// @name ban-hn
// @namespace https://github.com/dylanarmstrong/userscripts/
// @supportURL https://github.com/dylanarmstrong/userscripts/issues
// @updateURL https://raw.githubusercontent.com/dylanarmstrong/userscripts/main/ban-hn.js
// @version 3
// ==/UserScript==
/**
* HN has really bad moderation, so it's easier to just block the people who are cancer.
*/
(function main() {
// Store the version # in localStorage
// this will be useful for possible breaking changes
// How many ? can you fit on one line?
const version = 1;
const keys = {
users: 'filter.users',
version: 'filter.version',
};
const currentVersion = Number.parseInt(localStorage.getItem(keys.version));
// Migrate from 0 -> 1
if (currentVersion === 0 && version === 1) {
const bannedUsers =
localStorage.getItem(keys.users)?.split(',').filter(Boolean) || [];
const data = {};
for (const user of bannedUsers) {
data[user] = 'Unknown';
}
localStorage.setItem(keys.users, JSON.stringify(data));
}
const _users = localStorage.getItem(keys.users);
if (_users === null) {
localStorage.setItem(keys.users, '{}');
}
// This is in case changes are ever breaking
localStorage.setItem(keys.version, version);
// Make a (x) button next to subhn
const button = document.createElement('button');
button.classList.add('hn-filter-block');
let nodes;
const updateNodes = () => {
nodes = [...document.querySelectorAll('.comment-tree .comhead')].filter(
(node) => node !== null && node !== undefined,
);
};
const getUser = (node) =>
node.parentNode.parentNode
.querySelector('.hnuser')
.textContent.toLowerCase();
const getData = () => JSON.parse(localStorage.getItem(keys.users));
const isBanned = (user) => getData()[user] !== undefined;
// Remove all blocked subhns
const hide = () => {
for (let index = 0, length_ = nodes.length; index < length_; index++) {
const node = nodes[index];
const user = getUser(node);
if (
isBanned(user) && // Check if already hidden
!node.parentNode.parentNode.parentNode.parentNode.querySelector(
'.noshow',
)
) {
node.parentNode.parentNode.querySelector('a.togg.clicky').click();
}
}
};
const updateButtons = () => {
for (let index = 0, length_ = nodes.length; index < length_; index++) {
const node = nodes[index];
const a = node.querySelector('.hn-filter-span a');
const user = getUser(node);
const reason = getData()[user];
if (reason !== undefined) {
a.title = reason;
}
a.textContent = isBanned(user) ? 'unban' : 'ban';
}
};
const click = (event) => {
event.preventDefault();
event.stopPropagation();
const { target } = event;
const title =
target.parentNode.parentNode.parentNode.parentNode.querySelector(
'.commtext',
)?.textContent || '';
const key = keys.users;
const user = getUser(target);
const banned = isBanned(user);
const data = getData();
if (
// eslint-disable-next-line no-alert
confirm(
`Are you sure you want to ${banned ? 'unblock' : 'block'} '${user}'?`,
)
) {
if (banned) {
delete data[user];
} else {
data[user] = title;
}
localStorage.setItem(key, JSON.stringify(data));
hide();
updateButtons();
}
};
const addButtons = () => {
for (let index = 0, length_ = nodes.length; index < length_; index++) {
const node = nodes[index];
if (!node.querySelector('.hn-filter-span')) {
const span = document.createElement('span');
span.classList.add('hn-filter-span');
const a = document.createElement('a');
const user = getUser(node);
const reason = getData()[user];
if (reason !== undefined) {
a.title = reason;
}
a.textContent = isBanned(user) ? 'unban' : 'ban';
a.addEventListener('click', click);
span.prepend(' | ');
span.append(a);
node.append(span);
}
}
};
const run = () => {
updateNodes();
hide();
addButtons();
};
run();
})();