Greasy Fork

Greasy Fork is available in English.

AO3: Links to Last Chapter and Entire Works

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

当前为 2017-12-25 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         AO3: Links to Last Chapter and Entire Works
// @namespace    http://greasyfork.icu/en/users/163551-vannius
// @version      1.5
// @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 chpater 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) {
            // Make link to last chpater
            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);
            }
        }
    }
})();