-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kovacs.js
43 lines (31 loc) · 1022 Bytes
/
kovacs.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
let state = {}
function updateCheckboxWithLocalState() {
document.getElementById('enabled').checked = state.enabled;
}
async function sendState() {
let queryOptions = { active: true, lastFocusedWindow: true };
let [tab] = await chrome.tabs.query(queryOptions);
if (tab == undefined) return;
chrome.tabs.sendMessage(tab.id, state);
}
async function syncWithUIState() {
let queryOptions = { active: true, lastFocusedWindow: true };
let [tab] = await chrome.tabs.query(queryOptions);
if (tab == undefined) return;
let response = await chrome.tabs.sendMessage(tab.id, "getState");
updateState(response.enabled)
}
function updateState( newEnabledValue ) {
state = {...state, enabled : newEnabledValue}
sync()
}
function sync() {
updateCheckboxWithLocalState()
sendState()
}
window.addEventListener('DOMContentLoaded', () => {
document.getElementById('enabled').addEventListener('change', (event) => {
updateState(event.currentTarget.checked)
})
syncWithUIState()
})