AO3: Links for Entire Works

Add links to next and previous chapter to Entire Works of AO3.

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         AO3: Links for Entire Works
// @namespace    https://greasyfork.org/en/users/163551-vannius
// @version      1.5
// @license      MIT
// @description  Add links to next and previous chapter to Entire Works of AO3.
// @author       Vannius
// @match        https://archiveofourown.org/works/*view_full_work=true*
// @grant        none
// ==/UserScript==

(function() {
    // get id="chapter-[d]+" tags
    let chapters = [];
    let index = 1;
    while (true) {
        const chapter = document.getElementById('chapter-' + index);
        if (chapter) {
            chapters.push(chapter);
            index += 1;
        } else {
            break;
        }
    }

    for (let i = 0; i < chapters.length; i++) {
        // Display add links to right of each chapter title.
        const rightSpan = document.createElement('span');
        rightSpan.style.float = 'right';

        // Make a link to current chapter
        if (i === 0) {
            const currentChapter = document.createElement('a');
            currentChapter.title = "Current chapter";
            currentChapter.href = "#chapter-" + (i + 1);
            currentChapter.appendChild(document.createTextNode('◆'));
            rightSpan.appendChild(currentChapter);
        }
        // Make a link to prev chapter
        if (i !== 0) {
            const prevChapter = document.createElement('a');
            prevChapter.title = "Previous chapter";
            prevChapter.href = "#chapter-" + i;
            prevChapter.appendChild(document.createTextNode('▲'));
            rightSpan.appendChild(prevChapter);
        }
        // Make a link to next chapter
        if (i != chapters.length - 1) {
            const nextChapter = document.createElement('a');
            nextChapter.title = "Next chapter";
            nextChapter.href = "#chapter-" + (i + 2);
            nextChapter.appendChild(document.createTextNode('▼'));
            rightSpan.appendChild(nextChapter);
        }
        // Make a link to current chapter
        if (i == chapters.length - 1) {
            const currentChapter = document.createElement('a');
            currentChapter.title = "Current chapter";
            currentChapter.href = "#chapter-" + (i + 1);
            currentChapter.appendChild(document.createTextNode('◆'));
            rightSpan.appendChild(currentChapter);
        }

        // Add links
        chapters[i].querySelector(".chapter .title").appendChild(rightSpan);
    }
})();