Greasy Fork - Change Default Script Sort

Change default script sort on GreasyFork

目前為 2018-05-15 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name Greasy Fork - Change Default Script Sort
  3. // @namespace https://greasyfork.org/users/649
  4. // @version 1.2.16
  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/4ce99f3afbf6c6edaa21bacc2981204582a20522/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. getQueryParam(name, url = location.href) {
  24. name = name.replace(/[[\]]/g, '\\$&');
  25. const regex = new RegExp(`[?&]${name}(=([^&#]*)|&|#|$)`);
  26. const results = regex.exec(url);
  27. if (!results) return null;
  28. if (!results[2]) return '';
  29. return decodeURIComponent(results[2].replace(/\+/g, ' '));
  30. },
  31. setQueryParam(key, value, url = location.href) {
  32. const regex = new RegExp(`([?&])${key}=.*?(&|#|$)(.*)`, 'gi');
  33. const hasValue = (typeof value !== 'undefined' && value !== null && value !== '');
  34. if (regex.test(url)) {
  35. if (hasValue) {
  36. return url.replace(regex, `$1${key}=${value}$2$3`);
  37. } else {
  38. let [path, hash] = url.split('#');
  39. url = path.replace(regex, '$1$3').replace(/(&|\?)$/, '');
  40. if (hash) url += `#${hash[1]}`;
  41. return url;
  42. }
  43. } else if (hasValue) {
  44. let separator = url.includes('?') ? '&' : '?';
  45. let [path, hash] = url.split('#');
  46. url = `${path + separator + key}=${value}`;
  47. if (hash) 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. key: 'search',
  70. label: 'Search Sort',
  71. default: 'relevance',
  72. type: 'dropdown',
  73. values: [{ value: 'relevance', text: 'Relevance' }].concat(commonValues)
  74. }, {
  75. key: 'user',
  76. label: 'User Profile Sort',
  77. default: 'daily-installs',
  78. type: 'dropdown',
  79. values: commonValues
  80. }
  81. ]);
  82. GM_registerMenuCommand('GreasyFork Sort Settings', Config.setup);
  83.  
  84. let onScripts = location.href.match(/^https?:\/\/greasyfork\.org\/[^/]+\/scripts\/?(?:\?.*)?$/i);
  85. let onSearch = location.href.match(/^https?:\/\/greasyfork\.org\/[^/]+\/scripts\/search?(?:\?.*)?$/i);
  86. let onProfile = location.href.match(/^https?:\/\/greasyfork\.org\/[^/]+\/users\/[^/]+?(?:\?.*)?$/i);
  87.  
  88. document.addEventListener('DOMContentLoaded', () => {
  89. let defaultSort = Util.q('#script-list-sort > ul > li:nth-child(1) > a');
  90. if (defaultSort) {
  91. if (onSearch) {
  92. defaultSort.href = Util.setQueryParam('sort', 'relevance', defaultSort.href);
  93. } else {
  94. defaultSort.href = Util.setQueryParam('sort', 'daily-installs', defaultSort.href);
  95. }
  96. }
  97. });
  98.  
  99. let sort = Util.getQueryParam('sort');
  100. if (!sort) {
  101. let cfg = Config.load();
  102. let cfgSort;
  103. if (onScripts) {
  104. cfgSort = cfg.all;
  105. } else if (onSearch) {
  106. cfgSort = cfg.search;
  107. } else if (onProfile) {
  108. cfgSort = cfg.user;
  109. }
  110. if (cfgSort) {
  111. window.location.replace(Util.setQueryParam('sort', cfgSort));
  112. }
  113. }
  114. })();