-
Notifications
You must be signed in to change notification settings - Fork 11
/
popup.js
111 lines (101 loc) · 3.4 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// popup.js
// persistence when out of focus but terminated when closed
// variables to represent items from html doc
var input_s = document.getElementById("input-sec");
var input_m = document.getElementById("input-min");
var ss_button = document.getElementById("ss_button");
// logic specific variables
var refresh_interval = 0;
var ss_state = false;
// passes message to background.js with the message to reload
function reload() {
chrome.runtime.sendMessage({message: "reload"});
}
// button mouseover logic := display hover images
function ss_mouseoverHandler(e) {
if(ss_state == false)
ss_button.style.backgroundImage = 'url("images/go-hover.png")';
else
ss_button.style.backgroundImage = 'url("images/stop-hover.png")';
}
// button mouseleave logic := display non-hover images
function ss_mouseleaveHandler(e) {
if(ss_state == false)
ss_button.style.backgroundImage = 'url("images/go.png")';
else
ss_button.style.backgroundImage = 'url("images/stop.png")';
}
function ss_clickHandler(e) {
if(ss_state === false){
// retrieve interval value from input fields
refresh_interval = (parseInt(input_m.value)*60)+parseInt(input_s.value);
// value must be greater than 0 to continue
if(refresh_interval !== 0) {
// change image to represent new state
ss_button.style.backgroundImage = 'url("images/stop-hover.png")';
// initiate countdown
countdown(refresh_interval);
// set state flag to true
ss_state = true;
}
else {
alert('Enter an amoung greater than zero to start');
}
}
else{ // ss_state == true
chrome.runtime.sendMessage({message: "stop clicked!"});
// replace image
ss_button.style.backgroundImage = 'url("images/go-hover.png")';
// set state to false to keep page from refreshing
ss_state = false;
// erase badge
chrome.browserAction.setBadgeText({text:''});
// refresh extension to erase previous intervals (which will trigger 'resetAll()')
window.location.reload(true);
}
}
// use to reset all relevant vars
function resetAll(){
input_s.innerHTML = 0;
input_m.innerHTML = 0;
refresh_interval = 0;
ss_state = false;
chrome.browserAction.setBadgeText({text:''});
}
// countdown logic := count to zero, when zero is met reset timer and refresh page
function countdown(time){
var t = time;
var x = setInterval((function(){
setInterval(function() {
// only carry on if in active state
if(ss_state === true){
// set badge text
chrome.browserAction.setBadgeText({text:''+t});
// decerement time
t--;
// if time is below zero reset counter and refresh page
if(t<0){
reload();
t = time;
}
}
else { // ss_state == false
// clear previously set timing interval
clearInterval(x);
// remove badge from icon
chrome.browserAction.setBadgeText({text:''});
}
}, 1000);
})(), 0);
}
// add event listeners after DOM has fully loaded (`DOMContentLoaded`)
document.addEventListener('DOMContentLoaded', function () {
// reset all global vars and badge
resetAll();
// start-stop button click listener
ss_button.addEventListener('click', ss_clickHandler);
// start-stop button mouseover listener
ss_button.addEventListener('mouseover', ss_mouseoverHandler);
// start-stop button mouseleave listener
ss_button.addEventListener('mouseleave', ss_mouseleaveHandler);
});