[AO3] Default Filters

Automatically set global filters for AO3 searches.

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         [AO3] Default Filters
// @namespace    https://greasyfork.org/en/users/1138163-dreambones
// @version      1.2.2
// @description  Automatically set global filters for AO3 searches.
// @author       DREAMBONES
// @match        http*://archiveofourown.org/*
// @icon         data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    // Config is case-insensitive. Enclose all config parameters in quotes unless stated otherwise.
    var config = {
        Sort: "",
        Language: "",

        IncludeRatings: [],
        ExcludeRatings: [],

        IncludeWarnings: [],
        ExcludeWarnings: [],

        IncludeCategories: [],
        ExcludeCategories: [],

        IncludeFandoms: [],
        ExcludeFandoms: [],

        IncludeCharacters: [],
        ExcludeCharacters: [],

        IncludeRelationships: [],
        ExcludeRelationships: [],

        IncludeAdditionalTags: [],
        ExcludeAdditionalTags: [],

        Crossovers: "",
        CompletionStatus: "",

        // Set word counts to "null" if you don't want a threshold. Type a number (NOT enclosed in quotes) to set an upper/lower threshold.
        MinWordCount: null,
        MaxWordCount: null,
    }

    var domainRe = /https?:\/\/archiveofourown\.org\/(works|tags).*/i
    if (domainRe.test(document.URL)) {
        selectDropdownOption("#work_search_sort_column", config.Sort);
        selectDropdownOption("#work_search_language_id", config.Language);

        selectListOption("#include_rating_tags > ul", config.IncludeRatings);
        selectListOption("#exclude_rating_tags > ul", config.ExcludeRatings);
        selectListOption("#include_archive_warning_tags > ul", config.IncludeWarnings);
        selectListOption("#exclude_archive_warning_tags > ul", config.ExcludeWarnings);
        selectListOption("#include_category_tags > ul", config.IncludeCategories);
        selectListOption("#exclude_category_tags > ul", config.ExcludeCategories);
        selectListOption("#include_fandom_tags > ul", config.IncludeFandoms);
        selectListOption("#exclude_fandom_tags > ul", config.ExcludeFandoms);
        selectListOption("#include_character_tags > ul", config.IncludeCharacters);
        selectListOption("#exclude_character_tags > ul", config.ExcludeCharacters);
        selectListOption("#include_relationship_tags > ul", config.IncludeRelationships);
        selectListOption("#exclude_relationship_tags > ul", config.ExcludeRelationships);
        selectListOption("#include_freeform_tags > ul", config.IncludeAdditionalTags);
        selectListOption("#exclude_freeform_tags > ul", config.ExcludeAdditionalTags);

        selectListOption("#work_crossover > ul", config.Crossovers);
        selectListOption("#work_complete > ul", config.CompletionStatus);

        if (config.MinWordCount != null) {
            var minWords = document.querySelector("#work_search_words_from");
            minWords.setAttribute("value", config.MinWordCount);
        }

        if (config.MaxWordCount != null) {
            var maxWords = document.querySelector("#work_search_words_to");
            maxWords.setAttribute("value", config.MaxWordCount);
        }

        function selectDropdownOption(query, option) {
            if (option.length) {
                option = new RegExp(option, "i")
                var list = document.querySelector(query);
                for (var i = 0; i < (list.length); i++) {
                    if (option.test(list[i].innerHTML)) {
                        list[i].setAttribute("selected", "");
                        break;
                    }
                }
            }
        }

        function selectListOption(query, option) {
            if (option.length) {
                option = new RegExp(option, "i")
                var list = document.querySelector(query).children;
                for (var i = 0; i < (list.length); i++) {
                    var span = list[i].querySelectorAll("span")[1];
                    var input = list[i].querySelector("input");
                    if (Array.isArray(option)) {
                        for (var ii = 0; ii < (option.length); ii++) {
                            if (option.test(span.innerHTML)) {
                                input.setAttribute("checked", "");
                                let listControls = document.querySelector(`button[aria-controls="${document.querySelector(query).parentNode.id}"]`);
                                let listExpanded = listControls.getAttribute("aria-expanded");
                                if (!listExpanded) { listControls.click(); }
                            }
                        }
                    }
                    else {
                        if (option.test(span.innerHTML)) {
                            input.setAttribute("checked", "");
                            let listControls = document.querySelector(`button[aria-controls="${document.querySelector(query).parentNode.id}"]`);
                            let listExpanded = listControls.getAttribute("aria-expanded");
                            if (!listExpanded) { listControls.click(); }
                        }
                    }
                }
            }
        }
    }
})();