-
Notifications
You must be signed in to change notification settings - Fork 1
/
popup.js
96 lines (84 loc) · 3.14 KB
/
popup.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
const bg = chrome.extension.getBackgroundPage()
// bg variables: isRunning, timerTime
let addToWhitelist = document.getElementById('addToWhitelist');
let clearButton = document.getElementById('clearButton');
let startButton = document.getElementById('startButton');
let stopButton = document.getElementById('stopButton');
let resetButton = document.getElementById('resetButton');
let hours = document.querySelector('.hours');
let minutes = document.querySelector('.minutes');
let seconds = document.querySelector('.seconds');
let port = chrome.runtime.connect({name: "timer"});
addToWhitelist.onclick = function (element) {
chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
let query = (new URL(tabs[0].url)).searchParams.get("list");
if (query === "") {
addToWhitelist.innerText = "No Playlist";
} else {
chrome.storage.sync.get("whitelistOfPlaylists", (items) => {
if (items.whitelistOfPlaylists.includes(query)) {
addToWhitelist.innerText = "Playlist exists on whitelist.";
return;
}
chrome.storage.sync.set({ whitelistOfPlaylists: [...items.whitelistOfPlaylists, query]}, () => {
addToWhitelist.innerText = "Playlist is added";
});
});
}
chrome.storage.sync.get(null, (items) => {
console.log(items);
});
});
};
clearButton.onclick = function (element) {
chrome.storage.sync.set({whitelistOfPlaylists: []}, () => {
console.log("Cleared.");
});
}
startButton.onclick = function startTimer() {
port.postMessage({timer : "start"});
}
stopButton.onclick = function stopTimer() {
port.postMessage({ timer: "stop" });
}
resetButton.onclick = function resetTimer() {
port.postMessage({timer : "reset"})
hours.innerText = '00';
minutes.innerText = '00';
seconds.innerText = '00';
}
function pad(number) {
return (number < 10) ? '0' + number : number;
}
function stopwatchUpdate(newTime) {
const numberOfHours = Math.floor(newTime/3600);
const numberOfMinutes = Math.floor((newTime % 3600)/60);
const numberOfSeconds = Math.floor(newTime % 60);
hours.innerText = pad(numberOfHours);
minutes.innerText = pad(numberOfMinutes);
seconds.innerText = pad(numberOfSeconds);
}
port.onMessage.addListener(
function (message) {
if (message.newTime) {
stopwatchUpdate(message.newTime);
}
else if (message.warning == "tab closing") {
port.disconnect();
}
}
)
chrome.runtime.onMessage.addListener(
function(request, sender, sendResponse) {
console.log(sender.tab ?
"from a content script:" + sender.tab.url :
"from the extension");
if (request.warning == "blacklist") {
startButton.click();
sendResponse({farewell: "black start"});
} else if (request.warning == "whitelist") {
stopButton.click();
sendResponse({farewell: "white end"});
}
}
);