-
Notifications
You must be signed in to change notification settings - Fork 130
/
Copy pathbackground.js
304 lines (276 loc) · 8.17 KB
/
background.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
const defaultRPC = 'http://localhost:6800/jsonrpc'
const httpSend = ({ url, options }, resolve, reject) => {
fetch(url, options).then((response) => {
if (response.ok) {
response.json().then((data) => {
resolve(data)
})
} else {
reject(response)
}
}).catch((err) => {
reject(err)
})
}
const getConfig = (key) => {
return new Promise(function (resolve) {
chrome.storage.local.get(key, resolve)
})
}
// 生成右键菜单
function addContextMenu (id, title) {
chrome.contextMenus.create({
id,
title,
contexts: ['link']
})
}
function updateContextMenu () {
chrome.contextMenus.removeAll(() => {
getConfig('isContextMenus').then(({ isContextMenus }) => {
if (isContextMenus !== false) {
getConfig('rpcLists').then(({ rpcLists }) => {
if (!rpcLists) {
rpcLists = [{
name: 'ARIA2 RPC',
path: defaultRPC
}]
}
rpcLists.forEach(rpcItem => {
addContextMenu(rpcItem.path, rpcItem.name)
})
})
}
})
})
}
chrome.storage.onChanged.addListener(function (changes, areaName) {
if (changes.rpcLists) {
updateContextMenu()
}
})
// 弹出chrome通知
function showNotification (id, opt) {
chrome.notifications.create(id, opt, function (notifyId) {
return notifyId
})
setTimeout(function () {
chrome.notifications.clear(id, function () {})
}, 3000)
}
function requestAuth (url) {
return url.match(/^(?:(?![^:@]+:[^:@/]*@)[^:/?#.]+:)?(?:\/\/)?(?:([^:@]*(?::[^:@]*)?)?@)?/)[1]
}
function removeAuth (url) {
return url.replace(/^((?![^:@]+:[^:@/]*@)[^:/?#.]+:)?(\/\/)?(?:(?:[^:@]*(?::[^:@]*)?)?@)?(.*)/, '$1$2$3')
}
// 解析 RPC地址 返回验证数据 和地址
function parseURL (url) {
const parseURL = new URL(removeAuth(url))
let authStr = requestAuth(url)
if (authStr) {
if (!authStr.includes('token:')) {
authStr = `Basic ${btoa(authStr)}`
}
}
const paramsString = parseURL.hash.substr(1)
const options = {}
const searchParams = new URLSearchParams(paramsString)
for (const key of searchParams) {
options[key[0]] = key.length === 2 ? key[1] : 'enabled'
}
const path = parseURL.origin + parseURL.pathname
return { authStr, path, options }
}
function generateParameter (authStr, path, data) {
if (authStr && authStr.startsWith('token')) {
data.params.unshift(authStr)
}
const parameter = {
url: path,
options: {
method: 'POST',
headers: {
'Content-type': 'application/x-www-form-urlencoded; charset=UTF-8'
},
body: JSON.stringify(data)
}
}
if (authStr && authStr.startsWith('Basic')) {
parameter.options.headers.Authorization = authStr
}
return parameter
}
function aria2Send (rpcPath, fileDownloadInfo) {
const { authStr, path, options } = parseURL(rpcPath)
chrome.cookies.getAll({ url: fileDownloadInfo.link }, function (cookies) {
const formatedCookies = []
cookies.forEach(cookie => {
formatedCookies.push(cookie.name + '=' + cookie.value)
})
const header = []
header.push('Cookie: ' + formatedCookies.join('; '))
header.push('User-Agent: ' + navigator.userAgent)
const rpcData = {
jsonrpc: '2.0',
method: 'aria2.addUri',
id: new Date().getTime(),
params: [
[fileDownloadInfo.link], {
header
}
]
}
const rpcOption = rpcData.params[1]
if (fileDownloadInfo.fileName) {
rpcOption.out = fileDownloadInfo.fileName
}
if (options) {
for (const key in options) {
rpcOption[key] = options[key]
}
}
getConfig('downloadPath').then(({ downloadPath }) => {
if (downloadPath) {
rpcOption.dir = downloadPath
}
const parameter = generateParameter(authStr, path, rpcData)
httpSend(parameter, () => {
const opt = {
type: 'basic',
title: chrome.i18n.getMessage('startDownload'),
message: fileDownloadInfo.fileName ? fileDownloadInfo.fileName : chrome.i18n.getMessage('downloadSuccess'),
iconUrl: fileDownloadInfo.icon ? fileDownloadInfo.icon : 'images/icon.jpg'
}
const id = new Date().getTime().toString()
showNotification(id, opt)
}, (error) => {
console.log(error)
const opt = {
type: 'basic',
title: chrome.i18n.getMessage('downloadFailed'),
message: chrome.i18n.getMessage('downloadFailedDesc'),
iconUrl: 'images/icon.jpg'
}
const id = new Date().getTime().toString()
showNotification(id, opt)
})
})
})
}
function matchRule (str, rule) {
return new RegExp('^' + rule.split('*').join('.*') + '$').test(str)
}
function getHostName (url) {
if (url.startsWith('http')) {
return decodeURI(new URL(url).hostname)
} else {
return url
}
}
async function isCapture (downloadItem) {
const { fileSize } = await getConfig('fileSize')
const { whitelist } = await getConfig('whitelist')
const { blocklist } = await getConfig('blocklist')
const url = downloadItem.referrer || downloadItem.url
if (downloadItem.error || downloadItem.state !== 'in_progress' || url.startsWith('http') === false) {
return false
}
const target = getHostName(url)
const inWhitelist = whitelist.split('\n').some((site) => {
const rule = getHostName(site)
return matchRule(target, rule)
})
if (inWhitelist) {
return true
}
const inBlocklist = blocklist.split('\n').some((site) => {
const rule = getHostName(site)
return matchRule(target, rule)
})
if (inBlocklist) {
return false
}
if (downloadItem.fileSize >= fileSize * 1024 * 1024) {
return true
} else {
return false
}
}
chrome.contextMenus.onClicked.addListener(function (info, tab) {
aria2Send(info.menuItemId, {
link: info.linkUrl
})
})
function interceptionDownload (downloadItem) {
getConfig('isInterception').then(({ isInterception }) => {
if (isInterception) {
isCapture(downloadItem).then(result => {
if (result) {
chrome.downloads.getFileIcon(downloadItem.id, function (iconUrl) {
if (chrome.runtime.lastError) {
console.log(chrome.runtime.lastError.message)
}
getConfig('rpcLists').then(({ rpcLists }) => {
if (!rpcLists) {
rpcLists = [{
name: 'ARIA2 RPC',
path: defaultRPC
}]
}
aria2Send(rpcLists[0].path, {
link: downloadItem.finalUrl,
filename: decodeURIComponent(downloadItem.filename).split(/[/\\]/).pop(),
icon: iconUrl || 'images/icon.jpg'
})
chrome.downloads.cancel(downloadItem.id, function () {})
chrome.downloads.erase({ id: downloadItem.id })
})
})
}
})
}
})
}
getConfig('isAutoRename').then(({ isAutoRename }) => {
if (isAutoRename !== false) {
chrome.downloads.onDeterminingFilename.addListener(interceptionDownload)
} else {
chrome.downloads.onCreated.addListener(interceptionDownload)
}
})
function openYAAW () {
const index = chrome.extension.getURL('yaaw/index.html')
chrome.tabs.getAllInWindow(undefined, function (tabs) {
tabs.forEach(tab => {
if (tab.url && tab.url === index) {
chrome.tabs.update(tab.id, { selected: true })
}
})
chrome.tabs.create({ url: index })
})
}
chrome.browserAction.onClicked.addListener(function () {
openYAAW()
})
chrome.notifications.onClicked.addListener(function () {
openYAAW()
})
// 软件版本更新提示
const manifest = chrome.runtime.getManifest()
const previousVersion = localStorage.getItem('version')
if (previousVersion === '' || previousVersion !== manifest.version) {
const opt = {
type: 'basic',
title: chrome.i18n.getMessage('updated'),
message: chrome.i18n.getMessage('updatedDesc', manifest.version),
iconUrl: 'images/icon.jpg'
}
const id = new Date().getTime().toString()
showNotification(id, opt)
localStorage.setItem('version', manifest.version)
}
if (!localStorage.getItem('jsonrpc_path')) {
localStorage.setItem('jsonrpc_path', defaultRPC)
}
updateContextMenu()