Greasy Fork - Change Default Script Sort

Change default script sort on GreasyFork

当前为 2020-03-10 提交的版本,查看 最新版本

  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(/^https?:\/\/greasyfork\.org\/.+?\/scripts\/?.*\?.*q=/iu);
  54. const onScripts = location.href.match(/^https?:\/\/greasyfork\.org\/.+?\/scripts\/?/iu);
  55. const onProfile = location.href.match(/^https?:\/\/greasyfork\.org\/.+?\/users\//iu);
  56.  
  57. waitForElems({
  58. sel: '#script-list-sort > ul > li:first-of-type > a',
  59. stop: true,
  60. onmatch(defaultSort) {
  61. const url = new URL(defaultSort.href);
  62. url.searchParams.set('sort', onSearch ? 'relevance' : 'daily-installs');
  63. defaultSort.href = url.href;
  64. }
  65. });
  66.  
  67. const url = new URL(location.href);
  68. const sort = url.searchParams.get('sort');
  69. if (!sort) {
  70. const cfg = Config.load();
  71. let cfgSort;
  72. if (onSearch) {
  73. cfgSort = cfg.search;
  74. } else if (onScripts) {
  75. cfgSort = cfg.all;
  76. } else if (onProfile) {
  77. cfgSort = cfg.user;
  78. }
  79. if (cfgSort) {
  80. url.searchParams.set('sort', cfgSort);
  81. window.location.replace(url.href);
  82. }
  83. }
  84. })();