Greasy Fork - Change Default Script Sort

Change default script sort on GreasyFork

目前为 2018-09-06 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name Greasy Fork - Change Default Script Sort
  3. // @namespace https://greasyfork.org/users/649
  4. // @version 1.3.1
  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://cdn.rawgit.com/kufii/My-UserScripts/fa4555701cf5a22eae44f06d9848df6966788fa8/libs/gm_config.js
  13. // @require https://cdn.rawgit.com/fuzetsu/userscripts/89e64ca31aa4c27ce8bc68a84ffac53e06f074c0/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. key: 'search',
  37. label: 'Search Sort',
  38. default: 'relevance',
  39. type: 'dropdown',
  40. values: [
  41. { value: 'relevance', text: 'Relevance' },
  42. ...commonValues
  43. ]
  44. }, {
  45. key: 'user',
  46. label: 'User Profile Sort',
  47. default: 'daily-installs',
  48. type: 'dropdown',
  49. values: commonValues
  50. }
  51. ]);
  52. GM_registerMenuCommand('GreasyFork Sort Settings', Config.setup);
  53.  
  54. const onSearch = location.href.match(/^https?:\/\/greasyfork\.org\/.+?\/scripts\/?.*\?.*q=/i);
  55. const onScripts = location.href.match(/^https?:\/\/greasyfork\.org\/.+?\/scripts\/?/i);
  56. const onProfile = location.href.match(/^https?:\/\/greasyfork\.org\/.+?\/users\//i);
  57.  
  58. waitForElems({
  59. sel: '#script-list-sort > ul > li:first-of-type > a',
  60. stop: true,
  61. onmatch(defaultSort) {
  62. const url = new URL(defaultSort.href);
  63. url.searchParams.set('sort', onSearch ? 'relevance' : 'daily-installs');
  64. defaultSort.href = url.href;
  65. }
  66. });
  67.  
  68. const url = new URL(location.href);
  69. const sort = url.searchParams.get('sort');
  70. if (!sort) {
  71. const cfg = Config.load();
  72. let cfgSort;
  73. if (onSearch) {
  74. cfgSort = cfg.search;
  75. } else if (onScripts) {
  76. cfgSort = cfg.all;
  77. } else if (onProfile) {
  78. cfgSort = cfg.user;
  79. }
  80. if (cfgSort) {
  81. url.searchParams.set('sort', cfgSort);
  82. window.location.replace(url.href);
  83. }
  84. }
  85. })();