Sort character lists alphabetically

The character lists are not sorted alphabetically, making finding characters in the more difficult than it should be. This script sorts them across the page.

当前为 2023-08-02 提交的版本,查看 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Sort character lists alphabetically
// @namespace    http://f-list.net/c/Grimokk
// @version      1.1
// @description  The character lists are not sorted alphabetically, making finding characters in the more difficult than it should be. This script sorts them across the page.
// @author       Grimokk
// @match        https://www.f-list.net/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=f-list.net
// @grant        none
// ==/UserScript==


(function() {
    'use strict';

    function _gmSortCharacterList(target) {

        //https://stackoverflow.com/questions/13033472/ordering-a-select-with-javascript-without-jquery
        var sorted = Array.prototype.slice.call(target.options).sort(function(a, b) {
            if(a.label < b.label) return -1;
            if(a.label > b.label) return 1;
            return 0;
        });

        for(var i = 0; i < sorted.length; i++) {
            target.add(sorted[i]);
        }

    }

    // get element used to fill in character lists on note page
    var _gmCharListNotes = document.getElementById("NoteVarCharacters");
    var _gmCharListOther = document.getElementsByName("character_id");
    var _gmCharListChatLogs = document.getElementById("character");

    // only try sorting this if the element actually exists
    if(_gmCharListNotes) {
        _gmSortCharacterList(_gmCharListNotes);
    }

    // only try sorting this if the element actually exists and is a select element
    if(_gmCharListOther && _gmCharListOther.length > 0 && _gmCharListOther[0].type === "select-one") {
        _gmSortCharacterList(_gmCharListOther[0]);
    }

    // only try sorting this if we're on the chat3 page and it is a select element
    if(window.location.href.includes("chat3") && _gmCharListChatLogs && _gmCharListChatLogs.type === "select-one") {
        _gmSortCharacterList(_gmCharListChatLogs);
    }

})();