-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathpreload.js
206 lines (194 loc) · 5.5 KB
/
preload.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
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
const Nanobar = require('nanobar')
const MouseTrap = require('mousetrap')
const squirrel = require('./utils/squirrel')
const cssList = require('./css')
const sites = require('./sites')
// 插件依赖两组模板, 没有模板的规则都被过滤掉
.filter(s => s.properties.SEARCH_LITE_TITLE_TEMPLATE !== null && s.properties.SEARCH_LITE_TITLE_TEMPLATE !== '')
.filter(s => s.properties.SEARCH_LITE_DESC_TEMPLATE !== null && s.properties.SEARCH_LITE_DESC_TEMPLATE !== '')
const settings = require('./settings')
let loadingBar, input
utools.onPluginReady(() => {
// 初始化松鼠集
squirrel.initial()
// 初始化进度条
loadingBar = new Nanobar({
classname: 'loading-bar'
})
// 初始化设置对话框
settings.initial(squirrel)
// 初始化样式
let head = document.head || document.getElementsByTagName('head')[0]
cssList.forEach(css => {
let style = document.createElement('style')
head.appendChild(style)
style.appendChild(css)
})
})
utools.onPluginEnter(({
code,
type,
payload,
optional
}) => {
utools.subInputFocus()
})
const labelUtils = require('./utils/label')
let observe
// 监听节点变化
const initialObserver = () => {
observe = new MutationObserver((mutations, observer) => {
mutations.forEach(m => {
if (m.addedNodes && m.addedNodes.length > 0) {
let node = m.addedNodes[0]
let html = node.innerHTML
html = labelUtils.handle(html)
node.innerHTML = html
}
})
})
let el = document.querySelector('#root .list div:nth-child(1)')
observe.observe(el, {
'childList': true
})
}
const initialKeyBinding = () => {
// 初始化按键绑定
MouseTrap.bind('enter', async () => {
if (document.hasFocus()) {
let link = document.querySelector('#root .list .list-item-selected span.link').innerHTML
if (link !== '') {
utools.shellOpenExternal(link)
// utools.ubrowser.goto(link).run({ width: 1000, height: 600 })
}
}
else {
if (input) {
let {
code,
text,
callback
} = input
let site = sites.find(s => s.code === code)
if (site && text !== '') {
callback([])
loadingBar.go(45)
let result = await squirrel.page({
code: code,
search: text
})
console.log(JSON.stringify(result))
loadingBar.go(100)
if (result.code === 0) {
let data = result.data
if (data && data.list && data.list.length > 0) {
callback(data.list)
utools.subInputBlur()
}
else {
utools.showNotification('资源获取成功, 但没有找到更多内容, 请更换搜索词')
}
}
else {
utools.showNotification('资源获取失败, 可能是网络问题, 尝试设置代理使用')
callback([])
}
}
}
}
})
MouseTrap.bind('esc', () => {
if (settings.isOpen()) {
settings.close()
utools.subInputFocus()
}
else {
utools.subInputSelect()
}
return false
})
MouseTrap.bind('ctrl+t', () => {
settings.open()
utools.subInputBlur()
})
MouseTrap.bind('ctrl+s', () => {
if (input) {
let {
code,
text,
callback
} = input
let site = sites.find(s => s.code === code)
if (site && text !== '' && site.search) {
let result = squirrel.parseUrl({
code: code,
search: text
})
if (result.code === 0) {
utools.shellOpenExternal(result.data)
}
}
}
})
}
const placeholder = 'Enter(回车): 搜索/选择 Ctrl + s: 直接搜索 Ctrl + t: 打开设置'
let exportList = {}
const filterSites = text => sites
.filter(site => {
if (text && text !== '') {
return site.name.toLowerCase()
.indexOf(text.toLowerCase()) > -1
}
return true
})
.map(site => {
return {
site: site,
title: `#title{${site.name}}#other{${site.category}}`,
description: site.description,
icon: site.icon,
}
})
exportList['list'] = {
mode: 'list',
args: {
enter: (action, callbackSetList) => {
0
initialObserver()
callbackSetList(filterSites(''))
},
select: (action, itemData, callbackSetList) => {
utools.redirect(itemData.site.name)
}
}
}
sites
.forEach(s => {
exportList[s.code] = {
mode: 'list',
args: {
enter: () => {
initialObserver()
initialKeyBinding()
},
search: async (action, text, callbackSetList) => {
input = {
code: s.code,
text: text,
callback: list => callbackSetList(list.map(i => {
let item = {
title: eval('`' + s.properties.SEARCH_LITE_TITLE_TEMPLATE + '`'),
description: eval('`' + s.properties.SEARCH_LITE_DESC_TEMPLATE + '`'),
}
if (s.properties.SEARCH_LITE_IMAGE_TEMPLATE !== null && s.properties.SEARCH_LITE_IMAGE_TEMPLATE !== '') {
item.icon = eval('`' + s.properties.SEARCH_LITE_IMAGE_TEMPLATE + '`')
}
return item
}))
}
},
placeholder: placeholder
}
}
})
window.exports = exportList