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
130 lines (120 loc) · 3.41 KB
/
Copy pathtools.js
File metadata and controls
130 lines (120 loc) · 3.41 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
/* global chrome, psl, fetch */
import timeago from 'timeago.js'
import fuzzy from 'fuzzy.js'
let config = {
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 jsStr = `
var evt = document.createEvent('MouseEvents');
evt.initEvent('click', true, true);
var link = document.createElement('a');
link.href = '${uri}';
link.dispatchEvent(evt);
`
chrome.tabs.executeScript(null, {code: jsStr})
},
/* 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 : ''
})
},
get currentTab () {
return new Promise(function (resolve, reject) {
let queryInfo = {
active: true,
currentWindow: true
}
chrome.tabs.query(queryInfo, (tabs) => {
let tab = tabs[0]
resolve(tab)
})
})
},
get sessionStorage () {
return new Promise(function (resolve, reject) {
chrome.runtime.getBackgroundPage(function (bg) {
resolve(bg.sessionStorage)
})
})
},
get host () {
return new Promise((resolve, reject) => {
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)
})
})
},
// 获取油猴缓存好的脚本数据
getData (callback) {
this.sessionStorage.then((bgSessionStorage) => {
this.host.then((host) => {
let data = bgSessionStorage.getItem(host)
if (data) {
data = JSON.parse(data)
callback(data)
} else {
let api = this.nano(config.api, {
host: host
})
fetch(api)
.then((r) => {
r.json().then((json) => {
json = json.map((item) => {
item.user = item.users[0]
return item
})
bgSessionStorage.setItem(host, JSON.stringify(json))
callback(json)
})
})
}
})
})
},
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
}
}