-
Notifications
You must be signed in to change notification settings - Fork 2
/
background.js
71 lines (55 loc) · 1.75 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
console.log("Starting ezfluent background...");
browser.webRequest.onBeforeRequest.addListener(
quizListener,
{urls: ["*://portal.gofluent.com/mobile-rest/ws/quiz"]},
["blocking"]
);
browser.webRequest.onBeforeRequest.addListener(
articleListener,
{urls: ["*://portal.gofluent.com/mobile-rest/ws/trainingpath/tp/assigned/*"]},
["blocking"]
);
function quizListener(details) {
let filter = browser.webRequest.filterResponseData(details.requestId);
let decoder = new TextDecoder("utf-8");
let encoder = new TextEncoder();
let data = '';
filter.ondata = (event) => {
const str = decoder.decode(event.data, {stream: true});
data += str;
filter.write(encoder.encode(str));
}
filter.onstop = (event) => {
filter.close();
var querying = browser.tabs.query({
active: true,
currentWindow: true
});
querying.then((tabs) => {
browser.tabs.sendMessage(tabs[0].id, JSON.stringify({type: 'quiz', data}));
});
}
return;
}
function articleListener(details) {
let filter = browser.webRequest.filterResponseData(details.requestId);
let decoder = new TextDecoder("utf-8");
let encoder = new TextEncoder();
let data = '';
filter.ondata = (event) => {
const str = decoder.decode(event.data, {stream: true});
data += str;
filter.write(encoder.encode(str));
}
filter.onstop = (event) => {
filter.close();
var querying = browser.tabs.query({
active: true,
currentWindow: true
});
querying.then((tabs) => {
browser.tabs.sendMessage(tabs[0].id, JSON.stringify({type: 'articles', data}));
});
}
return;
}