ao3 hide bookmarks

permanently hide bookmarks created by specified users

目前為 2021-08-08 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         ao3 hide bookmarks
// @namespace    https://greasyfork.org/en/users/800073-bellisk
// @version      0.1.1
// @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;}';
document.head.appendChild(blockStyle);

let unblock = document.createElement('li');
unblock.innerHTML = `
    <a>Hide Bookmarks</a>
    <ul class="menu">
        <li id="clearLast"><a>Unblock last</a></li>
        <li id="clearAll"><a>Unblock all</a></li>
        <li id="blockUsername"><a>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);
    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";
    };
}