forked from magicoflolis/Userscript-Plus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart.js
More file actions
210 lines (191 loc) 路 5.49 KB
/
Copy pathstart.js
File metadata and controls
210 lines (191 loc) 路 5.49 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
'use strict';
import { us } from './api.js';
import {qs,qsA} from "./querySelector.js";
import fuzzy from "fuzzy.js";
import { format } from "timeago.js";
import psl from "psl";
let config = {
api: 'https://greasyfork.org/scripts/by-site/{host}.json',
sapi: 'https://sleazyfork.org/scripts/by-site/{host}.json'
},
cfg,
brws = (typeof browser=="undefined"?chrome:browser),
msg = brws.runtime.connect({name:"messenger"});
msg.onMessage.addListener((m) => {
cfg = m.cfg;
us.log(cfg);
});
const Tools = {
timeagoFormat (time) {
let lang = (navigator.language !== 'zh-CN') ? 'en_short' : 'zh_CN'
return format(time, lang)
},
installUserJs(url) {
let jsStr = `
let evt = new MouseEvent("click"),
link = document.createElement("a");
link.href = '${url}';
link.dispatchEvent(evt);`;
brws.tabs.executeScript({code: jsStr});
},
/* Nano Templates - https://github.com/trix/nano */
nano(template, data) {
return template.replace(/\{([\w.]*)\}/g, (str, key) => {
let keys = key.split("."),
v = data[keys.shift()];
for (let i = 0, l = keys.length; i < l; i++) v = v[keys[i]];
return typeof v === "undefined" ? "" : v;
});
},
get currentTab() {
return new Promise((resolve, reject) => {
try {
let queryInfo = {
active: true,
currentWindow: true
};
brws.tabs.query(queryInfo, tabs => {
let tab = tabs[0];
resolve(tab);
});
} catch (error) {
reject(error);
}
});
},
get sessionStorage() {
return new Promise((resolve, reject) => {
try {
brws.runtime.getBackgroundPage(bg => {
resolve(bg.sessionStorage);
});
} catch (error) {
reject(error);
}
});
},
get host() {
return new Promise((resolve, reject) => {
try {
this.currentTab.then(tab => {
let a = document.createElement("a");
a.href = tab.url;
let mainHost = psl.get(a.hostname) || a.hostname.split(".").splice(-2).join(".");
resolve(mainHost);
});
} catch (error) {
reject(error);
}
});
},
getData(callback) {
this.sessionStorage.then(bgSessionStorage => {
this.host.then(host => {
let data = bgSessionStorage.getItem(host),
fetchJS = (url) => {
let f = fetch(url).then(r => r.json());
f.then(json => {
json = json.map(item => {
item.user = item.users[0];
return item;
});
bgSessionStorage.setItem(host, JSON.stringify(json));
callback(json);
})
};
(data) ? (data = JSON.parse(data),callback(data)) : (
fetchJS(this.nano(config.api, {host: host})),
fetchJS(this.nano(config.sapi, {host: host}))
);
});
});
},
searcher(data, query) {
let rt = [];
for (let i = 0; i < data.length; i++) {
let max,frt,item = data[i];
for (let key of ["name", "description", "user"]) {
key === "user" ? (frt = fuzzy(item["user"]["name"], query)) : (frt = fuzzy(item[key], query));
max ?? (max = frt);
max.score < frt.score ? (max = frt) : false;
}
rt.push({
item,
score: max.score
});
}
rt = rt.filter(a => a.score !== 0).sort((a, b) => b.score - a.score).map(a => a.item);
return rt;
},
isZH() {
let nlang = navigator.language.toLowerCase();
nlang === "zh" ? (nlang = "zh-cn") : false;
return nlang.search("zh-") === 0;
}
};
let container = us.create("div","ivu-table-body",{
innerHTML: `<table style="width: 857px;" cellspacing="0" cellpadding="0" border="0">
<colgroup>
<col width="50">
<col width="50">
<col width="35%">
<col width="126">
<col width="105">
<col width="126">
<col width="100">
</colgroup>
<tbody class="ivu-table-tbody">
</tbody>
</table>`,
});
(() => {
qs("#app").append(container);
})();
{/* <tr class="ivu-table-row">
</tr> */}
let itemID = 0;
Tools.getData((json) => {
let data = json,
originData = json,
count = data.length;
for (let d of data) {
itemID+=1;
let row = us.create("tr","ivu-table-row", {
innerHTML: `<td class="">
<div class="ivu-table-cell ivu-table-cell-with-expand">
<div class="ivu-table-cell-expand"><i class="ivu-icon ivu-icon-ios-arrow-right"></i></div>
</div>
</td>
<td class="ivu-table-column-center">
<div class="ivu-table-cell"><span>${itemID}</span>
</div>
</td>
<td class="">
<div class="ivu-table-cell">
<span title="${d.description}" style="cursor: pointer;">${d.name}</span>
</div>
</td>
<td class="">
<div class="ivu-table-cell">
<span title="Click to access the ${d.user.name} home page" style="cursor: pointer;">${d.user.name}</span>
</div>
</td>
<td class="">
<div class="ivu-table-cell">
<span style="cursor: pointer;">${d.daily_installs}</span></div>
</td>
<td class="">
<div class="ivu-table-cell">
<span style="cursor: pointer;">${d.created_at}</span></div>
</td>
<td class="ivu-table-column-center">
<div class="ivu-table-cell">
<div><button type="button" class="ivu-btn ivu-btn-primary ivu-btn-small" style="margin-right: 5px;">
<i class="ivu-icon ivu-icon-ios-download-outline"></i> <span>Install</span></button></div>
</div>
</td>`,
});
container.append(row);
}
// us.log(data,originData,count);
})