-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpopup.js
29 lines (27 loc) · 1.13 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
document.addEventListener('DOMContentLoaded', () => {
const toggle = document.getElementById('toggle');
const domainNameElement = document.querySelector('.domain_name');
// Get the current tab's URL and set the toggle switch state based on the stored value
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
const tab = tabs[0];
if (tab && tab.url && tab.url.startsWith('http')) {
const url = new URL(tab.url);
domainNameElement.textContent = url.hostname;
chrome.storage.sync.get([url.hostname], (result) => {
toggle.checked = !result[url.hostname];
});
// Update the Chrome storage and content script when the toggle switch state changes
toggle.addEventListener('change', () => {
chrome.tabs.query({ active: true, currentWindow: true }, (tabs) => {
const tab = tabs[0];
const url = new URL(tab.url);
chrome.storage.sync.set({ [url.hostname]: !toggle.checked }, () => {
chrome.tabs.sendMessage(tab.id, { action: 'replaceWords' });
});
});
});
} else {
toggle.disabled = true;
}
});
});