-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcontent_script.js
More file actions
executable file
·101 lines (94 loc) · 3.62 KB
/
Copy pathcontent_script.js
File metadata and controls
executable file
·101 lines (94 loc) · 3.62 KB
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
/**
* The start of the super cool browser extension.
* Note that the whole content script is wrapped in a function.
*/
(function () {
const d = new Date();
const n = d.getHours();
/**
* Here we have three bedtime variables.
* The first is the default, defaultBT, that comes preloaded with the extension.
* The second, messagedBT, is the bedtime sent in a message from the popup when a user changes the time there.
* The third, storedBT, is the bedtime that is then added to local storage from the popup.
* The storedBT should persist across tabs.
*/
let defaultBT = 23;
let messagedBT;
let storedBT;
/**
* Next two functions OnGot and OnError, as well as the gettingItem variable
* All deal with getting the bedtime from local storage.
*/
function onGot(item) {
storedBT = item.storedbedtime;
}
function onError(error) {
console.log(`Error: ${error}`);
}
let gettingItem = browser.storage.local.get();
gettingItem.then(onGot, onError)
.then(chooseBedtime);
function checkBedTime(messagedBT) {
/**
* We now check if the current time is past one's bed time.
* Note the special case if the time is past midnight and before 6 am.
* If it is past that time, we create our sleep message.
* We check to see if the div has already been created, to avoid duplicates.
* The sleep message is then made visible/hidden depending on the time.
*/
if (n >= messagedBT || n < 6) {
const sleepDiv = document.createElement("div");
sleepDiv.textContent = "It is " + n + " o'clock. Go to Sleep!!!!";
sleepDiv.className = 'sleepMessage';
sleepDiv.setAttribute("id", "goToSleep");
var gts = document.getElementById("goToSleep");
if (!gts) {
document.body.appendChild(sleepDiv);
} else {
gts.setAttribute("style", "visibility:visible");
}
} else {
/**
* Below we are hiding the sleepMessage div if it is later than the bedtime.
*/
document.getElementById("goToSleep").setAttribute("style", "visibility:hidden");
}
console.log("the bedtime checked here is " + messagedBT + " and the n used is " + n)
console.log(n <= 6)
console.log(n >= messagedBT)
console.log(messagedBT)
console.log(n >= messagedBT && n > 20)
}
/**
* Below is the logic defining which of the BTs to use (stored vs default.)
* If the storedBT is undefined, we use default
*/
function chooseBedtime() {
if (storedBT === undefined) {
console.log("the default BT is" + defaultBT)
checkBedTime(defaultBT)
} else {
checkBedTime(storedBT)
}
}
/**
* Below we are updaeted the storedBT variable with the BT received from the popup in the message.
* Note that we have have to parse the BT as an integer so it can be compared to current time.
*/
function updateBedTime(newBedTime) {
messagedBT = parseInt(newBedTime, 10);
browser.storage.local.set({
"storedbedtime": messagedBT
});
checkBedTime(messagedBT)
}
/**
* Below is the listener to listen for messages from the popup script.
* We then send whatever receive in the message to the updateBedTime function above.
*/
browser.runtime.onMessage.addListener((message) => {
if (message.command === "setbedtime") {
updateBedTime(message.newBedTime);
}
});
})();