forked from jae-jae/Userscript-Plus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtools.js
More file actions
109 lines (103 loc) · 3 KB
/
Copy pathtools.js
File metadata and controls
109 lines (103 loc) · 3 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
/* global parent, Event, sessionStorage */
import timeago from 'timeago.js'
import fuzzy from 'fuzzy.js'
import psl from 'psl'
let config = {
cacheKey: 'jae_fetch_userjs_cache',
countKey: 'jae_fetch_userjs_count',
host: psl.get(window.location.hostname) || window.location.hostname.split('.').splice(-2).join('.'),
api: 'https://greasyfork.org/en/scripts/by-site/{host}.json'
}
export default {
timeagoFormat (time) {
let lang = (navigator.language === 'zh-CN') ? 'zh_CN' : 'en_short'
return timeago(null, lang).format(time)
},
installUserJs (uri) {
let evt = parent.document.createEvent('MouseEvents')
evt.initEvent('click', true, true)
let link = parent.document.createElement('a')
link.href = uri
// link.click()
link.dispatchEvent(evt)
},
dispatchEvent (eventName) {
parent.document.getElementById('jae_userscript_box').dispatchEvent(new Event(eventName))
},
/* Nano Templates - https://github.com/trix/nano */
nano (template, data) {
return template.replace(/\{([\w.]*)\}/g, function (str, key) {
let keys = key.split('.')
let v = data[keys.shift()]
for (let i = 0, l = keys.length; i < l; i++) v = v[keys[i]]
return (typeof v !== 'undefined' && v !== null) ? v : ''
})
},
getJSON (url, callback) {
parent.window.GmAjax({
method: 'GET',
url: url,
onload: (res) => {
let json = JSON.parse(res.responseText)
callback(json)
}
})
},
// 获取油猴缓存好的脚本数据
getData (callback) {
let data = sessionStorage.getItem(config.cacheKey)
if (data) {
data = JSON.parse(data)
callback(data)
} else {
let api = this.nano(config.api, {
host: config.host
})
this.getJSON(api, (json) => {
json = json.map((item) => {
item.user = item.users[0]
return item
})
sessionStorage.setItem(config.cacheKey, JSON.stringify(json))
callback(json)
})
}
},
getCount () {
let count = sessionStorage.getItem(config.countKey)
return count >= 50 ? 50 : count
},
searcher (data, query) {
let rt = []
for (let i = 0; i < data.length; i++) {
let item = data[i]
let max = null
let frt = null
for (let key of ['name', 'description', 'user']) {
if (key === 'user') {
frt = fuzzy(item['user']['name'], query)
} else {
frt = fuzzy(item[key], query)
}
if (max === null) {
max = frt
} else if (max.score < frt.score) {
max = frt
}
}
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()
if (nlang === 'zh') {
nlang = 'zh-cn'
}
return nlang.search('zh-') === 0
}
}