Greasy Fork

AO3: Links to Last Chapter and Entire Works

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

目前为 2017-12-24 提交的版本。查看 最新版本

// ==UserScript==
// @name         AO3: Links to Last Chapter and Entire Works
// @namespace    https://greasyfork.org/en/users/163551-vannius
// @version      1.3
// @description  Add links to last chapter and entire works right after title of story.
// @author       Vannius
// @include      http*://archiveofourown.org*
// @exclude      /^https?:\/\/archiveofourown\.org\/works\/\d+/
// @grant          GM_xmlhttpRequest
// @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);
            }
        }
    }
})();