-
Notifications
You must be signed in to change notification settings - Fork 0
/
ao3-bookmarker-block.js
109 lines (93 loc) · 3.77 KB
/
ao3-bookmarker-block.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
// ==UserScript==
// @name ao3 hide bookmarks
// @namespace https://greasyfork.org/en/users/800073-bellisk
// @version 0.1.2
// @description permanently hide bookmarks created by specified users
// @author bellisk
// @include http://archiveofourown.org/works/*/bookmarks*
// @include https://archiveofourown.org/works/*/bookmarks*
// @grant GM.setValue
// @grant GM.getValue
// @grant GM.listValues
// @grant GM.deleteValue
// ==/UserScript==
const bookmarks = document.querySelectorAll('li.short');
// interface
let headerModule, blockLink, blockStyle;
for (let i=0;i<bookmarks.length;i++) {
headerModule = bookmarks[i].getElementsByClassName('header module')[0];
blockLink = document.createElement('div');
blockLink.className = 'bookmarkblock';
blockLink.innerHTML = '<a class="blockThisBookmarker">block this bookmarker</a>';
headerModule.parentNode.insertBefore(blockLink, headerModule.nextSibling);
}
blockStyle = document.createElement('style');
blockStyle.innerHTML = 'div.bookmarkblock {text-align: right; font-family:monospace; margin-bottom: .375em; cursor: pointer;}';
document.head.appendChild(blockStyle);
let unblock = document.createElement('li');
unblock.innerHTML = `
<a class="dropdown-toggle" href="/menu/hide-bookmarks" data-toggle="dropdown" data-target="#">Hide Bookmarks</a>
<ul class="menu dropdown-menu" role="menu">
<li id="clearLast" role="menu-item"><a href="#">Unblock last</a></li>
<li id="clearAll" role="menu-item"><a href="#">Unblock all</a></li>
<li id="blockUsername" role="menu-item"><a href="#">Block username</a></li>
</ul>`;
unblock.className = 'dropdown bookmarkblock';
let search = document.getElementsByClassName('primary navigation actions')[0].getElementsByClassName('search')[0];
search.parentNode.insertBefore(unblock, search);
// block bookmarks
function getBookmarkerName(liTag) {
const byline = liTag.getElementsByClassName('byline')[0];
const bookmarker = byline.getElementsByTagName('a')[0];
return bookmarker.text;
}
function blockThisBookmarker(bookmark) {
const bookmarker = getBookmarkerName(bookmark);
GM.setValue(bookmarker, bookmarker);
GM.setValue('last', bookmarker);
}
async function blockSelected(bookmarks) {
const blocked = await GM.listValues();
for (let j=0; j<bookmarks.length; j++) {
const bookmarker = getBookmarkerName(bookmarks[j]);
if (blocked.find(function(id){return id === bookmarker;})) {
bookmarks[j].style.display = 'none';
}
}
}
function blockUsername() {
const username = prompt("Enter a username to hide all bookmarks from");
GM.setValue(username, username);
GM.setValue('last', username);
location.reload();
}
// unblock bookmarks
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 username = await GM.getValue('last');
await GM.deleteValue('last');
await GM.deleteValue(username);
const blocked = await GM.listValues();
GM.setValue('last', blocked[blocked.length -1]);
location.reload();
}
// run
blockSelected(bookmarks);
document.getElementById('clearLast').onclick = function() {clearLast();};
document.getElementById('clearAll').onclick = function() {clearAll();};
document.getElementById('blockUsername').onclick = function() {blockUsername();};
const blockLinks = document.getElementsByClassName('blockThisBookmarker');
for (let k=0; k<blockLinks.length; k++) {
let bLink = blockLinks[k];
bLink.onclick = function() {
let bookmark = this.parentNode.parentNode;
blockThisBookmarker(bookmark)
bookmark.style.display = "none";
};
}