Greasy Fork - Change Default Script Sort

Change default script sort on GreasyFork

当前为 2018-03-13 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Greasy Fork - Change Default Script Sort
  3. // @namespace https://greasyfork.org/users/649
  4. // @version 1.2.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://cdn.rawgit.com/kufii/My-UserScripts/986f0f450f6a3940907ce4798f1bb5c092549ec1/libs/gm_config.js
  13. // @run-at document-start
  14. // ==/UserScript==
  15.  
  16. (() => {
  17. 'use strict';
  18.  
  19. const Util = {
  20. q(query, context = document) {
  21. return context.querySelector(query);
  22. },
  23. getQueryParameter(name, url = window.location.href) {
  24. name = name.replace(/[[\]]/g, '\\$&');
  25. let regex = new RegExp(`[?&]${name}(=([^&#]*)|&|#|$)`),
  26. results = regex.exec(url);
  27. if (!results) return null;
  28. if (!results[2]) return '';
  29. return decodeURIComponent(results[2].replace(/\+/g, ' '));
  30. },
  31. setQueryParameter(key, value, url = window.location.href) {
  32. let re = new RegExp(`([?&])${key}=.*?(&|#|$)(.*)`, 'gi'),
  33. hash;
  34.  
  35. if (re.test(url)) {
  36. if (typeof value !== 'undefined' && value !== null) return url.replace(re, `$1${key}=${value}$2$3`);
  37. else {
  38. hash = url.split('#');
  39. url = hash[0].replace(re, '$1$3').replace(/(&|\?)$/, '');
  40. if (typeof hash[1] !== 'undefined' && hash[1] !== null) url += `#${hash[1]}`;
  41. return url;
  42. }
  43. } else if (typeof value !== 'undefined' && value !== null) {
  44. let separator = url.indexOf('?') !== -1 ? '&' : '?';
  45. hash = url.split('#');
  46. url = `${hash[0] + separator + key}=${value}`;
  47. if (typeof hash[1] !== 'undefined' && hash[1] !== null) url += `#${hash[1]}`;
  48. return url;
  49. } else return url;
  50. }
  51. };
  52.  
  53. const commonValues = [
  54. { value: 'daily-installs', text: 'Daily installs' },
  55. { value: 'total_installs', text: 'Total installs' },
  56. { value: 'ratings', text: 'Ratings' },
  57. { value: 'created', text: 'Created date' },
  58. { value: 'updated', text: 'Updated date' },
  59. { value: 'name', text: 'Name' }
  60. ];
  61. const Config = GM_config([
  62. {
  63. key: 'all',
  64. label: 'All Scripts Sort',
  65. default: 'daily-installs',
  66. type: 'dropdown',
  67. values: commonValues
  68. },
  69. {
  70. key: 'search',
  71. label: 'Search Sort',
  72. default: 'relevance',
  73. type: 'dropdown',
  74. values: [{ value: 'relevance', text: 'Relevance' }].concat(commonValues)
  75. },
  76. {
  77. key: 'user',
  78. label: 'User Profile Sort',
  79. default: 'daily-installs',
  80. type: 'dropdown',
  81. values: commonValues
  82. }
  83. ]);
  84. GM_registerMenuCommand('GreasyFork Sort Settings', Config.setup);
  85.  
  86. let onScripts = location.href.match(/^https?:\/\/greasyfork\.org\/[^/]+\/scripts\/?(?:\?.*)?$/i);
  87. let onSearch = location.href.match(/^https?:\/\/greasyfork\.org\/[^/]+\/scripts\/search?(?:\?.*)?$/i);
  88. let onProfile = location.href.match(/^https?:\/\/greasyfork\.org\/[^/]+\/users\/[^/]+?(?:\?.*)?$/i);
  89.  
  90. document.addEventListener('DOMContentLoaded', () => {
  91. let defaultSort = Util.q('#script-list-sort > ul > li:nth-child(1) > a');
  92. if (defaultSort) {
  93. if (onSearch) {
  94. defaultSort.href = Util.setQueryParameter('sort', 'relevance', defaultSort.href);
  95. } else {
  96. defaultSort.href = Util.setQueryParameter('sort', 'daily-installs', defaultSort.href);
  97. }
  98. }
  99. });
  100.  
  101. let sort = Util.getQueryParameter('sort');
  102. if (!sort) {
  103. let cfg = Config.load();
  104. let cfgSort;
  105. if (onScripts) {
  106. cfgSort = cfg.all;
  107. } else if (onSearch) {
  108. cfgSort = cfg.search;
  109. } else if (onProfile) {
  110. cfgSort = cfg.user;
  111. }
  112. if (cfgSort) {
  113. window.location.replace(Util.setQueryParameter('sort', cfgSort));
  114. }
  115. }
  116. })();