-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
36 lines (32 loc) · 1.41 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
document.addEventListener('DOMContentLoaded', function() {
const toggleSwitch = document.getElementById('enablePaste');
function updateToggleState(isEnabled) {
toggleSwitch.checked = isEnabled;
}
function handleToggleChange() {
const isEnabled = toggleSwitch.checked;
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
const activeTabUrl = tabs[0].url;
chrome.storage.sync.set({ pasteEnabled: isEnabled, enabledUrl: activeTabUrl }, function() {
chrome.runtime.sendMessage({ action: 'storageUpdated' });
chrome.tabs.sendMessage(tabs[0].id, {
action: 'updatePasteState',
isEnabled: isEnabled
});
});
});
}
chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
const activeTabUrl = tabs[0].url;
chrome.storage.sync.get(['pasteEnabled', 'enabledUrl'], function(data) {
updateToggleState((data.enabledUrl === activeTabUrl) && data.pasteEnabled);
toggleSwitch.addEventListener('change', handleToggleChange);
});
});
// Listen for updates from background script
chrome.runtime.onMessage.addListener(function(request, sender, sendResponse) {
if (request.action === 'updatePopupState') {
updateToggleState(request.isEnabled);
}
});
});