-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathui.js
285 lines (251 loc) · 7.98 KB
/
ui.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
'use strict';
let INTERVAL = null;
let currentScanLength = 0;
const excludeList = new Map();
const LOCAL_URL_PREFIXES = ["192.168.","127.0.","10.0.0.","10.10.1.","10.1"];
const SUPPORTED_PROPERTIES = ["url","title"];
const DQ = document.querySelector.bind(document);
function messageHandler(request,sender,sendResponse){
if(sender.id != browser.runtime.id || sender.envType != "addon_child"){
return
}
let button = DQ("#almostUpdateButton");
switch(request.type){
case "scan":
currentScanLength = request.length;
if(request.success){
button.removeAttribute("disabled");
setStatus("Scan complete",false,"Number of matching bookmarks");
DQ("#domainText").textContent = request.domain || "";
browser.runtime.sendMessage({operation:"list"})
// background will send the list soon after
}else{
button.setAttribute("disabled","true");
setStatus("Scan failed");
}
DQ("#scanButton").textContent = "Scan bookmarks";
break;
case "update":
if(request.success){
setStatus(`Update success:${request.length} bookmarks were updated`," ");
}else{
let fails = request.failures ? request.failures.length : 0;
setStatus(`Update finished with ${request.length - fails} success and ${request.failures.length} failures`," ","Failed operations");
listFailures({list:request.failures});
}
button.setAttribute("disabled","true");
DQ("#scanButton").removeAttribute("disabled");
clearInterval(INTERVAL);
break;
case "list":
listBookmarks(request);
break;
default:
return
}
}
function createListItem(bm,isChecked){
function createCheckbox(bm){
const checkbox = document.createElement("input");
checkbox.type = "checkbox";
checkbox.checked = !!isChecked;
checkbox.setAttribute("data-id",bm.id);
return checkbox
}
let container = document.createElement("div");
container.classList.add("listItem");
if(bm.hasOwnProperty("error")){
container.appendChild(document.createElement("div")).textContent = bm.error;
return container
}
container.appendChild(createCheckbox(bm));
for(let prop in bm){
if(SUPPORTED_PROPERTIES.includes(prop)){
let t = document.createElement("div");
t.appendChild(document.createElement("code")).textContent = "--";
t.append(bm[prop].match);
if(bm[prop].hasOwnProperty("replacement")){
t.appendChild(document.createElement("br"));
t.appendChild(document.createElement("code")).textContent = "++";
t.append(bm[prop].replacement);
}
container.appendChild(t)
}
}
return container
}
function listFailures(request){
let listParent = DQ("#bmList");
let odd = true;
while(listParent.children.length > 0){
listParent.removeChild(listParent.children[0]);
}
for(let fail of request.list){
let item = listParent.appendChild(createListItem(fail,false));
odd && item.classList.add("odd");
odd = !odd;
}
document.body.setAttribute("style",`--bmb-bookmark-count:'${request.list.length}'`);
}
function listBookmarks(request){
excludeList.clear();
function shouldBMBeChecked(bm){
if(!bm.url){
return true
}
switch(request.operation){
case "protocol":
return LOCAL_URL_PREFIXES.every(url=>!bm.url.match.startsWith(`http://${url}`))
case "regexp":
return !bm.url.match.startsWith("javascript:") && !bm.url.match.startsWith("data:") ;
}
return true
}
let listParent = DQ("#bmList");
let odd = true;
while(listParent.children.length > 0){
listParent.removeChild(listParent.children[0]);
}
for(let bm of request.list){
let enabled = shouldBMBeChecked(bm);
if(!enabled){
excludeList.set(bm.id,true);
}
let item = listParent.appendChild(createListItem(bm,enabled));
odd && item.classList.add("odd");
odd = !odd;
}
document.body.setAttribute("style",`--bmb-bookmark-count:'${request.list.length - excludeList.size}'`);
}
function setStatus(message,progress,listContext){
if(message){
DQ("#messageBox").textContent = message;
}
if(progress){
DQ("#progressBox").textContent = progress;
}
if(listContext){
DQ("#nBookmarks").textContent = listContext;
}
}
function selectType(){
return DQ(".radio:checked").value
}
function requestScan(e){
e.target.textContent = "Scanning...";
let type = selectType();
let op = {operation:"scan",properties:{type:type}};
switch(type){
case "domain":
op.properties.toDomain = String(DQ("#domainReplace").value) || null;
case "protocol":
op.properties.fromDomain = String(DQ("#domainFilter").value) || null;
break;
case "regexp":
op.properties.fromDomain = String(DQ("#regexpURLFilter").value);
op.properties.toDomain = String(DQ("#regexpURLReplace").value);
op.properties.fromTitle = String(DQ("#regexpTitleFilter").value);
op.properties.toTitle = String(DQ("#regexpTitleReplace").value);
break;
default:
op.properties.type = null;
}
browser.runtime.sendMessage(op)
.then(
(response)=>{
if(response.ok){
setStatus("Please wait");
}else{
e.target.textContent = "Scan Bookmarks";
setStatus(`Error: ${response.message}`);
listBookmarks({list:[]});
document.body.setAttribute("style","--bmb-bookmark-count:'0'");
}
},
(error)=>(setStatus(`Error: ${error}`))
);
}
function initView(state){
setStatus(state.message,state.progress);
DQ("#scanButton").setAttribute("disabled","true");
}
async function statusCheck(){
browser.runtime.sendMessage({operation:"status"})
.then(
(message)=>(message.busy && setStatus("Progress: ",message.progress))
)
}
function constructExcludeList(){
excludeList.clear();
let bookmarkList = Array.from(DQ("#bmList").children);
for(let row of bookmarkList){
let el = row.children[0];
if(el.tagName != "INPUT"){ continue }
if(!el.checked){
let id = el.getAttribute("data-id");
id && excludeList.set(id,true)
}
}
return list
}
function onCheckboxToggle(ev){
let target = ev.target;
let dataid = target.getAttribute("data-id");
if(dataid){
if(target.checked){
excludeList.delete(dataid)
}else{
excludeList.set(dataid,true)
}
document.body.setAttribute("style",`--bmb-bookmark-count:'${currentScanLength - excludeList.size}'`);
}
}
function requestUpdate(e){
browser.runtime.sendMessage({
operation:"update",
excludes:Array.from(excludeList.keys()),
allowUrlFixup:DQ("#fixupCheckbox").checked
})
.then(
(response)=>{
if(response.ok){
initView(response);
INTERVAL = setInterval(statusCheck,300)
}else{
setStatus(`Error:${response.message}`);
DQ("#almostUpdateButton").setAttribute("disabled","true");
}
},
(error)=>(setStatus(`something went wrong: ${error}`))
)
.finally(()=>(DQ("#updateWarning").classList.add("hidden")))
}
function showUpdateButton(){
DQ("#updateWarning").classList.remove("hidden")
}
document.onreadystatechange = function () {
if (document.readyState === "complete") {
DQ("#scanButton").addEventListener("click",requestScan);
DQ("#updateButton").addEventListener("click",requestUpdate);
DQ("#almostUpdateButton").addEventListener("click",showUpdateButton);
DQ("#bmList").addEventListener("change",onCheckboxToggle);
document.querySelectorAll(".radio").forEach(
(r) => {
r.addEventListener("change",
() => (r.checked&&DQ("#almostUpdateButton").setAttribute("disabled","true")))
}
);
browser.runtime.onMessage.addListener(messageHandler);
// Ask status from background
browser.runtime.sendMessage({operation:"status"})
.then( (state) => {
if(state.busy){
initView(state);
INTERVAL = setInterval(statusCheck,300)
}
});
window.addEventListener("unload",function(e){
browser.runtime.sendMessage({operation:"reset"})
})
}
}