-
Notifications
You must be signed in to change notification settings - Fork 0
/
4chan-auto-watcher.user.js
82 lines (64 loc) · 2.48 KB
/
4chan-auto-watcher.user.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
// ==UserScript==
// @name 4chan-auto-watcher
// @namespace github.com/diegostafa/userscripts
// @match https://boards.4chan.org/*/catalog
// @match https://boards.4channel.org/*/catalog
// @version 3
// @author Diego <[email protected]> (github.com/diegostafa)
// @description automatically watch threads matching a pattern
// @run-at document-end
// ==/UserScript==
const currentBoard = window.location.pathname.split("/").filter((s) => s !== "")[0];
let boardsAndFilters = [
["", /^[^a-z]*$/],
];
const buildUi = () => {
const createButton = (text) => {
let button = document.createElement("div");
button.innerHTML = text;
button.style.backgroundColor = "#444444";
button.style.color = "white";
button.style.textAlign = "center";
button.style.border = "none";
button.style.padding = "2px";
button.style.cursor = "pointer";
return button;
};
const createAutowatchButton = () => {
let button = createButton("autowatch");
button.addEventListener("click", () => { autoWatch(); });
return button;
};
const createConfigButton = () => {
let button = createButton("config");
button.addEventListener("click", () => {
var win = window.open("", "Autowatch config", "toolbar=no");
win.document.body.innerHTML = `TODO`;
});
return button;
};
let threadWatcher = document.getElementById("threadWatcher");
let btnContainer = document.createElement("div");
btnContainer.style.display = "grid";
btnContainer.style.gridTemplateColumns = "100%";
btnContainer.style.gridGap = "2px";
//btnContainer.appendChild(createConfigButton());
btnContainer.appendChild(createAutowatchButton());
threadWatcher.appendChild(btnContainer);
};
const isThreadMatching = (filters) => (thread) => {
const teaser = thread.querySelector('.teaser');
if (!teaser) return false;
return filters.reduce(
(acc, [board, filter]) =>
acc = acc || (filter.test(teaser.textContent) && (board === "" || currentBoard === board)), false);
};
const autoWatch = () => {
let threads = Array.from(document.querySelectorAll('#threads .thread'));
threads
.filter(isThreadMatching(boardsAndFilters))
.map((thread) => thread.querySelector('span.watchIcon'))
.filter((watchIcon) => watchIcon !== null)
.forEach((watchIcon) => watchIcon.click());
};
buildUi();