-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathao3 warn for fics with keywords.js
More file actions
227 lines (211 loc) · 7.98 KB
/
ao3 warn for fics with keywords.js
File metadata and controls
227 lines (211 loc) · 7.98 KB
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
// ==UserScript==
// @name ao3 warn for works with keywords
// @namespace http://tampermonkey.net/
// @version 2025-02-08
// @description Adds a warning box on works that contain certain keywords on AO3
// @author bellisk
// @license MIT
// @include https://archiveofourown.org/*
// @icon http://archiveofourown.org/favicon.ico
// @grant GM.setValue
// @grant GM.getValue
// @grant GM.listValues
// @grant GM.deleteValue
// @require https://greasemonkey.github.io/gm4-polyfill/gm4-polyfill.js
// ==/UserScript==
/*jshint esversion: 8 */
// interface
function addMenu() {
if (document.getElementsByClassName("hideKeywordWorks").length > 0) {
return;
}
const dropdown = document.createElement('li');
dropdown.innerHTML = `
<a class="dropdown-toggle" href="/menu/hide-keywords" data-toggle="dropdown" data-target="#">Hide works with keywords</a>
<ul class="menu dropdown-menu" role="menu">
<li id="clearLast" role="menu-item"><a href="#">Remove last-added keyword</a></li>
<li id="clearAll" role="menu-item"><a href="#">Remove all keywords</a></li>
<li id="addKeyword" role="menu-item"><a href="#">Add keyword to hide works for</a></li>
</ul>
`;
dropdown.className = 'dropdown hideKeywordWorks';
const primaryNav = document.getElementsByClassName('primary navigation actions')[0];
primaryNav.appendChild(dropdown);
}
// manage keywords to hide
function addKeyword() {
const keyword = prompt("Enter a keyword to hide works that contain it");
GM.setValue(keyword, keyword);
GM.setValue('last', keyword);
location.reload();
}
async function clearAll() {
const keys = await GM.listValues();
for (let k = 0; k < keys.length; k++) {
await GM.deleteValue(keys[k]);
}
location.reload();
}
async function clearLast() {
const keyword = await GM.getValue('last');
await GM.deleteValue('last');
await GM.deleteValue(keyword);
const keywordsToHide = await GM.listValues();
if (keywordsToHide.length > 0) {
GM.setValue('last', keywordsToHide[keywordsToHide.length - 1]);
}
location.reload();
}
// filter and hide works
const warningBoxCss = `
.warningBox {
box-shadow: 1px;
box-shadow: 1px 1px 5px #aaa;
border: 1px solid #ccc;
clear: right;
padding: 1.286em 0.75em;
position: relative;
overflow: hidden;
text-align: center;
}`
function toggleWorkBlurbDisplay(index) {
const blurbs = document.querySelectorAll('li.blurb');
const warningButton = document.getElementById(`warningButton${index}`);
if (blurbs[index].getElementsByClassName('header')[0].style.display === 'none') {
for (let k = 0; k < blurbs[index].children.length; k++) {
blurbs[index].children[k].style.display = 'block';
}
warningButton.textContent = 'Hide the work summary';
} else {
for (let k = 0; k < blurbs[index].children.length; k++) {
if (blurbs[index].children[k].className !== 'warningBox') {
blurbs[index].children[k].style.display = 'none';
}
}
warningButton.textContent = 'Display the work summary';
}
}
async function filterListOfWorks() {
const works = document.querySelectorAll('li.blurb');
if (works.length === 0) {
return;
}
const keywordsToHide = await GM.listValues();
for (let j = 0; j < works.length; j++) {
const workText = works[j].innerText.toLowerCase().normalize('NFD').replace(/[\u0300-\u036f]/g, "");
const foundKeywords = [];
for (let k = 0; k < keywordsToHide.length; k++) {
if (keywordsToHide[k] === 'last') {
continue;
}
const value = await GM.getValue(keywordsToHide[k]);
const valueText = value.toLowerCase().normalize('NFD').replace(/[\u0300-\u036f]/g, "")
if (workText.includes(valueText)) {
foundKeywords.push(value);
}
}
if (foundKeywords.length > 0) {
const warning = document.createElement('div');
warning.id = `warningBox${j}`;
warning.className = "warningBox";
warning.innerHTML = `
<h3>Blocked keyword(s) found in this work summary</h3>
<details>
<summary>Click to see the keywords found.</summary>
<p>${foundKeywords}</p>
</details>
<button id="warningButton${j}">Display the work summary</button>
`;
GM_addStyle(warningBoxCss);
for (let k = 0; k < works[j].children.length; k++) {
works[j].children[k].style.display = 'none';
}
works[j].prepend(warning);
document.getElementById(`warningButton${j}`).onclick = function () {
toggleWorkBlurbDisplay(j);
};
}
}
}
function toggleWorkDisplay() {
const chapters = document.getElementById('chapters');
const summary = document.getElementsByClassName('summary')[0];
const warningButton = document.getElementById('warningButton');
if (chapters.style.display === 'none') {
chapters.style.display = 'block';
summary.style.display = 'block';
const notes = document.getElementsByClassName('notes');
for (let k = 0; k < notes.length; k++) {
notes[k].style.display = 'block';
}
warningButton.textContent = 'Hide the work';
} else {
chapters.style.display = 'none';
summary.style.display = 'none';
const notes = document.getElementsByClassName('notes');
for (let k = 0; k < notes.length; k++) {
notes[k].style.display = 'none';
}
warningButton.textContent = 'Display the work';
}
}
async function addWarningBoxForSingleWorkPage() {
const chapters = document.getElementById('chapters');
if (chapters === null) {
return;
}
const chapterText = chapters.innerText.toLowerCase().normalize('NFD').replace(/[\u0300-\u036f]/g, "");
const summary = document.getElementsByClassName('summary')[0];
const summaryText = summary.innerText.toLowerCase().normalize('NFD').replace(/[\u0300-\u036f]/g, "")
const keywordsToHide = await GM.listValues();
const foundKeywords = [];
for (let j = 0; j < keywordsToHide.length; j++) {
if (keywordsToHide[j] === 'last') {
continue;
}
const value = await GM.getValue(keywordsToHide[j]);
const valueText = value.normalize("NFD").replace(/[\u0300-\u036f]/g, "").toLowerCase();
if (chapterText.includes(valueText) ||
summaryText.includes(valueText)) {
foundKeywords.push(value);
}
}
if (foundKeywords.length > 0) {
const warning = document.createElement('div');
warning.id = "warningBox";
warning.className = "warningBox";
warning.innerHTML = `
<h3>Blocked keyword(s) found in this work</h3>
<details>
<summary>Click to see the keywords found.</summary>
<p>${foundKeywords}</p>
</details>
<button id="warningButton">Display the work</button>
`;
GM_addStyle(warningBoxCss);
const summary = document.getElementsByClassName('summary')[0];
summary.parentNode.insertBefore(warning, summary);
chapters.style.display = 'none';
summary.style.display = 'none';
const notes = document.getElementsByClassName('notes');
for (let k = 0; k < notes.length; k++) {
notes[k].style.display = 'none';
}
document.getElementById('warningButton').onclick = function () {
toggleWorkDisplay();
};
}
}
// run
addMenu();
document.getElementById('clearLast').onclick = function () {
clearLast();
};
document.getElementById('clearAll').onclick = function () {
clearAll();
};
document.getElementById('addKeyword').onclick = function () {
addKeyword();
};
filterListOfWorks();
addWarningBoxForSingleWorkPage();