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
82 lines (73 loc) · 2.16 KB
/
Copy pathtools.js
File metadata and controls
82 lines (73 loc) · 2.16 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
/* global parent, Event, sessionStorage */
import timeago from 'timeago.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 currentTabWindow() {
return new Promise(function(resolve, reject){
let queryInfo = {
active: true,
currentWindow: true
};
chrome.tabs.query(queryInfo, (tabs) => {
var tab = tabs[0];
chrome.windows.get(tab.windowId,function(win){
resolve(win)
})
});
})
},
get sessionStorage () {
return new Promise(function(resolve, reject){
chrome.runtime.getBackgroundPage(function (bg) {
resolve(bg.sessionStorage)
})
})
},
// 获取油猴缓存好的脚本数据
getData (callback) {
this.sessionStorage.then((bgSessionStorage) => {
let host = bgSessionStorage.getItem('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) => {
bgSessionStorage.setItem(host, JSON.stringify(json))
callback(json)
})
})
}
})
}
}