-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreddit-filter.js
262 lines (223 loc) · 7.26 KB
/
reddit-filter.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
256
257
258
259
260
261
262
// ==UserScript==
// @author dylanarmstrong
// @description Filter subreddits on r/all
// @grant none
// @match https://*.reddit.com/r/all/*
// @name reddit-filter
// @namespace https://github.com/dylanarmstrong/userscripts/
// @supportURL https://github.com/dylanarmstrong/userscripts/issues
// @updateURL https://raw.githubusercontent.com/dylanarmstrong/userscripts/master/reddit-filter.js
// @version 8
// ==/UserScript==
/**
* Adds a filter at end of tagline that has a popup to filter on subreddit, domain, or user.
* They're stored in the localStorage keys 'filter.*'
*
* TODO: Add filtering of words, not sure on how to do UI for this one
*/
(function() {
'use strict';
const css = `
.reddit-filter-popup {
position: absolute;
z-index: 999;
background-color: #eee;
border: 1px solid #bbb;
padding: 5px 0px 2px 5px;
border-radius: 3px;
height: 20px;
}
.reddit-filter-popup button {
font-size: 10px;
font-weight: bold;
color: white;
background-image: linear-gradient(rgb(123, 184, 80), rgb(117, 168, 73));
cursor: pointer;
margin: 0px 5px 5px 0px;
padding: 1px 6px;
border: 1px solid rgb(68, 68, 68);
border-image: initial;
border-radius: 3px;
}
`;
const style = document.createElement('style');
style.textContent = css;
document.head.insertBefore(style, document.head.firstChild);
// Store the version # in localStorage
// this will be useful for possible breaking changes
// How many ? can you fit on one line?
const version = typeof GM_info === 'object' ? GM_info?.script?.version ?? 0 : 0;
const keys = {
domains: 'filter.domains',
subreddits: 'filter.subreddits',
users: 'filter.users',
words: 'filter.words',
version: 'filter.version'
};
// Make sure all keys are initialized
Object.keys(keys).forEach((key) => {
const value = keys[key];
const item = localStorage.getItem(value);
if (item === null) {
localStorage.setItem(value, '');
}
});
// This is in case changes are ever breaking
localStorage.setItem(keys.version, version);
// Make a (x) button next to subreddit
let button = document.createElement('button');
button.classList.add('reddit-filter-block');
let nodes = null;
const updateNodes = () => {
nodes = Array.from(document.getElementsByClassName('entry'));
nodes = nodes.filter(node => node !== null && typeof node !== 'undefined');
};
// Remove all blocked subreddits
const hide = () => {
const subreddits = localStorage.getItem(keys.subreddits).split(',');
const users = localStorage.getItem(keys.users).split(',');
const domains = localStorage.getItem(keys.domains).split(',');
const words = localStorage.getItem(keys.words).split(',');
const removed = [];
for (let i = 0, len = nodes.length; i < len; i++) {
const node = nodes[i];
// Remove r/ from the subreddit
let [ subreddit ] = node.getElementsByClassName('subreddit')
, [ user ] = node.getElementsByClassName('author')
, [ domain ] = node.getElementsByClassName('domain');
if (subreddit) {
subreddit = subreddit.textContent.slice(2).toLowerCase();
}
if (user) {
user = user.textContent.toLowerCase();
if (user.slice(0, 2) === 'u/') {
user = user.slice(2);
}
}
if (domain) {
[ domain ] = domain.getElementsByTagName('a');
if (domain) {
domain = domain.textContent.toLowerCase();
}
}
if (subreddits.includes(subreddit) || users.includes(user) || domains.includes(domain)) {
const p = node.parentNode;
if (p && p.parentNode) {
p.parentNode.removeChild(p);
}
// Remove from nodes
removed.push(i);
}
}
// Do not act on removed nodes anymore
nodes = nodes.filter((node, index) => {
return !removed.includes(index);
});
};
const click = (key, e) => {
const { target } = e;
let selector = ''
, type = '';
if (key === keys.subreddits) {
selector = '.subreddit';
type = 'r/';
} else if (key === keys.users) {
selector = '.author';
type = 'u/';
} else if (key === keys.domains) {
selector = '.domain a';
type = 'domain ';
} else {
return;
}
let block =
target.parentNode.parentNode.parentNode.parentNode.querySelector(selector).textContent;
block = block.replace(/^((r|u)\/){1}/, '').toLowerCase();
if (confirm(`Are you sure you want to block ${type}${block}?`)) {
let blocked = localStorage.getItem(key);
// Already in here, how'd the user block it twice?
if (blocked.split(',').includes(block)) {
return;
}
blocked += `,${block}`;
blocked = blocked.replace(/^,/, '');
localStorage.setItem(key, blocked);
hide();
}
};
let hasPopups = false;
const closePopups = (e = null) => {
if (hasPopups) {
if (!e || !e.target.classList.contains('reddit-filter-popup')) {
const popups = document.getElementsByClassName('reddit-filter-popup');
for (let i = 0, len = popups.length; i < len; i++) {
const p = popups[i];
if (p && typeof p !== 'undefined' && p.parentNode) {
p.parentNode.removeChild(p);
}
}
hasPopups = false;
}
}
};
// Hide any popups on click anywhere on page
document.body.addEventListener('click', closePopups);
const popup = (ul, x, y) => {
hasPopups = true;
const div = document.createElement('div');
div.classList.add('reddit-filter-popup');
div.style.left = `${x}px`;
div.style.top = `${y}px`;
let button = document.createElement('button');
button.textContent = 'subreddit';
button.addEventListener('click', click.bind(this, keys.subreddits));
div.appendChild(button);
button = document.createElement('button');
button.textContent = 'user';
button.addEventListener('click', click.bind(this, keys.users));
div.appendChild(button);
button = document.createElement('button');
button.textContent = 'domain';
button.addEventListener('click', click.bind(this, keys.domains));
div.appendChild(button);
ul.appendChild(div);
};
const aClick = (e) => {
e.preventDefault();
e.stopPropagation();
const {
pageX,
pageY,
target
} = e;
const ul = target.parentNode.parentNode;
closePopups();
popup(ul, pageX, pageY);
};
const addButtons = () => {
for (let i = 0, len = nodes.length; i < len; i++) {
const node = nodes[i];
const buttons = node.querySelector('ul.flat-list.buttons');
if (buttons) {
if (!buttons.querySelector('.reddit-filter-li')) {
const li = document.createElement('li');
li.classList.add('reddit-filter-li');
const a = document.createElement('a');
a.textContent = 'filter';
a.href = '';
a.addEventListener('click', aClick);
li.appendChild(a);
buttons.appendChild(li);
}
}
}
};
const run = () => {
updateNodes();
hide();
addButtons();
};
// Infinite loading scroll script has loaded more items
document.addEventListener('reddit-load', run);
run();
})();