-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
91 lines (75 loc) · 2.61 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
let isProcessing = false;
let baseUrl = "http://localhost:8080/";
let postDataUrl = baseUrl + "proof";
let checkStatusUrl = baseUrl + "status";
chrome.runtime.onMessage.addListener(async (message, sender, sendResponse) => {
if (message.action === "sendData") {
const url = message.data.url;
const html = message.data.html;
console.log("isProcessing", isProcessing);
sendResponse(!isProcessing);
if (isProcessing) return;
isProcessing = true;
const bodyJson = {
source_url: url,
data: html,
};
try {
const request = new Request(postDataUrl, {
method: "POST",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify(bodyJson),
});
const result = await fetch(request);
const data = await result.json();
const id = data.reqId;
console.log(data);
console.log("id", id);
if (typeof data.error === "string") {
chrome.runtime.sendMessage({
target: "devtools",
message: {
action: "error",
},
});
isProcessing = false;
return;
}
while (true) {
let urlSearchParams = new URLSearchParams({
reqId: id,
});
const request = new Request(
checkStatusUrl + "?" + urlSearchParams,
{
method: "GET",
headers: {
"Content-Type": "application/json",
},
}
);
const response = await fetch(request);
const data = await response.json();
console.log(data);
let status = data.status;
let trackingId = data.tracking_id;
chrome.runtime.sendMessage({
target: "devtools",
message: {
action: "status",
status,
trackingId,
...(data.proof ? { proof: data.proof } : {}),
},
});
if (status === "completed") break;
await new Promise((resolve) => setTimeout(resolve, 2000));
}
} catch (e) {
console.log(e);
}
isProcessing = false;
}
});