[AO3] Better Work Buttons

Configure the buttons for works by hiding or rearranging their order.

当前为 2023-10-18 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

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

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         [AO3] Better Work Buttons
// @namespace    https://greasyfork.org/en/users/1138163-dreambones
// @version      0.6.2
// @description  Configure the buttons for works by hiding or rearranging their order.
// @author       DREAMBONES
// @match        http*://archiveofourown.org/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=archiveofourown.org
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    // Configure here! Set to "true" if you want the button hidden, "false" if you want it to be seen.
    var hideSubscribeWhenCompleted = true; // Only show the "Subscribe" button if a work isn't completed.
    var hideTopButtons = {
        "Entire Work": true,
        "Previous Chapter": false,
        "Next Chapter": false,
        "Chapter Index": false,
        "Bookmark": true,
        "Mark as Read": false,
        "Comments": true,
        "Hide Creator's Style": true,
        "Share": true,
        "Subscribe": false,
        "Download": false
    }

    var hideBottomButtons = {
        "Top": true,
        "Kudos": false,
        "Bookmark": false,
        "Mark as Read": false,
        "Comments": false,
    }

    var topButtonPos = ["Entire Work", "Previous Chapter", "Chapter Index", "Next Chapter", "Bookmark", "Mark as Read", "Subscribe", "Comments", "Share", "Download", "Hide Creator's Style"]
    var bottomButtonPos = ["Top", "Kudos", "Bookmark", "Mark as Read", "Comments"]
    //Config ends here!

    var topButtons = {
        "Entire Work": "li.chapter.entire",
        "Previous Chapter": "li.chapter.previous",
        "Next Chapter": "li.chapter.next",
        "Chapter Index": "li[class='chapter']",
        "Bookmark": "li[class='bookmark']",
        "Mark as Read": "li[class='mark']",
        "Comments": "li[class='comments']",
        "Hide Creator's Style": "li[class='style']",
        "Share": "li[class='share']",
        "Subscribe": "li[class='subscribe']",
        "Download": "li[class='download']"
    }

    var bottomButtons = {
        "Top": "li > a[href='#main']",
        "Kudos": "li > form[id='new_kudo']",
        "Bookmark": "li > a[href='#bookmark-form']",
        "Mark as Read": "li > a[href$='mark_as_read']",
        "Comments": "li > a[href^='/comments/']",
    }

    var domainRe = /https?:\/\/archiveofourown\.org\/works\/\d+/i
    if (domainRe.test(document.URL)) {
        var topRow = document.querySelector("ul.work.navigation.actions");
        var bottomRow = document.querySelector("ul[class='actions'][role='navigation']");

        arrangeButtons(topRow, topButtons, topButtonPos);
        arrangeButtons(bottomRow, bottomButtons, bottomButtonPos);

        if (hideSubscribeWhenCompleted) {
            let chapters = document.querySelector("dd.chapters").innerHTML.split("/");
            if (chapters[0] == chapters[1]) {
                let button = topRow.querySelector(topButtons.Subscribe);
                button.remove();
            }
        }

        toggleButtons(topRow, topButtons, hideTopButtons);
        toggleButtons(bottomRow, bottomButtons, hideBottomButtons);
    }

    function arrangeButtons(obj, array, settings) {
        for (let i = 0; i < (settings.length); i++) {
            try {
                let button = obj.querySelector(array[settings[i]]);
                if (button.nodeName != "LI") { button = button.parentElement; }
                obj.insertBefore(button, obj.children[i]);
            }
            catch (TypeError) { null; }
        }
    }

    function toggleButtons(obj, array, settings) {
        for (let key in settings) {
            if (settings.hasOwnProperty(key)) {
                if (settings[key] == true) {
                    try {
                        let button = obj.querySelector(array[key]);
                        if (button.nodeName != "LI") { button = button.parentElement; }
                        button.remove();
                    }
                    catch (TypeError) { null; }
                }
            }
        }
    }
})();