AO3: [Wrangling] Collapse and Expand Tag Lists

On tags landing pages, turns long tag lists into a collapsable/expandable accordion, as well as each subtag which in turn has subtags

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

You will need to install an extension such as Tampermonkey to install this script.

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         AO3: [Wrangling] Collapse and Expand Tag Lists
// @namespace    https://greasyfork.org/en/users/906106-escctrl
// @description  On tags landing pages, turns long tag lists into a collapsable/expandable accordion, as well as each subtag which in turn has subtags
// @author       escctrl
// @version      1.1
// @match        *://*.archiveofourown.org/tags/*
// @require      https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js
// @license      MIT
// ==/UserScript==
 
(function($) {
    'use strict';
 
    // we'll show little triangles pointing down/up to show where subtags are hidden
    const arrow_expand = '▽';
    const arrow_collapse = '△';
 
    // don't make accordions if there are less then this many tags in the list:
    const magic_number = 10;
 
    // ***** SUBTAG ACCORDIONS
 
    // find all subtags that have subtags themselves
    const subhassub = $('div.sub li').has('ul.tags.tree');
 
    $(subhassub).each(function() {
        if ($(this).children('ul').children().length < magic_number) { return; }
        // after the link of the tag, add the triangle
        $(this).contents().first().after(' <a class="accordion" style="cursor: pointer;">'+arrow_expand+'</a>');
        // event listener to toggle visibility of the following subtag list
        $(this).children('a.accordion').click(toggleAccordion);
        // initialization: hide all subtag lists
        $(this).children('a.accordion').siblings('ul').hide();
    });
 
    function toggleAccordion() {
        $(this).siblings('ul').toggle("fast", "swing");
 
        var is_collapsed = $('<div/>').html(arrow_collapse);
        var is_expanded = $('<div/>').html(arrow_expand);
 
        if ($(this).html() == is_collapsed.html()) { $(this).html(arrow_expand); }
        else if ($(this).html() == is_expanded.html()) { $(this).html(arrow_collapse); }
    }
 
    // ***** VARIOUS TAG LIST GROUP HEADING ACCORDIONS
 
    // find all subtags that have subtags themselves
    const taggroups = $('div.listbox').not('.child,.parent,.meta').find('h3,h4');
 
    $(taggroups).each(function() {
        if ($(this).siblings('ul.tags').children().length < magic_number) { return; }
        // after the link of the tag, add the triangle
        $(this).contents().first().after(' <a class="accordion" style="cursor: pointer; border-bottom: none; font-size: smaller;">'+arrow_expand+'</a>');
        // event listener to toggle visibility of the following tag list
        $(this).children('a.accordion').click(toggleAccordionGroup);
        // initialization: hide all tag lists
        $(this).siblings('ul.tags').hide();
    });
 
    function toggleAccordionGroup() {
        $(this).parent().siblings('ul').toggle("fast", "swing");
 
        var is_collapsed = $('<div/>').html(arrow_collapse);
        var is_expanded = $('<div/>').html(arrow_expand);
 
        if ($(this).html() == is_collapsed.html()) { $(this).html(arrow_expand); }
        else if ($(this).html() == is_expanded.html()) { $(this).html(arrow_collapse); }
    }
 
 
})(jQuery);