AO3 Direct Chapter Index and List Button (Entire Website)

A lazy solution to add buttons to navigate directly to the Chapter Index on AO3 work pages, user dashboard, and other users' works, styled to match existing buttons, working across the entire AO3 site. Or something. It's probably a mess, frankly.

目前為 2024-10-17 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         AO3 Direct Chapter Index and List Button (Entire Website)
// @namespace    http://tampermonkey.net/
// @version      2.0
// @description  A lazy solution to add buttons to navigate directly to the Chapter Index on AO3 work pages, user dashboard, and other users' works, styled to match existing buttons, working across the entire AO3 site. Or something. It's probably a mess, frankly.
// @author       stroke6
// @license MIT
// @match        https://archiveofourown.org/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // Function to add the "Go to Chapter Index" button on work pages — it's up there, with the other buttons
    function addButtonOnWorkPage() {
        const navContainer = document.querySelector("#main > ul.work.navigation.actions");
        const workIdMatch = window.location.href.match(/works\/(\d+)/);

        if (navContainer && workIdMatch && workIdMatch[1]) {
            const newButtonItem = document.createElement("li");
            const button = document.createElement("a");

            button.innerHTML = "Go to Chapter Index";
            button.classList.add("button");
            button.style.cursor = "pointer";
            button.href = `https://archiveofourown.org/works/${workIdMatch[1]}/navigate`;

            newButtonItem.appendChild(button);
            navContainer.appendChild(newButtonItem);
        } else {
            console.error('Work ID not found in the URL or navigation container missing.');
        }
    }

    // Function to add a 'Chapters' button on the user dashboard —right next to the 'Add Chapter' button.
    function addButtonOnDashboard() {
        const workElements = document.querySelectorAll('[id^="work_"] ul.actions');

        workElements.forEach(function(workElement) {
            const workId = workElement.closest('[id^="work_"]').id.replace('work_', '');
            if (workId) {
                const smallButton = document.createElement("a");
                smallButton.innerHTML = "Chapters";
                smallButton.classList.add("button");
                smallButton.style.cursor = "pointer";
                smallButton.href = `https://archiveofourown.org/works/${workId}/navigate`;

                workElement.appendChild(smallButton);
            }
        });
    }

    // Function to add "Chapter List" text link to other users' works — and in your own dashoard, because just one wasn't enough
    function addChapterListLinkToOtherUsersWorks() {
        const workElements = document.querySelectorAll('[id^="work_"] dl');

        workElements.forEach(function(workElement) {
            const workId = workElement.closest('[id^="work_"]').id.replace('work_', '');
            if (workId) {
                const chapterListElement = document.createElement("dd");
                const chapterListLink = document.createElement("a");

                chapterListLink.innerHTML = "Chapter List";
                chapterListLink.href = `https://archiveofourown.org/works/${workId}/navigate`;
                chapterListLink.style.cursor = "pointer";

                chapterListElement.appendChild(chapterListLink);
                workElement.appendChild(chapterListElement);
            }
        });
    }

    // Function to handle adding buttons across the entire site — determines which of the three functions above to run.
    function handlePage() {
        const currentURL = window.location.href;

        if (currentURL.includes("/works/")) {
            // For work pages
            addButtonOnWorkPage();
            addChapterListLinkToOtherUsersWorks();
        }

        else if (currentURL.includes("/users/")) {
            // For user-specific pages (dashboard, etc.)
            addButtonOnDashboard();
            addChapterListLinkToOtherUsersWorks();
        } else {
            // For other pages where works might be listed, such as tags — likely a lazy solution
            addChapterListLinkToOtherUsersWorks();
            addButtonOnDashboard();
        }
    }

    // Run the handler when the page loads
    window.onload = function() {
        handlePage();
    };

})();