Greasy Fork - Change Default Script Sort

Change default script sort on GreasyFork

当前为 2016-08-16 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Greasy Fork - Change Default Script Sort
  3. // @namespace https://greasyfork.org/users/649
  4. // @version 1.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. // @run-at document-start
  13. // ==/UserScript==
  14.  
  15. (function() {
  16. 'use strict';
  17.  
  18. var Util = {
  19. getQueryParameter: function(name, url) {
  20. if (!url) url = window.location.href;
  21. name = name.replace(/[\[\]]/g, "\\$&");
  22. var regex = new RegExp("[?&]" + name + "(=([^&#]*)|&|#|$)"),
  23. results = regex.exec(url);
  24. if (!results) return null;
  25. if (!results[2]) return '';
  26. return decodeURIComponent(results[2].replace(/\+/g, " "));
  27. },
  28.  
  29. setQueryParameter: function(key, value, url) {
  30. if (!url) url = window.location.href;
  31. var re = new RegExp("([?&])" + key + "=.*?(&|#|$)(.*)", "gi"),
  32. hash;
  33.  
  34. if (re.test(url)) {
  35. if (typeof value !== 'undefined' && value !== null)
  36. 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)
  41. url += '#' + hash[1];
  42. return url;
  43. }
  44. }
  45. else {
  46. if (typeof value !== 'undefined' && value !== null) {
  47. var separator = url.indexOf('?') !== -1 ? '&' : '?';
  48. hash = url.split('#');
  49. url = hash[0] + separator + key + '=' + value;
  50. if (typeof hash[1] !== 'undefined' && hash[1] !== null)
  51. url += '#' + hash[1];
  52. return url;
  53. }
  54. else
  55. return url;
  56. }
  57. }
  58. };
  59.  
  60. var Config = {
  61. load: function() {
  62. var defaults = {
  63. all: 'daily-installs',
  64. search: 'relevance',
  65. user: 'daily-installs'
  66. };
  67.  
  68. var cfg = GM_getValue('cfg');
  69. if (!cfg) return defaults;
  70.  
  71. cfg = JSON.parse(cfg);
  72. for (var property in defaults) {
  73. if (defaults.hasOwnProperty(property)) {
  74. if (!cfg[property]) {
  75. cfg[property] = defaults[property];
  76. }
  77. }
  78. }
  79.  
  80. return cfg;
  81. },
  82.  
  83. save: function (cfg) {
  84. GM_setValue('cfg', JSON.stringify(cfg));
  85. },
  86.  
  87. setup: function() {
  88. var createContainer = function() {
  89. var div = document.createElement('div');
  90. div.style.backgroundColor = 'white';
  91. div.style.padding = '5px';
  92. div.style.border = '1px solid black';
  93. div.style.position = 'fixed';
  94. div.style.top = '0';
  95. div.style.right = '0';
  96. return div;
  97. };
  98.  
  99. var createSelect = function(label, options, value) {
  100. var select = document.createElement('select');
  101. select.style.margin = '2px';
  102. var optgroup = document.createElement('optgroup');
  103. if (label) {
  104. optgroup.setAttribute('label', label);
  105. }
  106. select.appendChild(optgroup);
  107. options.forEach(function(opt) {
  108. var option = document.createElement('option');
  109. option.setAttribute('value', opt.value);
  110. option.textContent = opt.text;
  111. optgroup.appendChild(option);
  112. });
  113. select.value = value;
  114. return select;
  115. };
  116.  
  117. var createButton = function(text, onclick) {
  118. var button = document.createElement('button');
  119. button.style.margin = '2px';
  120. button.textContent = text;
  121. button.onclick = onclick;
  122. return button;
  123. };
  124.  
  125. var createLabel = function(label) {
  126. var lbl = document.createElement('span');
  127. lbl.textContent = label;
  128. return lbl;
  129. };
  130.  
  131. var createLineBreak = function() {
  132. return document.createElement('br');
  133. };
  134.  
  135. var init = function(cfg) {
  136. var div = createContainer();
  137.  
  138. var all = createSelect('All Scripts Sort', [
  139. { value: 'daily-installs', text: 'Daily installs' },
  140. { value: 'total_installs', text: 'Total installs' },
  141. { value: 'ratings', text: 'Ratings' },
  142. { value: 'created', text: 'Created date' },
  143. { value: 'updated', text: 'Updated date' },
  144. { value: 'name', text: 'Name' }
  145. ], cfg.all);
  146. div.appendChild(createLabel('All Scripts Sort: '));
  147. div.appendChild(all);
  148. div.appendChild(createLineBreak());
  149.  
  150. var search = createSelect('Search Sort', [
  151. { value: 'relevance', text: 'Relevance' },
  152. { value: 'daily-installs', text: 'Daily installs' },
  153. { value: 'total_installs', text: 'Total installs' },
  154. { value: 'ratings', text: 'Ratings' },
  155. { value: 'created', text: 'Created date' },
  156. { value: 'updated', text: 'Updated date' },
  157. { value: 'name', text: 'Name' }
  158. ], cfg.search);
  159. div.appendChild(createLabel('Search Sort: '));
  160. div.appendChild(search);
  161. div.appendChild(createLineBreak());
  162.  
  163. var user = createSelect('User Profile Sort', [
  164. { value: 'daily-installs', text: 'Daily installs' },
  165. { value: 'total_installs', text: 'Total installs' },
  166. { value: 'ratings', text: 'Ratings' },
  167. { value: 'created', text: 'Created date' },
  168. { value: 'updated', text: 'Updated date' },
  169. { value: 'name', text: 'Name' }
  170. ], cfg.user);
  171. div.appendChild(createLabel('User Profile Sort: '));
  172. div.appendChild(user);
  173. div.appendChild(createLineBreak());
  174.  
  175. div.appendChild(createButton('Save', function(e) {
  176. var settings = {
  177. all: all.value,
  178. search: search.value,
  179. user: user.value
  180. };
  181. Config.save(settings);
  182. div.remove();
  183. }));
  184.  
  185. div.appendChild(createButton('Cancel', function(e) {
  186. div.remove();
  187. }));
  188.  
  189. document.body.appendChild(div);
  190. };
  191. init(Config.load());
  192. }
  193. };
  194.  
  195. GM_registerMenuCommand('GreasyFork Sort Settings', Config.setup);
  196.  
  197. var onScripts = location.href.match(/^https?:\/\/greasyfork\.org\/[^\/]+\/scripts\/?(?:\?.*)?$/i);
  198. var onSearch = location.href.match(/^https?:\/\/greasyfork\.org\/[^\/]+\/scripts\/search?(?:\?.*)?$/i);
  199. var onProfile = location.href.match(/^https?:\/\/greasyfork\.org\/[^\/]+\/users\/[^\/]+?(?:\?.*)?$/i);
  200.  
  201. document.addEventListener('DOMContentLoaded', function(e) {
  202. var defaultSort = document.querySelector('#script-list-sort > ul > li:nth-child(1) > a');
  203. if (defaultSort) {
  204. if (onSearch) {
  205. defaultSort.href = Util.setQueryParameter('sort', 'relevance', defaultSort.href);
  206. } else {
  207. defaultSort.href = Util.setQueryParameter('sort', 'daily-installs', defaultSort.href);
  208. }
  209. }
  210. });
  211.  
  212. var sort = Util.getQueryParameter('sort');
  213. if (!sort) {
  214. var cfg = Config.load();
  215. var cfgSort;
  216. if (onScripts) {
  217. cfgSort = cfg.all;
  218. } else if (onSearch) {
  219. cfgSort = cfg.search;
  220. } else if (onProfile) {
  221. cfgSort = cfg.user;
  222. }
  223. if (cfgSort) {
  224. window.location.replace(Util.setQueryParameter('sort', cfgSort));
  225. }
  226. }
  227. })();