AO3: [Wrangling] Peek fandoms used by tags in the bins!

Will show you what fandoms a tag is used in from unwrangled bins!

当前为 2022-06-21 提交的版本,查看 最新版本

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         AO3: [Wrangling] Peek fandoms used by tags in the bins!
// @description  Will show you what fandoms a tag is used in from unwrangled bins!
// @version      1.0.0

// @author       owlwinter
// @namespace    N/A
// @license      MIT license

// @match        *://*.archiveofourown.org/tags/*/wrangle?*status=unwrangled*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    // the return value of document.querySelectorAll is technically a "NodeList", which can be indexed like an array, but
    // doesn't have helpful functions like .map() or .forEach(). So this is a simple helper function to turn a NodeList
    // (or any other array-like object (indexed by integers starting at zero)) into an array
    const array = a => Array.prototype.slice.call(a, 0)

    // Called for each tag in the search results list
    // `url` is the url of the edit tag page
    // `result` is the span with the "Loading..." in it where we will put the results of our request
    const get_fandoms = function get_fandoms(url, result) {
        const xhr = new XMLHttpRequest();
        xhr.onreadystatechange = function xhr_onreadystatechange() {
            if (xhr.readyState == xhr.DONE ) {
                if (xhr.status == 200) {
                    //Returns all of the fandoms used on a tag
                    //Does this by peeking at all the "suggested fandoms" on the tag's edit form
                    const fandoms = []
                    var test2 = array(xhr.responseXML.documentElement.getElementsByClassName("tags commas"))
                    for (const check of test2) {
                        const name = check.innerText.trim()
                        fandoms.push(name)
                    }
                    result.innerText = "\n" + fandoms;

                } else if (xhr.status == 429) {
                    // ~ao3jail
                    result.innerText = "\nRate limited. Sorry :("
                } else {
                    result.innerText = "\nUnexpected error, check the console"
                    console.log(xhr)
                }
            }
        }
        xhr.open("GET", url)
        xhr.responseType = "document"
        xhr.send()
    }

    //Don't want to error when there's no tags in the bins
    if (document.querySelector("#wrangulator") != null) {
        //Adds new button at the beginning and end of the tag table
        const wranglebuttons = document.querySelector("#wrangulator").getElementsByClassName("submit actions")
        for (let a of wranglebuttons) {
            const peekfandombutton = document.createElement("button");
            peekfandombutton.name = "Peek fandoms button"
            peekfandombutton.style.textAlign = "center"
            peekfandombutton.textContent = "Peek fandoms";
            peekfandombutton.style.marginRight = "5px"
            peekfandombutton.style.paddingRight = "5px"
            //When either button is clicked
            peekfandombutton.addEventListener("click", (e) => {
                e.preventDefault()
                //Remove both peek fandoms buttons
                a.removeChild(peekfandombutton)
                const allpeekfandombuttons = document.getElementsByName("Peek fandoms button");
                allpeekfandombuttons.forEach(b => {
                    b.remove();
                });

                //For each row of the table
                const actionsbuttons = document.getElementById("wrangulator").querySelectorAll("td > ul.actions")
                for (const buttonset of actionsbuttons) {
                    const span = buttonset.parentElement.parentElement.firstElementChild.getElementsByTagName("label")[0]

                    // this is the element that will hold the result when the request finishes
                    const loading = document.createElement("span");

                    //Before our request finishes, shows a loading text so user knows something is happening
                    loading.innerText = "\nLoading fandoms..."
                    loading.style.color = "#00C"
                    loading.style.fontStyle = "italic"
                    span.appendChild(loading);

                    // trigger xhr (asynchronous)
                    get_fandoms("https://archiveofourown.org" + buttonset.querySelector('a').getAttribute('href'), loading);
                }
            })
            //What actually adds the buttons
            a.prepend(peekfandombutton)
        }
    }
    // Your code here...
})();