-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpopup.js
111 lines (93 loc) · 2.95 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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
document.addEventListener('DOMContentLoaded', init);
const _ = {
items: [],
tableBody: undefined,
saveBtn: undefined,
};
async function init() {
_.tableBody = document.getElementById('table-body');
const config = await loadConfig();
if (config) {
_.items = [...(config.items ?? []), {}];
populateForm();
}
if (!_.items.length) {
addPlaceholderRow();
}
}
function populateForm() {
_.tableBody.innerHTML = '';
_.items.forEach((item, index) => {
const node = createConfigHtmlElement(item, index);
_.tableBody.appendChild(node);
});
}
function createConfigHtmlElement(item, index) {
const urlInput = document.createElement('input');
urlInput.value = item.url ?? '';
urlInput.onkeyup = handleKeyUp;
urlInput.placeholder = 'URL pattern';
const urlTd = document.createElement('td');
urlTd.appendChild(urlInput);
const textInput = document.createElement('input');
textInput.value = item.text ?? '';
textInput.onkeyup = handleKeyUp;
textInput.placeholder = 'Prefix text';
const textTd = document.createElement('td');
textTd.appendChild(textInput);
const deleteBtn = document.createElement('button');
deleteBtn.addEventListener('click', () => deleteItem(index));
const deleteTd = document.createElement('td');
deleteTd.appendChild(deleteBtn);
const tr = document.createElement('tr');
tr.appendChild(urlTd);
tr.appendChild(textTd);
tr.appendChild(deleteTd);
return tr;
}
async function loadConfig() {
return new Promise(resolve => {
chrome.storage.sync.get("config", ({ config }) => {
resolve(config);
});
});
}
function handleKeyUp() {
saveConfig();
addPlaceholderRow();
}
function saveConfig() {
const config = getValuesFromFormFields();
chrome.storage.sync.set({config: { items: config }});
_.items = config;
}
function getValuesFromFormFields() {
return [...(_.tableBody.children ?? [])].map(tr => {
const [urlInput, textInput] = tr.getElementsByTagName('input');
return {
url: urlInput.value,
text: textInput.value
};
}).filter(item => item.url || item.text);
}
function hasPlaceHolderItem() {
return [...(_.tableBody.children ?? [])].some(tr => {
const inputFields = [...(tr.querySelectorAll('input') ?? [])];
return inputFields.every(inputField => !inputField.value);
});
}
function addPlaceholderRow() {
if (!hasPlaceHolderItem()) {
const placeHolderItem = {};
_.items.push(placeHolderItem);
const node = createConfigHtmlElement(placeHolderItem, _.tableBody.children.length);
_.tableBody.appendChild(node);
}
}
function deleteItem(index) {
const inputFields = [...(_.tableBody.children ?? [])][index]?.querySelectorAll('input');
inputFields?.forEach(element => element.value = '');
saveConfig();
populateForm();
addPlaceholderRow();
}