-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
60 lines (55 loc) · 1.97 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
"use strict";
// let browser = chrome;
function onError(error) {
console.error(`Error: ${error}`);
}
function filterData(details) {
let filter = browser.webRequest.filterResponseData(details.requestId);
let decoder = new TextDecoder("utf-8");
let encoder = new TextEncoder();
filter.ondata = event => {
// Get reqeuest data and release the request
let str = decoder.decode(event.data, { stream: true });
let body = JSON.parse(str)
filter.write(encoder.encode(str))
filter.disconnect()
// Send message to the content-script
let msg = "Question 1 = " + body.question_1.good_rep +
"\nQuestion 2 = " + body.question_2.good_rep +
"\nQuestion 3 = " + body.question_3.good_rep;
console.log(msg)
browser.tabs.query({
currentWindow: true,
active: true
}).then(function sendMessageToTabs(tabs) {
browser.tabs.sendMessage(
tabs[0].id,
{
rep1: body.question_1.good_rep,
rep2: body.question_2.good_rep,
rep3: body.question_3.good_rep
}
).then(response => {
console.log("Message from the content script:");
console.log(response.response);
}).catch(onError);
}
).catch(onError)
}
}
browser.webRequest.onBeforeRequest.addListener(
function logURL(details) {
console.log("Request listened: ".concat(details.url).concat(" | ").concat(details.method))
if ((details.method == "POST" && details.url.includes("new_round"))) {
console.log("new_round")
filterData(details)
}
if (details.method == "GET" && details.url.includes("get_last_round")) {
console.log("get_last_round")
filterData(details)
}
return {}
},
{ urls: ["https://*.agora-quiz.education/*"] },
["blocking"]
);