-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
75 lines (61 loc) · 1.93 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
let selector = document.getElementById('selector');
let pause = document.getElementById('pause');
let button_play = document.getElementById('button_play');
chrome.storage.sync.get('cssdog_selector', function(data) {
selector.setAttribute('value', data && data.cssdog_selector ? data.cssdog_selector : "");
});
chrome.storage.sync.get('cssdog_pause', function(data) {
pause.setAttribute('value', data && data.cssdog_pause ? data.cssdog_pause : 5000);
});
function updatePlayPause() {
chrome.storage.sync.get('cssdog_active', function(data) {
if (data) {
button_play.className = "btn fa " + ( data.cssdog_active ? "fa-pause" : "fa-play" );
}
});
}
// execute content.js, which will start a loop
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
chrome.tabs.executeScript(tabs[0].id, {file: 'content.js'});
});
// if the user changes the selector and/or pause, pass them via chrome.storage to content.js
selector.addEventListener("change", function() {
setSelector(selector.value);
});
function setSelector(selector) {
chrome.storage.sync.set({'cssdog_selector': selector}, function() {
setActive(true);
if (chrome.runtime.error) {
console.log("Runtime error.");
}
});
}
pause.addEventListener("change", function() {
setPause(pause.value);
});
function setPause(pause) {
chrome.storage.sync.set({'cssdog_pause': pause}, function() {
setActive(true);
if (chrome.runtime.error) {
console.log("Runtime error.");
}
});
}
// toggle activity
button_play.addEventListener("click", function() {
playPause();
});
function playPause() {
chrome.storage.sync.get('cssdog_active', function(data) {
setActive(!data.cssdog_active); // toggle state
});
}
function setActive(active) {
chrome.storage.sync.set({'cssdog_active': active}, function() {
updatePlayPause();
if (chrome.runtime.error) {
console.log("Runtime error.");
}
});
}
updatePlayPause();