-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathbackground.js
58 lines (57 loc) · 1.93 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
chrome.runtime.onMessage.addListener(function (data, sender, onSuccess) {
if (data.type !== "url") {
return;
}
console.log("get");
var url = data.url;
if (data.cors == false) {
var mode = "no-cors";
} else {
var mode = "cors";
}
fetch(url, { method: "get", mode: mode })
.then((response) => response.text())
.then((responseText) => onSuccess(responseText));
return true; // Will respond asynchronously.
});
chrome.runtime.onMessage.addListener(function (data, sender, onSuccess) {
if (data.type !== "urlPost") {
return;
}
console.log("post");
var url = data.url;
if (data.formData !== undefined) {
var formData = new FormData();
var pData = data.formData;
for (i in pData) {
formData.append(i, pData[i]);
}
if ((data.cors = false)) {
console.log(formData);
fetch(url, { method: "POST", body: formData, mode: "no-cors" })
.then((response) => response.text())
.then((responseText) => onSuccess(responseText));
} else {
console.log(formData);
fetch(url, { method: "POST", body: formData })
.then((response) => response.text())
.then((responseText) => onSuccess(responseText));
}
return true; // Will respond asynchronously.
} else if (data.rawData !== undefined) {
var pData = data.rawData;
if ((data.cors = false)) {
fetch(url, {
method: "POST",
body: new URLSearchParams(pData),
mode: "no-cors",
}).then((response) => onSuccess(response));
} else {
fetch(url, {
method: "POST",
body: new URLSearchParams(pData),
}).then((response) => onSuccess(response));
}
return true; // Will respond asynchronously.
}
});