[AO3] Default Filters

Automatically set global filters for AO3 searches.

当前为 2023-07-28 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         [AO3] Default Filters
// @namespace    ao3
// @version      1.0
// @description  Automatically set global filters for AO3 searches.
// @author       DREAMBONES
// @match        https://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: null,
        CompletionStatus: null,

        // 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,
    }

    if (document.URL.startsWith("https://archiveofourown.org/works") || document.URL.startsWith("https://archiveofourown.org/tags")) {
        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");
            minWords.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", "");
                            }
                        }
                    }
                    else {
                        if (option.test(span.innerHTML)) {
                            input.setAttribute("checked", "");
                        }
                    }
                }
            }
        }
    }
})();