-
Notifications
You must be signed in to change notification settings - Fork 2
/
options.js
708 lines (592 loc) · 18.3 KB
/
options.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
const PREFIX_PRESET = "preset-"; // Must match the one in common.js
const PREFIX_EDIT = "edit-";
const PREFIX_RESTORE = "restore-";
const PREFIX_REMOVE = "remove-";
const PREFIX_SAVE = "save-";
const PREFIX_CANCEL = "cancel-";
const PREFIX_DOWN = "down-"
const PREFIX_UP = "up-"
const MODE_NEW = "new";
const MODE_EDIT = "edit";
const OVERSIZE_ALLOWANCE_DEFAULT = 1.1;
var keepActionMenuOnClick = false;
var oversizeAllowance = OVERSIZE_ALLOWANCE_DEFAULT;
function makeID()
{
// This is not *perfect* but works as long as it's not called multiple times in the same millisecond (o).(O)
return new Date().valueOf();
}
// hasClass, addClass, removeClass functions borrowed (and reformatted) from: https://stackoverflow.com/questions/6787383
function hasClass(ele,cls)
{
return !!ele.className.match(new RegExp('(\\s|^)'+cls+'(\\s|$)'));
}
function addClass(ele,cls)
{
if(!hasClass(ele,cls))
{
ele.className += " " + cls;
}
}
function removeClass(ele,cls)
{
if (hasClass(ele,cls))
{
let reg = new RegExp('(\\s|^)'+cls+'(\\s|$)');
ele.className = ele.className.replace(reg,' ');
}
}
function extend(a, b)
{
for(let key in b)
{
if(b.hasOwnProperty(key))
{
a[key] = b[key];
}
}
return a;
}
Array.prototype.move = function(from, to) {
if(from >=0 && from < this.length && to >= 0 && to < this.length)
{
this.splice(to, 0, this.splice(from, 1)[0]);
}
};
function getId(fullString,prefixToRemove)
{
return parseInt(fullString.substring(prefixToRemove.length));
}
function getValues(mode)
{
let hasError = false;
let eW = document.querySelector("#" + mode + "Width");
let width = parseInt(eW.value);
if(Number.isInteger(width) && width > 0 && width <= screen.availWidth * oversizeAllowance)
{
removeClass(eW,"invalid");
}
else
{
hasError = true;
addClass(eW,"invalid");
}
let eH = document.querySelector("#" + mode + "Height");
let height = parseInt(eH.value);
if(Number.isInteger(height) && height > 0 && height <= screen.availHeight * oversizeAllowance)
{
removeClass(eH,"invalid");
}
else
{
hasError = true;
addClass(eH,"invalid");
}
let eN = document.querySelector("#" + mode + "Name");
let name = eN.value;
let result = {"valid": !hasError};
let eS = document.querySelector("#" + mode + "Status");
if(hasError)
{
eS.textContent = "Please correct errors";
addClass(eS,"errmsg");
}
else
{
eW.value = "";
eH.value = "";
eN.value = "";
eS.textContent = "";
removeClass(eS,"errmsg");
result.width = width;
result.height = height;
result.name = name;
}
return result;
}
async function loadOptions()
{
let storage = await browser.storage.local.get("options");
let options = storage.options;
keepActionMenuOnClick = options.keepActionMenuOnClick !== undefined && options.keepActionMenuOnClick;
document.querySelector("#keepActionMenuOnClick").checked = keepActionMenuOnClick;
}
async function loadAdvancedSettings()
{
let storage = await browser.storage.local.get("advanced");
let advanced = storage.advanced;
if(advanced.oversizeAllowance !== undefined)
{
oversizeAllowance = advanced.oversizeAllowance;
}
document.querySelector("#oversizeAllowance").value = oversizeAllowance;
}
async function displayPresets()
{
let storage = await browser.storage.local.get("presets");
let presets = storage.presets;
let existingRows = document.querySelectorAll(".presetItem");
for(let row of existingRows)
{
console.log("(displayPreset) Remove item row");
row.remove();
};
let presetCount = presets.length;
for(let i = 0;i < presetCount;i++)
{
let presetNum = i + 1;
let isFirst = (i == 0);
let isLast = (i == presetCount - 1);
console.log("(displayPreset) Add item " + presetNum + ": " + presets[i].name);
let myId = presets[i].id;
// Make the row, the annoying but safe way
let tr = document.createElement("tr");
tr.className = "presetItem";
let tdP = document.createElement("td");
let imgDown = document.createElement("img");
imgDown.width = 12;
imgDown.height = 12;
if(isLast)
{
imgDown.src = "images/arrow_none.png";
}
else
{
imgDown.src = "images/arrow_down.png";
imgDown.title = "Move Down";
imgDown.id = PREFIX_DOWN + i;
addClass(imgDown,"imgDown");
imgDown.addEventListener("click", moveDown);
}
tdP.appendChild(imgDown);
tdP.appendChild(document.createTextNode(" "));
let imgUp = document.createElement("img");
imgUp.width = 12;
imgUp.height = 12;
if(isFirst)
{
imgUp.src = "images/arrow_none.png";
}
else
{
imgUp.src = "images/arrow_up.png";
imgUp.title = "Move Up";
imgUp.id = PREFIX_UP + i;
addClass(imgUp,"imgUp");
imgUp.addEventListener("click", moveUp);
}
tdP.appendChild(imgUp);
tr.appendChild(tdP);
let tdK = document.createElement("td");
tdK.textContent = await browser.extension.getBackgroundPage().getShortcutKey(PREFIX_PRESET + presetNum);
tr.appendChild(tdK);
let tdW = document.createElement("td");
tdW.textContent = presets[i].width;
tr.appendChild(tdW);
let tdH = document.createElement("td");
tdH.textContent = presets[i].height;
tr.appendChild(tdH);
let tdN = document.createElement("td");
tdN.textContent = presets[i].name;
tr.appendChild(tdN);
let tdA = document.createElement("td");
let btnE = document.createElement("button");
btnE.type = "button";
btnE.id = PREFIX_EDIT + myId;
btnE.textContent = "Edit";
btnE.addEventListener("click", editPreset);
tdA.appendChild(btnE);
let btnR = document.createElement("button");
btnR.type = "button";
btnR.id = PREFIX_REMOVE + myId;
btnR.textContent = "Remove";
btnR.addEventListener("click", removePreset);
tdA.appendChild(btnR);
tr.appendChild(tdA);
tr.appendChild(document.createElement("td"));
// Add it to the table
let lastRow = document.querySelector("#addPreset");
lastRow.parentNode.insertBefore(tr,lastRow);
}
}
function showScreenSize()
{
document.querySelector("#screenSize").textContent = screen.availWidth + "x" + screen.availHeight;
}
async function showCurrentSize()
{
let currentWindow = await browser.windows.getCurrent();
document.querySelector("#currentSize").textContent = currentWindow.width + "x" + currentWindow.height + " (click to use)";
}
async function useCurrentSize()
{
let pendingEdit = document.querySelector("#editStatus");
let mode = pendingEdit == null ? MODE_NEW : MODE_EDIT;
let currentWindow = await browser.windows.getCurrent();
document.querySelector("#" + mode + "Width").value = currentWindow.width;
document.querySelector("#" + mode + "Height").value = currentWindow.height;
}
async function addPreset(e)
{
e.preventDefault();
let values = getValues(MODE_NEW);
if(values.valid)
{
console.log("(add) Adding preset: " + values.width + "x" + values.height + " " + values.name);
let item = {
"id": makeID(),
"width": values.width,
"height": values.height,
"name": values.name
};
let storage = await browser.storage.local.get("presets");
let presets = storage.presets;
presets.push(item);
await browser.storage.local.set({"presets": presets});
displayPresets();
}
}
async function editPreset(e)
{
e.preventDefault();
let myId = getId(e.target.id,PREFIX_EDIT);
let pendingEdit = document.querySelector("#editStatus");
if(pendingEdit != null)
{
// Abort the other & try this one again
await cancelEdits();
document.querySelector("#" + PREFIX_EDIT + myId).click();
return;
}
let tr = e.target.parentElement.parentElement;
// Replace values with editable versions
let tdW = tr.children[2]
let inW = document.createElement("input");
inW.type = "text";
inW.id = "editWidth";
inW.value = tdW.textContent;
inW.size = 4;
inW.maxLength = 4;
tdW.textContent = "";
tdW.appendChild(inW);
let tdH = tr.children[3]
let inH = document.createElement("input");
inH.type = "text";
inH.id = "editHeight";
inH.value = tdH.textContent;
inH.size = 4;
inH.maxLength = 4;
tdH.textContent = "";
tdH.appendChild(inH);
let tdN = tr.children[4]
let inN = document.createElement("input");
inN.type = "text";
inN.id = "editName";
inN.value = tdN.textContent;
inN.size = 15;
inN.maxLength = 15;
tdN.textContent = "";
tdN.appendChild(inN);
// Offer a different set of actions
let tdA = tr.children[5];
tdA.textContent = "";
let btnS = document.createElement("button");
btnS.type = "button";
btnS.id = PREFIX_SAVE + myId;
btnS.textContent = "Save";
btnS.addEventListener("click", saveEdits);
tdA.appendChild(btnS);
let btnC = document.createElement("button");
btnC.type = "button";
btnC.id = PREFIX_CANCEL + myId;
btnC.textContent = "Cancel";
btnC.addEventListener("click", cancelEdits);
tdA.appendChild(btnC);
let tdS = tr.children[6];
tdS.id = "editStatus";
}
async function saveEdits(e)
{
e.preventDefault();
let values = getValues(MODE_EDIT);
if(values.valid)
{
let myId = getId(e.target.id,PREFIX_EDIT);
console.log("(edit) Request to edit id=" + myId);
let storage = await browser.storage.local.get("presets");
let presets = storage.presets;
for(let i = 0;i < presets.length;i++)
{
if(presets[i].id === myId)
{
console.log("(edit) Found it at i=" + i);
presets[i].width = values.width;
presets[i].height = values.height;
presets[i].name = values.name;
break;
}
}
await browser.storage.local.set({"presets": presets});
displayPresets();
}
}
async function cancelEdits()
{
// displayPresets() will take care of this by reloading everything
await displayPresets();
}
async function removePreset(e)
{
e.preventDefault();
let myId = getId(e.target.id,PREFIX_REMOVE);
console.log("(remove) Request to remove id=" + myId);
let storage = await browser.storage.local.get("presets");
let presets = storage.presets;
for(let i = 0;i < presets.length;i++)
{
if(presets[i].id === myId)
{
console.log("(remove) Found it at i=" + i);
presets.splice(i,1); // Removes this item
break;
}
}
await browser.storage.local.set({"presets": presets});
displayPresets();
}
async function moveDown(e)
{
e.preventDefault();
let myPos = getId(e.target.id,PREFIX_DOWN);
console.log("(move) Moving down from " + myPos);
let storage = await browser.storage.local.get("presets");
let presets = storage.presets;
presets.move(myPos,myPos + 1);
await browser.storage.local.set({"presets": presets});
displayPresets();
}
async function moveUp(e)
{
e.preventDefault();
let myPos = getId(e.target.id,PREFIX_UP);
console.log("(move) Moving up from " + myPos);
let storage = await browser.storage.local.get("presets");
let presets = storage.presets;
presets.move(myPos,myPos - 1);
await browser.storage.local.set({"presets": presets});
displayPresets();
}
async function setKeepActionMenuOnClick()
{
let storage = await browser.storage.local.get("options");
let options = storage.options;
extend(options,{"keepActionMenuOnClick": document.querySelector("#keepActionMenuOnClick").checked});
await browser.storage.local.set({"options": options});
}
async function setOversizeAllowance()
{
let storage = await browser.storage.local.get("advanced");
let advanced = storage.advanced;
extend(advanced,{"oversizeAllowance": Number(document.querySelector("#oversizeAllowance").value) });
await browser.storage.local.set({"advanced": advanced});
}
async function resetAdvanced()
{
await browser.storage.local.set({"advanced": {"oversizeAllowance": OVERSIZE_ALLOWANCE_DEFAULT} });
loadAdvancedSettings();
}
async function showAdvanced()
{
document.querySelector("#acceptAdvanced").style.display = "none";
document.querySelector("#theAdvanced").style.display = "block";
}
async function doOnLoad()
{
displayPresets();
loadOptions();
displayBackups();
loadAdvancedSettings();
}
async function updateSizes()
{
showCurrentSize();
showScreenSize();
}
async function displayBackups()
{
let storage = await browser.storage.sync.get("backups");
if(storage.backups === undefined)
{
console.log("(displayBackup) Initializing backups storage...");
storage.backups = [];
await browser.storage.sync.set({"backups": storage.backups});
}
let backups = storage.backups;
let existingRows = document.querySelectorAll(".backupItem");
for(let row of existingRows)
{
console.log("(displayBackup) Remove item row");
row.remove();
};
let backupCount = backups.length;
for(let i = 0;i < backupCount;i++)
{
let buId = backups[i].id;
console.log("(displayBackup) Add item " + buId + ": " + backups[i].name);
let isLast = (i == backupCount - 1);
let bList = document.querySelector("#backupList");
let buItem = document.createElement("div");
buItem.className = "backupItem";
let btnR = document.createElement("button");
btnR.type = "button";
btnR.id = PREFIX_RESTORE + buId;
btnR.textContent = "Restore";
btnR.addEventListener("click", backupAction);
buItem.appendChild(btnR);
buItem.appendChild(document.createTextNode(" "));
let btnD = document.createElement("button");
btnD.type = "button";
btnD.id = PREFIX_REMOVE + buId;
btnD.textContent = "Delete";
btnD.addEventListener("click", backupAction);
buItem.appendChild(btnD);
let savedAt = new Date(backups[i].saveTime);
buItem.appendChild(document.createTextNode(" " + backups[i].name + " (Saved at: " + savedAt.toLocaleString() + ")"));
bList.appendChild(buItem);
}
}
function backupAction(e)
{
e.preventDefault();
let myId = e.target.id;
let buId = -1;
if(myId.startsWith(PREFIX_RESTORE))
{
restoreBackup(getId(myId,PREFIX_RESTORE));
}
else if(myId.startsWith(PREFIX_REMOVE))
{
deleteBackup(getId(myId,PREFIX_REMOVE));
}
}
async function restoreBackup(buId)
{
let storage = await browser.storage.sync.get("backups");
let backups = storage.backups;
let foundIndex = -1;
if(buId !== undefined)
{
for(let i = 0;i < backups.length;i++)
{
if(backups[i].id === buId)
{
console.log("(restoreBackup) Found it at i=" + i);
foundIndex = i;
break;
}
}
}
if(foundIndex == -1)
{
console.log("(restoreBackup) Not found, no changes made");
}
else
{
if(confirm("Restoring '" + backups[foundIndex].name + "' will overwrite your current options!"))
{
console.log("(restoreBackup) restoring");
let storageToLoad = backups[foundIndex].storage;
await browser.storage.local.set(storageToLoad);
doOnLoad();
}
}
}
async function deleteBackup(buId)
{
let storage = await browser.storage.sync.get("backups");
let backups = storage.backups;
let foundIndex = -1;
if(buId !== undefined)
{
for(let i = 0;i < backups.length;i++)
{
if(backups[i].id === buId)
{
console.log("(deleteBackup) Found it at i=" + i);
foundIndex = i;
break;
}
}
}
if(foundIndex == -1)
{
console.log("(deleteBackup) Not found, no changes made");
}
else
{
if(confirm("Backup '" + backups[foundIndex].name + "' will be removed forever!"))
{
console.log("(deleteBackup) Removing");
backups.splice(foundIndex,1);
await browser.storage.sync.set({"backups": backups});
displayBackups();
}
}
}
async function saveBackup(e)
{
e.preventDefault();
let storage = await browser.storage.sync.get("backups");
let backups = storage.backups;
let buName = document.querySelector("#newBackupName").value;
if(buName.length < 1)
{
buName = "Untitled";
}
let foundIndex = -1;
for(let i = 0;i < backups.length;i++)
{
if(backups[i].name === buName)
{
console.log("(saveBackup) Found it at i=" + i);
foundIndex = i;
break;
}
}
let storageToSave = await browser.storage.local.get(["version","presets","options","advanced"]);
if(foundIndex == -1)
{
console.log("(saveBackup) Not found, adding it");
let buNew = {
"id": makeID(),
"name": buName,
"saveTime": new Date().getTime(),
"storage": storageToSave
};
backups.push(buNew);
await browser.storage.sync.set({"backups": backups});
displayBackups();
}
else
{
if(confirm("Existing backup '" + backups[foundIndex].name + "' will be replaced with current options!"))
{
console.log("(saveBackup) Updating it");
backups[foundIndex].saveTime = new Date().getTime();
backups[foundIndex].storage = storageToSave;
await browser.storage.sync.set({"backups": backups});
displayBackups();
}
}
document.querySelector("#newBackupName").value = "";
}
window.addEventListener("resize", updateSizes);
document.addEventListener("DOMContentLoaded", doOnLoad);
document.querySelector("#addNew").addEventListener("click", addPreset);
document.querySelector("#currentSize").addEventListener("click", useCurrentSize);
document.querySelector("#keepActionMenuOnClick").addEventListener("change", setKeepActionMenuOnClick);
document.querySelector("#saveNewBackup").addEventListener("click", saveBackup);
document.querySelector("#oversizeAllowance").addEventListener("change", setOversizeAllowance);
document.querySelector("#acceptAdvanced button").addEventListener("click", showAdvanced);
document.querySelector("#resetAdvanced").addEventListener("click", resetAdvanced);
setInterval(updateSizes,300); // Because the "resize" event doesn't fire unless content is reflowed due to the resize. Also handles when moving window between different sized screens!