-
Notifications
You must be signed in to change notification settings - Fork 0
/
background.js
109 lines (97 loc) · 2.42 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
var currentTab;
var version = "1.0";
let problemId;
let response;
chrome.tabs.onUpdated.addListener(async () => {
chrome.tabs.query(
//get current Tab
{
currentWindow: true,
active: true,
},
function (tabArray) {
currentTab = tabArray[0];
if (!currentTab.url.startsWith("chrome:")) {
chrome.debugger.attach(
{
//debug at current tab
tabId: currentTab.id,
},
version,
onAttach.bind(null, currentTab.id)
);
}
}
);
});
function onAttach(tabId) {
chrome.debugger
.sendCommand(
{
//first enable the Network
tabId: tabId,
},
"Network.enable"
)
}
chrome.debugger.onEvent.addListener(allEventHandler);
function allEventHandler(debuggeeId, message, params) {
if (currentTab.id != debuggeeId.tabId) {
return;
}
if (message == "Network.responseReceived") {
chrome.debugger.sendCommand(
{
tabId: debuggeeId.tabId,
},
"Network.getResponseBody",
{
requestId: params.requestId,
},
function (response) {
if (response?.body) {
try {
let res = JSON.parse(response.body);
// console.log(
// "Parsed Response",
// res?.upid + " " + res?.result_code + " " + res?.time
// );
response = res;
if (
res?.upid &&
(res?.result_code == "accepted" ||
res?.result_code == "compile" || res?.result_code === "wrong") &&
res?.time
) {
chrome.tabs.sendMessage(currentTab.id, {
problemId: problemId,
resultCode:
res?.result_code === "compile"
? "having compilation error"
: res?.result_code,
});
}
} catch (e) {}
}
}
);
}
}
chrome.runtime.onMessage.addListener(function (message, sender) {
if (!message || typeof message !== "object" || !sender.tab) {
return;
}
switch (message.action) {
case "receiveBodyText": {
problemId = sender.tab.url.substring(sender.tab.url.lastIndexOf("/") + 1);
break;
}
}
if (message && message.type === "notification") {
isTypeNotification = true;
let datum = {
...message.options,
};
chrome.notifications.create("", datum);
}
});