-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
96 lines (81 loc) · 3.79 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
document.addEventListener('DOMContentLoaded', function() {
const powerSwitch = document.querySelector('.power-switch input');
const toggles = document.querySelectorAll('.toggle');
let currentTab = null;
// Get the current tab
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
currentTab = tabs[0];
updateUI();
});
function areAnyTogglesOn(settings) {
return Object.values(settings).some(value => value);
}
function updatePowerSwitchState(settings) {
powerSwitch.checked = areAnyTogglesOn(settings);
}
function updateUI() {
if (currentTab && currentTab.url) {
const url = new URL(currentTab.url);
const hostname = url.hostname;
chrome.storage.sync.get('siteSettings', function(data) {
const allSiteSettings = data.siteSettings || {};
const settings = allSiteSettings[hostname] || { history: false, cache: false, cookies: false };
toggles.forEach(toggle => {
const feature = toggle.getAttribute('data-feature');
toggle.classList.toggle('toggle--on', settings[feature]);
toggle.classList.toggle('toggle--off', !settings[feature]);
});
updatePowerSwitchState(settings);
});
}
}
function saveSettings(hostname, settings) {
chrome.storage.sync.get('siteSettings', function(data) {
const allSiteSettings = data.siteSettings || {};
allSiteSettings[hostname] = settings;
chrome.storage.sync.set({ siteSettings: allSiteSettings }, function() {
chrome.runtime.sendMessage({ action: 'updateSettings', hostname: hostname, settings: settings });
});
});
}
toggles.forEach(function(toggle) {
toggle.addEventListener('click', function(e) {
e.preventDefault();
this.classList.toggle('toggle--on');
this.classList.toggle('toggle--off');
this.classList.add('toggle--moving');
setTimeout(() => {
this.classList.remove('toggle--moving');
}, 200);
const feature = this.getAttribute('data-feature');
const isEnabled = this.classList.contains('toggle--on');
console.log(`${feature} is now ${isEnabled ? 'on' : 'off'}`);
if (currentTab && currentTab.url) {
const url = new URL(currentTab.url);
const hostname = url.hostname;
chrome.storage.sync.get('siteSettings', function(data) {
const allSiteSettings = data.siteSettings || {};
const settings = allSiteSettings[hostname] || { history: false, cache: false, cookies: false };
settings[feature] = isEnabled;
saveSettings(hostname, settings);
updatePowerSwitchState(settings);
});
}
});
});
powerSwitch.addEventListener('click', function(event) {
const newState = this.checked;
if (currentTab && currentTab.url) {
const url = new URL(currentTab.url);
const hostname = url.hostname;
chrome.storage.sync.get('siteSettings', function(data) {
const allSiteSettings = data.siteSettings || {};
const settings = allSiteSettings[hostname] || { history: false, cache: false, cookies: false };
Object.keys(settings).forEach(key => settings[key] = newState);
saveSettings(hostname, settings);
updateUI();
});
}
});
updateUI();
});