Greasy Fork 还支持 简体中文。

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-08 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name Sort character lists alphabetically
  3. // @namespace http://f-list.net/c/Grimokk
  4. // @version 1.2
  5. // @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.
  6. // @author Grimokk
  7. // @match https://www.f-list.net/*
  8. // @icon https://www.google.com/s2/favicons?sz=64&domain=f-list.net
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12.  
  13. (function() {
  14. 'use strict';
  15.  
  16. function _gmSortCharacterList(target) {
  17.  
  18. //https://stackoverflow.com/questions/13033472/ordering-a-select-with-javascript-without-jquery
  19. var sorted = Array.prototype.slice.call(target.options).sort(function(a, b) {
  20. if(a.label < b.label) return -1;
  21. if(a.label > b.label) return 1;
  22. return 0;
  23. });
  24.  
  25. for(var i = 0; i < sorted.length; i++) {
  26. target.add(sorted[i]);
  27. }
  28.  
  29. }
  30.  
  31. // get element used to fill in character lists on note page
  32. var _gmCharListNotes = document.getElementById("NoteVarCharacters");
  33. var _gmCharListOther = document.getElementsByName("character_id");
  34. var _gmCharListChatLogs = document.getElementById("character");
  35.  
  36.  
  37. // only try sorting this if the element actually exists
  38. if(_gmCharListNotes) {
  39. _gmSortCharacterList(_gmCharListNotes);
  40. }
  41.  
  42. // only try sorting this if the element actually exists and is a select element
  43. if(_gmCharListOther && _gmCharListOther.length > 0 && _gmCharListOther[0].type === "select-one") {
  44. _gmSortCharacterList(_gmCharListOther[0]);
  45. }
  46.  
  47. // only try sorting this if we're on the chat3 page and it is a select element
  48. if(window.location.href.includes("chat3") && _gmCharListChatLogs && _gmCharListChatLogs.type === "select-one") {
  49. _gmSortCharacterList(_gmCharListChatLogs);
  50. }
  51.  
  52. // observe document title so we can ensure that character list in logs modal gets sorted again
  53. // https://stackoverflow.com/questions/2497200/how-to-listen-for-changes-to-the-title-element
  54. if(window.location.href.includes("chat3")) {
  55. new MutationObserver(function(mutations) {
  56. var _gmTitleText = mutations[0].target.innerText;
  57. if(!_gmTitleText.includes("(") && !_gmTitleText.includes(")")) {
  58. return;
  59. }
  60.  
  61. // give chat a little bit of time to shuffle the character list before we attempt to sort it
  62. const characterSortTimeout = setTimeout(function() {
  63. // using the same ID multiple times is a no-no, but it happens in chat3, so...
  64. var characterSelects = document.querySelectorAll("[id='character']");
  65.  
  66. for(var i = 0; i < characterSelects.length; i++) {
  67. if(characterSelects[i].type === "select-one") {
  68. _gmSortCharacterList(characterSelects[i]);
  69. }
  70. }
  71. }, 2000);
  72.  
  73. }).observe(
  74. document.querySelector('title'),
  75. { subtree: true, characterData: true, childList: true }
  76. );
  77. }
  78.  
  79. })();