AO3: Links to First and Last Chapter

Add links to first and last chapter to AO3.

目前為 2017-12-18 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name          AO3: Links to First and Last Chapter
// @namespace    https://greasyfork.org/en/users/163551-vannius
// @version      1.0
// @description  Add links to first and last chapter to AO3.
// @author       Vannius
// @match        http*://archiveofourown.org/works/*
// @exclude      http*://archiveofourown.org/works/*view_full_work=true
// @grant        none
// ==/UserScript==

(function() {
    // selectedTag exists only when there are several chapters.
    const selectedTag = document.getElementById('selected_id');
    if (!selectedTag) return;

    // Make hrefs to first and last chapter.
    const firstHref = window.location.href.split('/').slice(0, 6).join('/') + '/' + selectedTag.children[0].value;
    const lastHref = window.location.href.split('/').slice(0, 6).join('/') + '/' + selectedTag.children[selectedTag.children.length - 1].value;

    // Search location to add new link tags.
    const chapterIndexTag = document.getElementById('chapter_index').parentElement;
    const prevTags = chapterIndexTag.parentElement.getElementsByClassName('previous');
    const nextTags = chapterIndexTag.parentElement.getElementsByClassName('next');
    const navigationTags = document.getElementById('feedback').getElementsByClassName('actions');

    // When '← Previous Chapter' exists
    if (prevTags.length){
        // When '← Previous Chapter' != first chapter
        if(prevTags[0].firstChild.href != firstHref){
            // Make a link tag to frist chapter
            const firstLiTag = document.createElement('li');
            firstLiTag.className = 'chapter first';
            const firstATag = document.createElement('a');
            firstATag.href = firstHref;
            firstATag.appendChild(document.createTextNode('«'));
            firstLiTag.appendChild(firstATag);

            // Add the link tag to page's top
            prevTags[0].parentElement.insertBefore(firstLiTag.cloneNode(true), prevTags[0]);
            // Add the link tag to page's end
            for (let i=0; i<navigationTags[0].children.length; i++){
                if (navigationTags[0].children[i].firstChild.textContent.search(/Previous/) != -1){
                    navigationTags[0].insertBefore(firstLiTag, navigationTags[0].children[i]);
                    break;
                }
            }
        }
    }

    // When 'Next Chapter →' exists
    if (nextTags.length){
        // When 'Next Chapter →' isn't last chapter
        if(nextTags[0].firstChild.href != lastHref){
            // Make a link tag to last chapter
            const lastLiTag = document.createElement('li');
            lastLiTag.className = 'chapter last';
            const lastATag = document.createElement('a');
            lastATag.href = lastHref;
            lastATag.appendChild(document.createTextNode('»'));
            lastLiTag.appendChild(lastATag);

            // Add the link tag to page's top
            nextTags[0].parentElement.insertBefore(lastLiTag.cloneNode(true), nextTags[0].nextElementSibling);
            // Add the link tag to page's end
            for (let i=0; i<navigationTags[0].children.length - 1; i++){
                if (navigationTags[0].children[i].firstChild.textContent.search(/Next/) != -1){
                    navigationTags[0].insertBefore(lastLiTag, navigationTags[0].children[i + 1]);
                    break;
                }
            }
        }
    }
})();