-
Notifications
You must be signed in to change notification settings - Fork 131
/
background.js
253 lines (241 loc) · 9.38 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
const defaultRPC='[{"name":"ARIA2 RPC","url":"http://localhost:6800/jsonrpc"}]';
var HttpSendRead = function(info) {
Promise.prototype.done=Promise.prototype.then;
Promise.prototype.fail=Promise.prototype.catch;
return new Promise(function(resolve, reject) {
var http = new XMLHttpRequest();
var contentType = "\u0061\u0070\u0070\u006c\u0069\u0063\u0061\u0074\u0069\u006f\u006e\u002f\u0078\u002d\u0077\u0077\u0077\u002d\u0066\u006f\u0072\u006d\u002d\u0075\u0072\u006c\u0065\u006e\u0063\u006f\u0064\u0065\u0064\u003b\u0020\u0063\u0068\u0061\u0072\u0073\u0065\u0074\u003d\u0055\u0054\u0046\u002d\u0038";
var timeout = 3000;
if (info.contentType != null) {
contentType = info.contentType;
}
if (info.timeout != null) {
timeout = info.timeout;
}
var timeId = setTimeout(httpclose, timeout);
function httpclose() {
http.abort();
}
http.onreadystatechange = function() {
if (http.readyState == 4) {
if ((http.status == 200 && http.status < 300) || http.status == 304) {
clearTimeout(timeId);
if (info.dataType == "json") {
resolve(JSON.parse(http.responseText), http.status, http);
}
else if (info.dataType == "SCRIPT") {
// eval(http.responseText);
resolve(http.responseText, http.status, http);
}
}
else {
clearTimeout(timeId);
reject(http, http.statusText, http.status);
}
}
}
http.open(info.type, info.url, true);
http.setRequestHeader("Content-type", contentType);
for (h in info.headers) {
if (info.headers[h]) {
http.setRequestHeader(h, info.headers[h]);
}
}
if (info.type == "POST") {
http.send(info.data);
}
else {
http.send();
}
});
};
//生成右键菜单
function addContextMenu(id,title){
chrome.contextMenus.create({
id:id,
title: title,
contexts: ['link']
});
}
//弹出chrome通知
function showNotification(id,opt){
var notification = chrome.notifications.create(id,opt,function(notifyId){return notifyId});
setTimeout(function(){
chrome.notifications.clear(id,function(){});
},3000);
}
//解析RPC地址
function parse_url(url){
var auth_str = request_auth(url);
var auth = null;
if (auth_str) {
if(auth_str.indexOf('token:') == 0){
auth= auth_str;
}else{
auth = "Basic " + btoa(auth_str);
}
}
var url_path=remove_auth(url);
function request_auth(url) {
return url.match(/^(?:(?![^:@]+:[^:@\/]*@)[^:\/?#.]+:)?(?:\/\/)?(?:([^:@]*(?::[^:@]*)?)?@)?/)[1];
}
function remove_auth(url) {
return url.replace(/^((?![^:@]+:[^:@\/]*@)[^:\/?#.]+:)?(\/\/)?(?:(?:[^:@]*(?::[^:@]*)?)?@)?(.*)/, '$1$2$3');
}
return [url_path,auth];
}
function aria2Send(link,url){
chrome.cookies.getAll({"url":link}, function(cookies) {
var format_cookies = [];
for (var i in cookies) {
var cookie = cookies[i];
format_cookies.push(cookie.name +"="+cookie.value);
}
var header=[];
header.push("Cookie: " + format_cookies.join(";"));
header.push("User-Agent: " + navigator.userAgent);
header.push("Connection: keep-alive");
var rpc_data = {
"jsonrpc": "2.0",
"method": "aria2.addUri",
"id": new Date().getTime(),
"params": [[link],{
"header":header
}]
};
var result=parse_url(url);
var auth=result[1];
if (auth && auth.indexOf('token:') == 0) {
rpc_data.params.unshift(auth);
}
var parameter = {'url': result[0], 'dataType': 'json', type: 'POST', data: JSON.stringify(rpc_data), 'headers': {'Authorization': auth}};
HttpSendRead(parameter)
.done(function(json, textStatus, jqXHR) {
var opt={
type: "basic",
title: "下载成功",
message: "导出下载成功~",
iconUrl: "images/icon.jpg"
}
var id= new Date().getTime().toString();
showNotification(id,opt);
})
.fail(function(jqXHR, textStatus, errorThrown) {
console.log(jqXHR);
var opt={
type: "basic",
title: "下载失败",
message: "导出下载失败! QAQ",
iconUrl: "images/icon.jpg"
}
var id= new Date().getTime().toString();
showNotification(id,opt);
});
});
}
function isCapture(downloadItem){
var fileSize =localStorage.getItem("fileSize");
var white_site =JSON.parse(localStorage.getItem("white_site"));
var black_site =JSON.parse(localStorage.getItem("black_site"));
var url =downloadItem.referrer|| downloadItem.url;
if(downloadItem.error || downloadItem.state != "in_progress" || url.startsWith("http") == false){
return false;
}
var parse_url=/^(?:([A-Za-z]+):)?(\/{0,3})([0-9.\-A-Za-z]+)(?::(\d+))?(?:\/([^?#]*))?(?:\?([^#]*))?(?:#(.*))?$/;
var result=parse_url.exec(url)[3];
if(black_site.join("|").indexOf(result)> -1){
return false;
}
if(white_site.join("|").indexOf(result)> -1){
return true;
}
if(downloadItem.fileSize >= fileSize*1024*1024){
return true;
}else{
return false;
}
}
chrome.contextMenus.onClicked.addListener(function(info, tab) {
aria2Send(info.linkUrl,info.menuItemId);
});
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
if (changeInfo.status === 'loading') {
chrome.contextMenus.removeAll();
var contextMenus=localStorage.getItem("contextMenus");
if(contextMenus == "true" || contextMenus == null){
var rpc_list=JSON.parse(localStorage.getItem("rpc_list")||defaultRPC);
for(var i in rpc_list){
addContextMenu(rpc_list[i]['url'],rpc_list[i]['name']);
}
localStorage.setItem("contextMenus", true);
}
}
});
chrome.downloads.onCreated.addListener(function(downloadItem){
var integration =localStorage.getItem("integration");
if(integration == "true" && isCapture(downloadItem)){
var rpc_list=JSON.parse(localStorage.getItem("rpc_list")||defaultRPC);
aria2Send(downloadItem.url,rpc_list[0]['url']);
chrome.downloads.cancel(downloadItem.id,function(){});
}
});
/*
chrome.webRequest.onHeadersReceived.addListener(function(details){
for (var i = 0; i < details.responseHeaders.length; ++i) {
if (details.responseHeaders[i].name === 'Content-Type' && details.responseHeaders[i].value === 'application/octet-stream') {
var item = details.responseHeaders;
var flag = 0;
var downloadItem ={url:null,name:null,fileSize:0};
item.filter(function(obj){
if(obj.name === 'Content-Length' && obj.value == 0){
flag++;
}
if(obj.name === 'Content-Transfer-Encoding' || obj.name == 'Transfer-Encoding'|| obj.name == 'Content-Encoding'){
flag++;
}
if(obj.name === 'Content-Disposition'){
downloadItem.name= decodeURIComponent(obj.value.split("=")[1]);
}
if(obj.name === 'Content-Length'){
downloadItem.fileSize= obj.value;
}
});
if(!flag){
downloadItem.url = details.url;
var integration =localStorage.getItem("integration");
if(integration == "true" && isCapture(downloadItem)){
var rpc_list=JSON.parse(localStorage.getItem("rpc_list")||defaultRPC);
aria2Send(downloadItem.url,rpc_list[0]['url']);
}
return {cancel: true};
}
}
}
},{urls: ["<all_urls>"]}, ["blocking","responseHeaders"]);
*/
chrome.browserAction.onClicked.addListener(function(){
var index=chrome.extension.getURL('yaaw/index.html');
chrome.tabs.getAllInWindow(undefined, function(tabs) {
for (var i = 0, tab; tab = tabs[i]; i++) {
if (tab.url && tab.url == index) {
chrome.tabs.update(tab.id, {selected: true});
return;
}
}
chrome.tabs.create({url:index});
});
});
//软件版本更新提示
var manifest = chrome.runtime.getManifest();
var previousVersion=localStorage.getItem("version");
if(previousVersion == "" || previousVersion != manifest.version){
var opt={
type: "basic",
title: "更新",
message: "YAAW for Chrome更新到" +manifest.version + "版本啦~\n此次更新修复无法取消拦截的的BUG~",
iconUrl: "images/icon.jpg"
};
var id= new Date().getTime().toString();
showNotification(id,opt);
localStorage.setItem("version",manifest.version);
}