-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
44 lines (36 loc) · 993 Bytes
/
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
'use strict';
function getCurrentTabUrl(callback) {
var queryInfo = {
active: true,
currentWindow: true
};
chrome.tabs.query(queryInfo, (tabs) => {
var tab = tabs[0];
console.assert(typeof tab.url == 'string', 'tab.url should be a string');
var url = new URL(tab.url);
callback(url.hostname);
});
}
function getSavedMode(url, callback) {
chrome.storage.sync.get(url, (items) => {
callback(chrome.runtime.lastError ? null : items[url]);
});
}
function saveMode(url, color) {
var items = {};
items[url] = color;
chrome.storage.sync.set(items);
}
document.addEventListener('DOMContentLoaded', () => {
getCurrentTabUrl((url) => {
var switcher = document.getElementById('switcher');
getSavedMode(url, (savedMode) => {
switcher.checked = savedMode;
});
switcher.addEventListener('change', (e) => {
saveMode(url, e.target.checked);
chrome.tabs.reload();
setTimeout(window.close, 1000);
});
});
});