forked from acgotaku/115
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
119 lines (113 loc) · 4.04 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
if (typeof browser != "undefined")
chrome = browser;
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);
var h;
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();
}
});
};
chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
console.log(request.method);
console.log(request.data);
switch (request.method) {
case "add_script":
chrome.tabs.executeScript(sender.tab.id, { file: request.data });
break;
case "rpc_data":
HttpSendRead(request.data)
.done(function (json, textStatus, jqXHR) {
sendResponse(true);
})
.fail(function (jqXHR, textStatus, errorThrown) {
sendResponse(false);
});
return true;
case "config_data":
for (var key in request.data) {
localStorage.setItem(key, request.data[key]);
}
break;
case "rpc_version":
HttpSendRead(request.data)
.done(function (json, textStatus, jqXHR) {
sendResponse(json);
})
.fail(function (jqXHR, textStatus, errorThrown) {
sendResponse(null);
});
return true;
case "get_cookies":
getCookies(request.data).then(value => sendResponse(value));
return true;
}
});
// Promise style `chrome.cookies.get()`
function getCookie(detail) {
return new Promise(function (resolve) {
chrome.cookies.get(detail, resolve);
});
};
//async function getCookies(details)
//{
// var obj = {};
// for (var item of await Promise.all(details.map(item => getCookie(item))))
// obj[item.name] = item.value;
// return obj;
//}
function getCookies(details) {
return new Promise(function (resolve) {
var list = details.map(item => getCookie(item));
Promise.all(list).then(function (cookies) {
var obj = {};
for (var item of cookies)
if (item != null)
obj[item.name] = item.value;
resolve(obj);
});
});
}