-
Notifications
You must be signed in to change notification settings - Fork 0
/
pop-up.js
55 lines (43 loc) · 1.37 KB
/
pop-up.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
'use strict';
function getCurrentWindow(cb) {
chrome.windows.getCurrent((win) => {
cb(win);
});
}
function clearCustomInterval() {
chrome.browserAction.setBadgeText({ text: '' });
chrome.extension.sendRequest({ startInterval: false });
window.close();
}
function setCustomInterval(countryCode) {
getCurrentWindow((win) => {
chrome.browserAction.setBadgeText({ text: 'ON' });
chrome.extension.sendRequest({ startInterval: true, windowId: win.id, countryCode: countryCode });
window.close();
});
}
function getStatHatStatus() {
return localStorage.statHatStatus;
}
function setStatHatStatus(elem) {
localStorage.statHatStatus = (getStatHatStatus() === 'ON') ? 'OFF' : 'ON';
}
function setInnerHTML(elem) {
elem.innerHTML = (getStatHatStatus() === 'ON') ? 'OFF' : 'ON';
}
function getCountryCode() {
let elem = document.getElementById('country-code');
if (!elem.value) return "00";
return String(elem.value).toUpperCase().substring(0, 2);
}
function buttonClicked(event) {
setStatHatStatus(event.target);
setInnerHTML(event.target);
(getStatHatStatus() === 'ON') ? setCustomInterval(getCountryCode()) : clearCustomInterval();
}
function contentLoaded(event) {
let elem = document.getElementById('toggle-btn');
setInnerHTML(elem);
elem.addEventListener('click', buttonClicked);
}
document.addEventListener('DOMContentLoaded', contentLoaded);