-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpopup.js
More file actions
70 lines (66 loc) · 2.58 KB
/
popup.js
File metadata and controls
70 lines (66 loc) · 2.58 KB
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
document.addEventListener('DOMContentLoaded', () => {
const enabledEl = document.getElementById('enabled');
const applyBtn = document.getElementById('apply');
const ACTION_RUN = 'runreplacements';
function sendRunMessageToGovTabs() {
chrome.tabs.query({ url: '*://*.gov/*' }, (tabs) => {
for (const t of tabs) chrome.tabs.sendMessage(t.id, { action: ACTION_RUN });
});
}
// Load current enabled state
chrome.storage.sync.get(['enabled'], (res) => {
enabledEl.checked = res.enabled === true;
});
enabledEl.addEventListener('change', () => {
const enabled = enabledEl.checked;
// Save enabled state
chrome.storage.sync.set({ enabled });
if (enabled) {
// If there are no custom phrases saved, populate defaults so enabling immediately works
chrome.storage.sync.get(['customPhrases'], (res) => {
const has = Array.isArray(res.customPhrases) && res.customPhrases.length;
if (!has) {
const url = chrome.runtime.getURL('defaults.json');
fetch(url).then(r => r.json()).then(defs => {
if (Array.isArray(defs) && defs.length) {
chrome.storage.sync.set({ customPhrases: defs }, () => {
// notify tabs to re-run replacements
sendRunMessageToGovTabs();
});
}
}).catch(() => {
// Still notify tabs to run if any custom phrases exist
sendRunMessageToGovTabs();
});
} else {
// already have custom phrases, just notify tabs
sendRunMessageToGovTabs();
}
});
} else {
// If disabling, notify tabs to clear replacements if needed (no-op here)
}
});
applyBtn.addEventListener('click', () => {
const url = chrome.runtime.getURL('defaults.json');
fetch(url).then(r => r.json()).then(defs => {
if (Array.isArray(defs)) {
chrome.storage.sync.set({ customPhrases: defs }, () => {
const lbl = applyBtn.querySelector('.btn-label');
if (lbl) { lbl.textContent = 'Applied'; setTimeout(() => lbl.textContent = 'Apply defaults', 1200); }
});
}
}).catch(() => {
const lbl = applyBtn.querySelector('.btn-label');
if (lbl) { lbl.textContent = 'Failed'; setTimeout(() => lbl.textContent = 'Apply defaults', 1200); }
});
});
const openLink = document.getElementById('openOptions');
if (openLink) {
openLink.addEventListener('click', (e) => {
e.preventDefault();
try { chrome.runtime.openOptionsPage(); } catch (err) { window.open('options.html'); }
window.close();
});
}
});