-
Notifications
You must be signed in to change notification settings - Fork 0
/
background-script.js
97 lines (93 loc) · 3.82 KB
/
background-script.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
console.log("Background script initiated...");
let ports = [];
let timers = {};
const host = "http://127.0.0.1:8000";
function leftPad(num, width) {
let tmpNum = "0" + num;
return tmpNum.substr(tmpNum.length - (width - 1))
}
function formatDate(date) {
let year = date.getUTCFullYear();
let month = leftPad(date.getUTCMonth(), 2);
let day = leftPad(date.getUTCDay(), 2);
let hour = leftPad(date.getUTCHours(), 2);
let minute = leftPad(date.getUTCMinutes(), 2);
let second = leftPad(date.getUTCSeconds(), 2);
let ms = leftPad(date.getUTCMilliseconds(), 3);
return `${year}-${month}-${day} ${hour}:${minute}:${second}.${ms}`
}
function submitEvent(event) {
axios({
method: "post",
url: `${host}/attempt-events/`,
data: {
eventType: event.type,
questionName: event.questionName,
questionUrl: event.questionUrl,
timestamp: formatDate(new Date()),
user: "Logan"
}
}).then((response) => console.log(response));
}
browser.runtime.onConnect.addListener((port) => {
let url = port.name.replace(/^content\+\+|popup\+\+/g, "");
console.log(`URL: ${url}`);
ports[port.name] = port;
if (port.name.includes("content++")) {
console.log("Got a message from a content script");
console.log(`Stored connection for ${port.name}`);
port.onMessage.addListener((message) => {
if (message.event === "pageLoad") {
console.log(`Starting timer at ${Date.now()}`);
timers[url] = new easytimer.Timer();
timers[url].start();
console.log(`Time elapsed: ${timers[url].getTimeValues().toString()}`);
submitEvent({
questionName: message.questionName,
questionUrl: message.questionUrl,
type: "start",
user: message.user
});
}
});
} else { // port.name.includes("popup++")
console.log("Got a message from a popup");
console.log(`Stored connection for ${port.name}`);
port.onMessage.addListener((message) => {
if (message.event === "popupOpened") {
console.log("Popup was opened");
port.postMessage({timeValues: timers[url].getTimeValues(), startIt: true});
} else if (message.event === "pause") {
console.log("Pausing the timer");
timers[url].pause();
port.postMessage({timeValues: timers[url].getTimeValues(), startIt: false});
} else if (message.event === "play") {
if (timers[url].getTimeValues() === 0) {
submitEvent({
questionName: message.questionName,
questionUrl: message.questionUrl,
type: "start",
user: message.user
});
}
timers[url].start();
console.log("Playing the timer");
port.postMessage({timeValues: timers[url].getTimeValues(), startIt: true});
} else if (message.event === "stop") {
timers[url].stop();
console.log("Stopping the timer");
port.postMessage({timeValues: timers[url].getTimeValues(), startIt: false});
submitEvent({
questionName: message.questionName,
questionUrl: message.questionUrl,
type: "stop",
user: message.user
});
} else if (message.event === "reset") {
timers[url].reset();
console.log("Resetting the timer");
port.postMessage({timeValues: timers[url].getTimeValues(), startIt: true});
}
})
}
});