-
Notifications
You must be signed in to change notification settings - Fork 1
/
popup.js
62 lines (53 loc) · 1.72 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
let toggleButton = document.getElementById("toggle");
// handle messages between popup and content
const handleStateMessage = (state) => {
if (state === 'disabled') {
toggleButton.classList.add('disabled');
toggleButton.classList.remove('enabled');
console.log(toggleButton.classList)
sendState("disabled");
toggleButton.textContent = "Enable Zappy";
} else if (state === 'enabled') {
toggleButton.classList.add('enabled');
toggleButton.classList.remove('disabled');
sendState("enabled");
toggleButton.textContent = "Disable Zappy";
}
}
const askState = () => {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
chrome.tabs.sendMessage(tabs[0].id, {
request: 'request'
}, (response) => {
handleStateMessage(response.state)
console.log('this is askState');
});
});
}
askState();
chrome.runtime.onMessage.addListener((message) => {
handleStateMessage(message.state);
console.log('this is the listener')
});
const sendState = (currentState) => {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
chrome.tabs.sendMessage(tabs[0].id, {
state: currentState
});
});
}
// handle click of popup button
toggleButton.onclick = () => {
if (toggleButton.classList.contains('disabled')) {
toggleButton.classList.remove('disabled');
toggleButton.classList.add('enabled');
console.log(toggleButton.classList);
sendState("enabled");
toggleButton.textContent = "Disable Zappy";
} else if (toggleButton.classList.contains('enabled')) {
toggleButton.classList.add('disabled');
toggleButton.classList.remove('enabled');
sendState("disabled");
toggleButton.textContent = "Enable Zappy";
}
};