Greasy Fork - Change Default Script Sort

Change default script sort on GreasyFork

目前为 2022-01-30 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Greasy Fork - Change Default Script Sort
  3. // @namespace https://greasyfork.org/users/649
  4. // @version 1.3.4
  5. // @description Change default script sort on GreasyFork
  6. // @author Adrien Pyke
  7. // @match *://greasyfork.org/*/users/*
  8. // @match *://greasyfork.org/*/scripts*
  9. // @grant GM_getValue
  10. // @grant GM_setValue
  11. // @grant GM_registerMenuCommand
  12. // @require https://gitcdn.link/repo/kufii/My-UserScripts/fa4555701cf5a22eae44f06d9848df6966788fa8/libs/gm_config.js
  13. // @require https://gitcdn.link/repo/fuzetsu/userscripts/b38eabf72c20fa3cf7da84ecd2cefe0d4a2116be/wait-for-elements/wait-for-elements.js
  14. // @run-at document-start
  15. // ==/UserScript==
  16.  
  17. (() => {
  18. 'use strict';
  19.  
  20. const commonValues = [
  21. { value: 'daily-installs', text: 'Daily installs' },
  22. { value: 'total_installs', text: 'Total installs' },
  23. { value: 'ratings', text: 'Ratings' },
  24. { value: 'created', text: 'Created date' },
  25. { value: 'updated', text: 'Updated date' },
  26. { value: 'name', text: 'Name' }
  27. ];
  28. const Config = GM_config([
  29. {
  30. key: 'all',
  31. label: 'All Scripts Sort',
  32. default: 'daily-installs',
  33. type: 'dropdown',
  34. values: commonValues
  35. },
  36. {
  37. key: 'search',
  38. label: 'Search Sort',
  39. default: 'relevance',
  40. type: 'dropdown',
  41. values: [{ value: 'relevance', text: 'Relevance' }, ...commonValues]
  42. },
  43. {
  44. key: 'user',
  45. label: 'User Profile Sort',
  46. default: 'daily-installs',
  47. type: 'dropdown',
  48. values: commonValues
  49. }
  50. ]);
  51. GM_registerMenuCommand('GreasyFork Sort Settings', Config.setup);
  52.  
  53. const onSearch = location.href.match(
  54. /^https?:\/\/greasyfork\.org\/.+?\/scripts\/?.*\?.*q=/iu
  55. );
  56. const onScripts = location.href.match(
  57. /^https?:\/\/greasyfork\.org\/.+?\/scripts\/?/iu
  58. );
  59. const onProfile = location.href.match(
  60. /^https?:\/\/greasyfork\.org\/.+?\/users\//iu
  61. );
  62.  
  63. waitForElems({
  64. sel: '#script-list-sort > ul > li:first-of-type > a',
  65. stop: true,
  66. onmatch(defaultSort) {
  67. const url = new URL(defaultSort.href);
  68. url.searchParams.set('sort', onSearch ? 'relevance' : 'daily-installs');
  69. defaultSort.href = url.href;
  70. }
  71. });
  72.  
  73. const url = new URL(location.href);
  74. const sort = url.searchParams.get('sort');
  75. if (!sort) {
  76. const cfg = Config.load();
  77. let cfgSort;
  78. if (onSearch) {
  79. cfgSort = cfg.search;
  80. } else if (onScripts) {
  81. cfgSort = cfg.all;
  82. } else if (onProfile) {
  83. cfgSort = cfg.user;
  84. }
  85. if (cfgSort) {
  86. url.searchParams.set('sort', cfgSort);
  87. window.location.replace(url.href);
  88. }
  89. }
  90. })();