-
Notifications
You must be signed in to change notification settings - Fork 2
/
greasyfork-default-sort.user.js
90 lines (85 loc) · 2.64 KB
/
greasyfork-default-sort.user.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
// ==UserScript==
// @name Greasy Fork - Change Default Script Sort
// @namespace https://greasyfork.org/users/649
// @version 1.3.5
// @description Change default script sort on GreasyFork
// @author Adrien Pyke
// @match *://greasyfork.org/*/users/*
// @match *://greasyfork.org/*/scripts*
// @grant GM_getValue
// @grant GM_setValue
// @grant GM_registerMenuCommand
// @require https://cdn.jsdelivr.net/gh/kufii/My-UserScripts@22210afba13acf7303fc91590b8265faf3c7eda7/libs/gm_config.js
// @require https://cdn.jsdelivr.net/gh/fuzetsu/userscripts@ec863aa92cea78a20431f92e80ac0e93262136df/wait-for-elements/wait-for-elements.js
// @run-at document-start
// ==/UserScript==
(() => {
'use strict';
const commonValues = [
{ value: 'daily-installs', text: 'Daily installs' },
{ value: 'total_installs', text: 'Total installs' },
{ value: 'ratings', text: 'Ratings' },
{ value: 'created', text: 'Created date' },
{ value: 'updated', text: 'Updated date' },
{ value: 'name', text: 'Name' }
];
const Config = GM_config([
{
key: 'all',
label: 'All Scripts Sort',
default: 'daily-installs',
type: 'dropdown',
values: commonValues
},
{
key: 'search',
label: 'Search Sort',
default: 'relevance',
type: 'dropdown',
values: [{ value: 'relevance', text: 'Relevance' }, ...commonValues]
},
{
key: 'user',
label: 'User Profile Sort',
default: 'daily-installs',
type: 'dropdown',
values: commonValues
}
]);
GM_registerMenuCommand('GreasyFork Sort Settings', Config.setup);
const onSearch = location.href.match(
/^https?:\/\/greasyfork\.org\/.+?\/scripts\/?.*\?.*q=/iu
);
const onScripts = location.href.match(
/^https?:\/\/greasyfork\.org\/.+?\/scripts\/?/iu
);
const onProfile = location.href.match(
/^https?:\/\/greasyfork\.org\/.+?\/users\//iu
);
waitForElems({
sel: '#script-list-sort > ul > li:first-of-type > a',
stop: true,
onmatch(defaultSort) {
const url = new URL(defaultSort.href);
url.searchParams.set('sort', onSearch ? 'relevance' : 'daily-installs');
defaultSort.href = url.href;
}
});
const url = new URL(location.href);
const sort = url.searchParams.get('sort');
if (!sort) {
const cfg = Config.load();
let cfgSort;
if (onSearch) {
cfgSort = cfg.search;
} else if (onScripts) {
cfgSort = cfg.all;
} else if (onProfile) {
cfgSort = cfg.user;
}
if (cfgSort) {
url.searchParams.set('sort', cfgSort);
window.location.replace(url.href);
}
}
})();