Greasy Fork

AO3: Tag Hider

Add hide/show tags button to browsing pages, hide/show data button to viewing pages on AO3.

目前为 2018-06-19 提交的版本。查看 最新版本

// ==UserScript==
// @name        AO3: Tag Hider
// @namespace    https://greasyfork.org/en/users/163551-vannius
// @version     1.5
// @description Add hide/show tags button to browsing pages, hide/show data button to viewing pages on AO3.
// @author       Vannius
// @match        http*://archiveofourown.org/*
// @grant       GM_addStyle
// ==/UserScript==

(function() {
    // Config
    const MAX_TAGS_ON_BROWSING_PAGE = 15;
    const HIDE_WORK_DATA_AUTOMATICALLY_ON_VIEW_PAGE = true;


    if (/archiveofourown\.org\/(collections\/[^\/]+\/)?works\/[0-9]+/.test(window.location.href)) {
        //viewing page
        const dataTag = document.getElementsByClassName('work meta group')[0];

        // Make Show Data/Hide Data button
        const btn = document.createElement('a');
        btn.appendChild(document.createTextNode(''));

        // Hide or Show data
        if (HIDE_WORK_DATA_AUTOMATICALLY_ON_VIEW_PAGE) {
            dataTag.style.display = 'none';
            btn.textContent = 'Show Data'
        }else{
            dataTag.style.display = 'block';
            btn.textContent = 'Hide Data'
        }

        // Add click event
        btn.addEventListener('click', function() {
            if (dataTag.style.display == 'none') {
                dataTag.style.display = 'block';
                btn.textContent = 'Hide Data'
            }else{
                dataTag.style.display = 'none';
                btn.textContent = 'Show Data'
            }
        });

        // Add Show Data/Hide Data button to menu
        const liTag = document.createElement('li');
        liTag.appendChild(btn);

        const fragment = document.createDocumentFragment();
        fragment.appendChild(document.createTextNode('    '));
        fragment.appendChild(liTag);
        fragment.appendChild(document.createTextNode('\n\n'));

        const menu = document.getElementsByClassName('work navigation actions')[0];
        menu.appendChild(fragment);
    } else {
        // browsing page
        const articles = document.getElementById('main').getElementsByClassName('blurb');

        // Add style for Show Tags/Hide Tags Button.
        GM_addStyle("button.taghider {padding: 0.5px 2px;}");
        GM_addStyle("button.taghider:hover {color: #900; box-shadow: inset 2px 2px 2px #bbb;}");
        GM_addStyle("button.taghider:focus {color: #900; box-shadow: inset 2px 2px 2px #bbb;}");
        GM_addStyle("button.taghider:active {background: #ccc; box-shadow: inset 1px 1px 3px #333;}");

        // Add Show Tags/Hide Data Tags to each article.
        for (let article of articles) {
            const ao3tag = article.getElementsByClassName('tags commas')[0];

            // Make Show Tags/Hide Data Tags
            const btn = document.createElement('button');
            btn.type = 'button';
            btn.className = 'taghider';
            btn.appendChild(document.createTextNode(''));

            // Hide or Show tags
            if (ao3tag.children.length > MAX_TAGS_ON_BROWSING_PAGE) {
                ao3tag.style.display = 'none';
                btn.textContent = 'Show Tags';
            }else{
                ao3tag.style.display = 'block';
                btn.textContent = 'Hide Tags';
            }

            // Add click event
            btn.addEventListener('click', function() {
                if (ao3tag.style.display == 'none') {
                    ao3tag.style.display = 'block';
                    btn.textContent = 'Hide Tags';
                }else{
                    ao3tag.style.display = 'none';
                    btn.textContent = 'Show Tags';
                }
            });

            // Add Show Data/Hide Data button to right after fandoms.
            const fandomTag = article.getElementsByClassName('header module')[0].children[1];
            const fragment = document.createDocumentFragment();
            fragment.appendChild(document.createTextNode(' '));
            fragment.appendChild(btn);
            fandomTag.insertBefore(fragment, fandomTag.lastChild);
        }
    }
})();