AO3: Links to Last Chapter and Entire Works

Add links to last chapter and entire works right after title of story.

目前為 2018-01-26 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 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 Last Chapter and Entire Works
// @namespace    https://greasyfork.org/en/users/163551-vannius
// @version      1.6
// @description  Add links to last chapter and entire works right after title of story.
// @author       Vannius
// @match        http*://archiveofourown.org/*
// @exclude      /^https?:\/\/archiveofourown\.org\/works\/\d+/
// @grant        GM_xmlhttpRequest
// @connect      archiveofourown.org
// @grant        GM_openInTab
// ==/UserScript==

(function() {
    // Config
    // Open last chapter in new tab.
    const OPEN_IN_NEW_TAB = true;

    // Functions
    // Make and return a link button to last chapter.
    function makeLastLink(url) {
        const lastLink = document.createElement('a');

        // Add click event
        lastLink.addEventListener('click', function(e) {
            e.preventDefault();

            // Get url of last chapter
            GM_xmlhttpRequest({
                method: "GET",
                url: url,
                onload: response => {
                    const doc = document.implementation.createHTMLDocument('myBody');
                    doc.documentElement.innerHTML = response.responseText; // Parse body
                    const selectedTag = doc.getElementById('selected_id');

                    // If selectedTag === null, there isn't consent to veiw adult content in cookie.
                    const lastHref = selectedTag === null ?
                          url : url + '/chapters/' + selectedTag.children[selectedTag.children.length - 1].value;

                    // Move page
                    if (OPEN_IN_NEW_TAB) {
                        GM_openInTab(lastHref);
                    } else {
                        window.location.href = lastHref;
                    }
                }
            });
        });
        return lastLink;
    }

    // Main
    const articles = document.getElementsByClassName('blurb');
    for (let article of articles) {
        // Scrape each article
        const titleTag = article.getElementsByClassName('heading')[0].firstChild.nextElementSibling;
        const series = (titleTag.href.indexOf("/series/") != -1) ? true : false;

        // When article isn't series page
        if (!series) {
            // Get number of chapters
            const chapters =
                  article.getElementsByTagName('dl')[0].getElementsByClassName('chapters')[1].textContent.split("/");

            // When chapter number isn't one
            if (chapters[0] != '1') {
                // Make link to entire contents
                const entireLink = document.createElement('a');
                entireLink.href = titleTag.href + "?view_full_work=true";
                entireLink.title = "Entire Contents";
                entireLink.appendChild(document.createTextNode('E'));

                // Add link to Entire contents right after title of story and adjust placement.
                titleTag.parentElement.insertBefore(entireLink, titleTag.nextSibling);
                titleTag.parentElement.insertBefore(document.createTextNode(' '), entireLink);

                // Make link button to last chapter.
                const lastLink = makeLastLink(titleTag.href);
                lastLink.title = "Click Event: open last chapter";
                lastLink.appendChild(document.createTextNode('L'));

                // Add link button to last chapter right after title of story and adjust placement.
                titleTag.parentElement.insertBefore(lastLink, titleTag.nextSibling);
                titleTag.parentElement.insertBefore(document.createTextNode(' '), lastLink);
            }
        }
    }
})();