-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathoptions.js
More file actions
79 lines (72 loc) · 2.89 KB
/
options.js
File metadata and controls
79 lines (72 loc) · 2.89 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
71
72
73
74
75
76
77
78
79
// Options page logic for Escape Hatch
(function () {
'use strict';
const enabledEl = document.getElementById('enabled');
const listEl = document.getElementById('list');
const addBtn = document.getElementById('add');
const saveBtn = document.getElementById('save');
const resetBtn = document.getElementById('reset');
function createRow(entry) {
const wrapper = document.createElement('div');
wrapper.className = 'phrase';
const find = document.createElement('input');
find.type = 'text';
find.placeholder = 'Find phrase';
find.value = entry && (entry.find || '') || '';
const replace = document.createElement('input');
replace.type = 'text';
replace.placeholder = 'Replace with';
replace.value = entry && (entry.replace || '') || '';
const del = document.createElement('button');
del.textContent = 'Remove';
del.addEventListener('click', () => wrapper.remove());
wrapper.appendChild(find);
wrapper.appendChild(replace);
wrapper.appendChild(del);
return wrapper;
}
function load() {
chrome.storage.sync.get(['enabled', 'customPhrases'], (res) => {
enabledEl.checked = res.enabled === true;
listEl.innerHTML = '';
let custom = Array.isArray(res.customPhrases) ? res.customPhrases : [];
// If there are no saved custom phrases, attempt to fetch defaults.json and pre-populate inputs
if (!custom.length) {
const url = chrome.runtime.getURL('defaults.json');
fetch(url).then(r => r.json()).then(defs => {
if (Array.isArray(defs) && defs.length) {
custom = defs.map(p => ({ find: p.find, replace: p.replace }));
}
for (const p of custom) listEl.appendChild(createRow(p));
}).catch(() => {
// On error, leave the list empty
});
} else {
for (const p of custom) listEl.appendChild(createRow(p));
}
});
}
function save() {
const nodes = Array.from(listEl.querySelectorAll('.phrase'));
const custom = nodes.map(n => {
const inputs = n.querySelectorAll('input[type="text"]');
return { find: inputs[0].value.trim(), replace: inputs[1].value.trim() };
}).filter(p => p.find && p.replace);
chrome.storage.sync.set({ enabled: enabledEl.checked, customPhrases: custom }, () => {
// notify
saveBtn.textContent = 'Saved';
setTimeout(() => saveBtn.textContent = 'Save', 1200);
});
}
addBtn.addEventListener('click', () => listEl.appendChild(createRow()));
saveBtn.addEventListener('click', save);
resetBtn.addEventListener('click', () => {
// reset custom phrases to empty and leave disabled by default
chrome.storage.sync.set({ enabled: false, customPhrases: [] }, load);
});
// Initialize options UI
document.addEventListener('DOMContentLoaded', load);
function escapeHtml(s) {
return s.replace(/&/g, '&').replace(/</g, '<').replace(/>/g, '>');
}
})();