AO3: Go to Comments

When clicking Comments button, jump to Comments section automatically.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         AO3: Go to Comments
// @namespace    https://greasyfork.org/en/users/163551-vannius
// @version      1.3
// @license      MIT
// @description  When clicking Comments button, jump to Comments section automatically.
// @author       Vannius
// @match        https://archiveofourown.org/*
// @grant        none
// ==/UserScript==

(function () {
    if (/archiveofourown\.org\/(collections\/[^/]+\/)?works\/[0-9]+/.test(window.location.href)) {
        // Get Comments/Hide Comments tags
        const commentTagTop = document.getElementById('show_comments_link_top');
        const commentTagBottom = document.getElementById('show_comments_link');
        // If there is no comment, commentTagBottom is undefined.
        // And there is no need to add go to comments feature.
        if (!commentTagBottom) return;

        const mutationObserver = new MutationObserver(mutations => {
            const commentFlag = mutations
                .map(x => x.addedNodes.length)
                .reduce((p, x) => p + x);

            if (commentFlag) {
                const sleep = msec => new Promise(resolve => setTimeout(resolve, msec));

                (async () => {
                    await sleep(1000);
                    window.location.href = "#comments_placeholder";
                })();
            }
            mutationObserver.disconnect();
        });

        // Add click event to go to #comments_placeholder
        const target = document.getElementById('comments_placeholder');
        const options = { childList: true };
        for (let commentTag of [commentTagTop, commentTagBottom]) {
            commentTag.addEventListener('click', () => {
                mutationObserver.observe(target, options);
            });
        }
    }
})();