-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathindex.js
1485 lines (1320 loc) · 51.1 KB
/
index.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
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// yet another speed dial
// copyright 2019 [email protected]
// absolutely no warranty is expressed or implied
'use strict';
// speed dial
const bookmarksContainerParent = document.getElementById('tileContainer');
const bookmarksContainer = document.getElementById('wrap');
const foldersContainer = document.getElementById('folders');
const addFolderButton = document.getElementById('addFolderButton');
const menu = document.getElementById('contextMenu');
const folderMenu = document.getElementById('folderMenu');
const settingsMenu = document.getElementById('settingsMenu');
const modal = document.getElementById('tileModal');
const modalContent = document.getElementById('tileModalContent');
const createDialModal = document.getElementById('createDialModal');
const createDialModalContent = document.getElementById('createDialModalContent');
const createDialModalURL = document.getElementById('createDialModalURL');
const createDialModalSave = document.getElementById('createDialModalSave');
const createFolderModal = document.getElementById('createFolderModal');
const createFolderModalContent = document.getElementById('createFolderModalContent');
const createFolderModalName = document.getElementById('createFolderModalName');
const createFolderModalSave = document.getElementById('createFolderModalSave');
const editFolderModal = document.getElementById('editFolderModal');
const editFolderModalContent = document.getElementById('editFolderModalContent');
const editFolderModalName = document.getElementById('editFolderModalName');
const editFolderModalSave = document.getElementById('editFolderModalSave');
const deleteFolderModal = document.getElementById('deleteFolderModal');
const deleteFolderModalContent = document.getElementById('deleteFolderModalContent');
const deleteFolderModalName = document.getElementById('deleteFolderModalName');
const deleteFolderModalSave = document.getElementById('deleteFolderModalSave');
const toast = document.getElementById('toast');
const toastContent = document.getElementById('toastContent');
const closeModal = document.getElementsByClassName("close");
const modalSave = document.getElementById('modalSave');
const sidenav = document.getElementById("sidenav");
const modalTitle = document.getElementById("modalTitle");
const modalURL = document.getElementById("modalURL");
const modalImgContainer = document.getElementById("modalImgContainer");
const modalImgInput = document.getElementById("modalImgFile");
const noBookmarks = document.getElementById('noBookmarks');
// settings sidebar
const reader = new FileReader();
const color_picker = document.getElementById("color-picker");
const color_picker_wrapper = document.getElementById("color-picker-wrapper");
const textColor_picker = document.getElementById("textColor-picker");
const textColor_picker_wrapper = document.getElementById("textColor-picker-wrapper");
const imgInput = document.getElementById("file");
const imgPreview = document.getElementById("preview");
const wallPaperEnabled = document.getElementById("wallpaper");
const previewContainer = document.getElementById("previewContainer");
const largeTilesInput = document.getElementById("largeTiles");
const showTitlesInput = document.getElementById("showTitles");
const labelContainer = document.getElementById("labelContainer");
const labelFontSizeInput = document.getElementById("labelFontSize");
const showCreateDialInput = document.getElementById("showCreateDial");
const showFoldersInput = document.getElementById("showFolders");
const showClockInput = document.getElementById("showClock");
const showSettingsBtnInput = document.getElementById("showSettingsBtn");
const maxColsInput = document.getElementById("maxcols");
const defaultSortInput = document.getElementById("defaultSort");
//const saveBtn = document.getElementById("saveBtn");
//const settingsToast = document.getElementById("settingsToast");
// clock
const clock = document.getElementById('clock');
const port = "p-" + new Date().getTime();
const tabMessagePort = browser.runtime.connect({name: port});
let cache = null;
let settings = null;
let speedDialId = null;
let sortable = null;
let targetTileHref = null;
let targetTileTitle = null;
let targetNode = null;
let targetFolder = null;
let targetFolderName = null;
let targetFolderLink = null;
let folders = [];
let currentFolder = null;
let scrollPos = 0;
let homeFolderTitle = browser.i18n.getMessage('home');
let windowSize = null;
let containerSize = null;
let layoutFolder = false;
let boxes = [];
let hourCycle = 'h12';
const locale = navigator.language;
const debounce = (func, delay) => {
let inDebounce
return function() {
const context = this
const args = arguments
clearTimeout(inDebounce)
inDebounce = setTimeout(() => func.apply(context, args), delay)
}
}
// detect clock settings
if (!locale.startsWith("en")) {
hourCycle = Intl.DateTimeFormat(locale, {hour: 'numeric'}).resolvedOptions().hourCycle;
}
function displayClock() {
clock.textContent = new Date().toLocaleString('en-US', {hour: 'numeric', minute: 'numeric', hourCycle: hourCycle});
setTimeout(displayClock, 10000);
}
displayClock();
// detect label settings
function changeLabelFontSize(){
var input = document.getElementById("labelFontSize").value;
document.getElementById("tileContainer").style.fontSize = input + "pt";
}
changeLabelFontSize();
function getBookmarks(folderId) {
browser.bookmarks.getChildren(folderId).then(result => {
if (folderId === speedDialId && !result.length && settings.showFolders) {
noBookmarks.style.display = 'block';
addFolderButton.style.display = 'none';
}
printBookmarks(result, folderId)
});
}
function removeBookmark(url) {
let currentParent = currentFolder ? currentFolder : speedDialId
browser.bookmarks.search({url})
.then(bookmarks => {
let cleanup = bookmarks.length < 2;
for (let bookmark of bookmarks) {
if (bookmark.parentId === currentParent) {
targetNode.remove();
browser.bookmarks.remove(bookmark.id);
// if we have duplicates (ex in other folders), keep the image cache, otherwise purge it
if (cleanup) {
browser.storage.local.remove(url);
}
// todo -- this only working for root folder?
sortable.save();
}
}
})
}
function moveBookmark(url, idFrom, idTo) {
if (url && idFrom && idTo) {
// the id of the main speed dial page is "wrap"; todo: clean this up
if (idTo === "wrap") {
idTo = speedDialId;
}
if (idFrom === "wrap") {
idFrom = speedDialId;
}
browser.bookmarks.search({url})
.then(bookmarks => {
for (let bookmark of bookmarks) {
if (bookmark.parentId === idFrom) {
browser.bookmarks.move(bookmark.id, {parentId: idTo})
// avoid chaos if there are duplicate bookmarks inside the folder; we're only dragging one so just move one
// todo: tiles need to store ids in addition to url..
break;
}
}
});
}
}
function showFolder(id) {
hideSettings();
let folders = document.getElementsByClassName('container');
for (let folder of folders) {
if (folder.id === id || (folder.id === 'wrap' && id === speedDialId)) {
folder.style.display = "flex"
folder.style.opacity = "0";
layoutFolder = true;
// transition between folders. todo more elegant solution
setTimeout(function () {
//layoutFolder = id;
folder.style.opacity = "1";
}, 20);
} else {
folder.style.display = "none";
}
}
// style the active tab
let folderTitles = document.getElementsByClassName('folderTitle');
for (let title of folderTitles) {
if (title.attributes.folderid.value === id) {
title.classList.add('activeFolder');
} else {
title.classList.remove('activeFolder');
}
}
}
function getThumbs(bookmarkUrl) {
return browser.storage.local.get(bookmarkUrl)
.then(result => {
if (result[bookmarkUrl]) {
return result[bookmarkUrl];
}
});
}
function sort() {
browser.storage.local.get(speedDialId)
.then(result => {
if (result[speedDialId]) {
// default setting is now to place new dials in the last position so they dont disrupt the order of dials on the page
// if the defaultSort setting is set to "first" this behavior is reversed.
// background: newest last is preferable when speed dial is generally static (easier to find tiles with unchanging position)
// but ive come to prefer newest first when using YASD for ALL bookmarks (since recently bookmarked sites will now be at the top)
// TODO: make this a per folder setting
if (settings.defaultSort && settings.defaultSort === "last") {
let savedOrder = result[speedDialId];
let currentOrder = sortable.toArray();
if (currentOrder.length > savedOrder.length) {
let newDials = currentOrder.filter(x => !savedOrder.includes(x));
if (newDials.length > 0) {
for (let dial of newDials) {
savedOrder.splice(-1, 0, dial)
}
}
}
sortable.sort(savedOrder);
} else {
sortable.sort(result[speedDialId]);
}
}
animate();
bookmarksContainer.style.opacity = "1";
bookmarksContainerParent.scrollTop = scrollPos;
sortable.save();
});
}
function printFolderBookmarks() {
for (let folder of folders) {
getBookmarks(folder)
}
}
function folderLink(title, id) {
let a = document.createElement('a');
if (id === speedDialId) {
a.id = "homeFolderLink";
}
a.classList.add('tile');
a.classList.add('folderTitle');
a.setAttribute('folderId', id);
let linkText = document.createTextNode(title);
a.appendChild(linkText);
//a.href = "#"+bookmark.id;
a.onclick = function () {
showFolder(id);
currentFolder = id;
};
// todo: allow dropping directly on folder title?
a.ondragenter = dragenterHandler;
a.ondragleave = dragleaveHandler;
foldersContainer.appendChild(a);
}
function createFolder() {
hideSettings();
createFolderModalName.value = '';
createFolderModalName.focus();
createFolderModal.style.transform = "translateX(0%)";
createFolderModal.style.opacity = "1";
createFolderModalContent.style.transform = "scale(1)";
createFolderModalContent.style.opacity = "1";
}
function saveFolder() {
let name = createFolderModalName.value.trim();
browser.bookmarks.create({
title: name,
parentId: speedDialId
}).then(node => {
hideModals();
});
}
function editFolder() {
browser.bookmarks.update(targetFolder, {
title: editFolderModalName.value.trim()
}).then(node => {
hideModals();
});
}
function refreshThumbnails(url) {
tabMessagePort.postMessage({refreshThumbs: true, url});
toastContent.innerText = ` Capturing images...`;
toast.style.transform = "translateX(0%)";
}
function removeFolder() {
browser.bookmarks.removeTree(targetFolder).then(() => {
hideModals();
targetFolderLink.remove();
folders.splice(folders.indexOf(targetFolder), 1);
if (!folders.length) {
document.getElementById('homeFolderLink').remove();
}
if (document.getElementById(targetFolder).style.display === 'flex') {
showFolder(speedDialId);
}
document.getElementById(targetFolder).remove();
});
}
// assumes 'bookmarks' param is content of a folder (from getBookmarks)
function printBookmarks(bookmarks, parentId) {
let fragment = document.createDocumentFragment();
//let folderContainer = document.createElement('div');
//folderContainer.id = parentId;
//document.body.append(div)
if (bookmarks) {
for (let bookmark of bookmarks) {
// folders
if (!bookmark.url && bookmark.dateGroupModified) {
// setup "tabs" folder header links
if (!folders.length) {
folderLink(homeFolderTitle, speedDialId)
}
if (folders.indexOf(bookmark.id) === -1) {
folders.push(bookmark.id);
folderLink(bookmark.title, bookmark.id)
} else {
if (bookmark.id === targetFolder && targetFolderName !== bookmark.title) {
targetFolderLink.textContent = bookmark.title;
}
}
} else if (bookmark.url && bookmark.url.startsWith("http")) {
// restricted to valid url schemes for security reasons -- http and https. see #26
// in ff bookmark "separators" can be created that have "data:" as the url.
let thumbUrl = null;
if (cache[bookmark.url]) {
// if the image is a blob:
//iconURL = URL.createObjectURL(result.icon);
//iconURL = result.icon;
thumbUrl = cache[bookmark.url];
} else {
thumbUrl = "../img/default.png";
}
let a = document.createElement('a');
a.classList.add('tile');
a.href = bookmark.url;
a.setAttribute('data-id', bookmark.id);
let main = document.createElement('div');
main.classList.add('tile-main');
let content = document.createElement('div');
content.classList.add('tile-content');
content.style.backgroundImage = "url(" + thumbUrl + ")";
let title = document.createElement('div');
title.classList.add('tile-title');
if (!settings.showTitles) {
title.classList.add('hide');
}
title.textContent = bookmark.title;
main.appendChild(content);
main.appendChild(title);
a.appendChild(main);
fragment.appendChild(a);
}
}
}
// new dial button
let a = document.createElement('a');
a.classList.add('tile', 'createDial');
a.onclick = function () {
hideSettings();
buildCreateDialModal(parentId);
modalShowEffect(createDialModalContent, createDialModal);
};
let main = document.createElement('div');
main.classList.add('tile-main');
let content = document.createElement('div');
content.classList.add('tile-content', 'createDial-content');
main.appendChild(content);
a.appendChild(main);
fragment.appendChild(a);
// root speed dial dir
if (parentId === speedDialId) {
// populate folders divs
if (folders.length) {
printFolderBookmarks();
}
bookmarksContainer.appendChild(fragment);
// todo: clean this up, restore sort when we remove migration
// preserve sorting from 1.5 versions
//migrate();
sort();
// we take care of this as part of "sort" fn now..
//bookmarksContainer.style.opacity = "1";
} else {
// build a folder "tab"
if (!document.getElementById(parentId)) {
let folderContainer = document.createElement('div');
folderContainer.id = parentId;
folderContainer.classList.add('container');
folderContainer.style.display = 'none';
folderContainer.style.opacity = "1";
//document.body.append(folderContainer);
bookmarksContainerParent.append(folderContainer);
}
let folderContainerEl = document.getElementById(parentId);
// folder sorting..
// todo: this is fubar
let sortable = new Sortable(folderContainerEl, {
group: 'shared',
animation: 160,
ghostClass: 'selected',
dragClass: 'dragging',
filter: ".createDial",
onMove: onMoveHandler,
onEnd: onEndHandler,
store: {
set: function (sortable) {
let order = sortable.toArray();
browser.storage.local.set({[parentId]: order});
}
}
});
// append bookmarks to container
folderContainerEl.appendChild(fragment);
// sort
browser.storage.local.get(parentId)
.then(result => {
if (result[parentId]) {
sortable.sort(result[parentId]);
animate();
bookmarksContainerParent.scrollTop = scrollPos;
sortable.save();
}
});
//
}
}
function showContextMenu(el, top, left) {
if ((document.body.clientWidth - left) < (el.clientWidth + 30)) {
el.style.left = (left - el.clientWidth) + 'px';
} else {
el.style.left = left + 'px';
}
if ((document.body.clientHeight - top) < (el.clientHeight + 30)) {
el.style.top = (top - el.clientHeight) + 'px';
} else {
el.style.top = top + 'px';
}
el.style.visibility = "visible";
el.style.opacity = "1";
}
function hideMenus() {
let menus = [menu, settingsMenu, folderMenu]
for (let el of menus) {
el.style.visibility = "hidden";
el.style.opacity = "0";
}
}
function openSettings() {
sidenav.style.boxShadow = "0px 2px 8px 0px rgba(0,0,0,0.5)";
sidenav.style.transform = "translateX(0%)";
}
function hideSettings() {
sidenav.style.transform = "translateX(100%)";
sidenav.style.boxShadow = "none";
}
function hideModals() {
let modals = [modal, createDialModal, createFolderModal, editFolderModal, deleteFolderModal];
let modalContents = [modalContent, createDialModalContent, createFolderModalContent, editFolderModalContent, deleteFolderModalContent]
for (let el of modalContents) {
el.style.transform = "scale(0.8)";
el.style.opacity = "0";
}
for (let el of modals) {
el.style.opacity = "0";
setTimeout(function () {
el.style.transform = "translateX(100%)";
}, 160);
}
}
function modalShowEffect(contentEl, modalEl) {
modalEl.style.transform = "translateX(0%)";
modalEl.style.opacity = "1";
contentEl.style.transform = "scale(1)";
contentEl.style.opacity = "1";
}
function hideToast() {
toast.style.transform = "translateX(100%)";
toastContent.innerText = '';
}
function buildCreateDialModal(parentId) {
createDialModalURL.value = '';
createDialModalURL.parentId = parentId ? parentId : speedDialId;
createDialModalURL.focus();
}
async function buildModal(url, title) {
// nuke any previous modal
let carousel = document.getElementById("carousel");
if (carousel) {
modalImgContainer.removeChild(carousel);
}
let customCarousel = document.getElementById("customCarousel");
if (customCarousel) {
modalImgContainer.removeChild(customCarousel);
}
let newCarousel = document.createElement('div');
newCarousel.setAttribute('id', 'carousel');
modalImgContainer.appendChild(newCarousel);
//let createdCarousel = document.getElementById('carousel');
modalTitle.value = title;
modalURL.value = url;
let images = await getThumbs(url);
if (images && images.thumbnails.length) {
// clunky af
// todo: support adding a custom image
let index = images.thumbIndex;
let imgDiv = document.createElement('div');
let img = document.createElement('img');
img.setAttribute('src', images.thumbnails[index]);
imgDiv.appendChild(img);
newCarousel.appendChild(imgDiv);
for (let [i, image] of images.thumbnails.entries()) {
if (i !== index) {
let imgDiv = document.createElement('div');
let img = document.createElement('img');
img.setAttribute('src', image);
imgDiv.appendChild(img);
newCarousel.appendChild(imgDiv);
}
}
$('#carousel').flexCarousel({height: '180px'});
}
}
function rectifyUrl(url) {
if (url && !url.startsWith('https://') && !url.startsWith('http://')) {
return 'https://' + url;
} else {
return url;
}
}
function createDial() {
let url = rectifyUrl(createDialModalURL.value.trim());
browser.bookmarks.create({
title: url,
url: url,
parentId: createDialModalURL.parentId
}).then(node => {
hideModals();
toastContent.innerText = ` Capturing images for ${url}...`;
toast.style.transform = "translateX(0%)";
});
}
function saveBookmarkSettings() {
// todo: cleanup this abomination when im not on drugs
let title = modalTitle.value;
let url = targetTileHref;
let newUrl = rectifyUrl(modalURL.value.trim());
let selectedImageSrc = null;
let thumbIndex = 0;
let imageNodes = document.getElementsByClassName('fc-slide');
let customCarousel = document.getElementById('customCarousel');
if (customCarousel) {
selectedImageSrc = customCarousel.children[0].src;
targetNode.children[0].children[0].style.backgroundImage = `url('${selectedImageSrc}')`;
browser.storage.local.get(url)
.then(result => {
let thumbnails = [];
if (result[url]) {
thumbnails = result[url].thumbnails;
thumbnails.push(selectedImageSrc);
thumbIndex = thumbnails.indexOf(selectedImageSrc);
} else {
thumbnails.push(selectedImageSrc);
thumbIndex = 0;
}
browser.storage.local.set({[newUrl]: {thumbnails, thumbIndex}}).then(result => {
tabMessagePort.postMessage({updateCache: true, url: newUrl, i: thumbIndex});
if (title !== targetTileTitle) {
updateTitle()
}
});
});
} else {
for (let node of imageNodes) {
// div with order "2" is the one being displayed by the carousel
if (node.style.order === '2') {
// sometimes the carousel puts images inside a <figure class="fc-image"> elem
if (node.children[0].className === "fc-image") {
selectedImageSrc = node.children[0].children[0].src;
} else {
selectedImageSrc = node.children[0].src;
}
// update tile
targetNode.children[0].children[0].style.backgroundImage = `url('${selectedImageSrc}')`;
break;
}
}
browser.storage.local.get(url)
.then(result => {
if (result[url]) {
let thumbnails = result[url].thumbnails;
thumbIndex = thumbnails.indexOf(selectedImageSrc);
if (thumbIndex >= 0) {
browser.storage.local.set({[newUrl]: {thumbnails, thumbIndex}}).then(result => {
tabMessagePort.postMessage({updateCache: true, url: newUrl, i: thumbIndex});
if (title !== targetTileTitle || url !== newUrl) {
updateTitle()
}
});
} else {
if (title !== targetTileTitle || url !== newUrl) {
updateTitle()
}
}
} else {
if (title !== targetTileTitle || url !== newUrl) {
updateTitle()
}
}
});
}
// find image index
function updateTitle() {
// allow ui to respond immediately while bookmark updated
//targetNode.children[0].children[1].textContent = title;
// sortable ids changed so rewrite to storage
//let order = sortable.toArray();
//browser.storage.local.set({"sort":order});
// todo: temp hack to match all until we start using bookmark ids
browser.bookmarks.search({url})
.then(bookmarks => {
if (bookmarks.length <= 1 && ( url !== newUrl) ) {
// cleanup unused thumbnails
browser.storage.local.remove(url)
}
for (let bookmark of bookmarks) {
let currentParent = currentFolder ? currentFolder : speedDialId
if (bookmark.parentId === currentParent) {
browser.bookmarks.update(bookmark.id, {
title,
url: newUrl
});
}
if (url !== newUrl && toastContent.innerText === '') {
toastContent.innerText = ` Capturing images for ${newUrl}...`;
toast.style.transform = "translateX(0%)";
}
}
})
}
hideModals();
}
// todo: maybe refactor this in gsap 3
const animate = debounce(() => {
//var inputs = document.querySelectorAll("input");
const nodes = document.querySelectorAll(".tile");
//const observerConfig = { attributes: false, childList: true, subtree: false };
const total = nodes.length;
//const time = 0.9;
const omega = 12;
const zeta = 0.8;
//let boxes = [];
//let windowSize = window.innerWidth;
for (let i = 0; i < total; i++) {
let node = nodes[i];
TweenLite.set(node, {x: "+=0"});
const transform = node._gsTransform;
const x = node.offsetLeft;
const y = node.offsetTop;
boxes[i] = {node, transform, x, y};
}
//const observer = new MutationObserver(() => { dirty = true; });
//observer.observe(bookmarksContainer, observerConfig);
// todo: move this
TweenLite.ticker.addEventListener("tick", layout);
layout();
function layout() {
if (layoutFolder || containerSize !== getComputedStyle(bookmarksContainer).maxWidth || windowSize !== window.innerWidth) {
windowSize = window.innerWidth;
containerSize = getComputedStyle(bookmarksContainer).maxWidth;
for (let i = 0; i < total; i++) {
let box = boxes[i];
let randTime;
const lastX = box.x;
const lastY = box.y;
box.x = box.node.offsetLeft;
box.y = box.node.offsetTop;
if (lastX !== box.x || lastY !== box.y) {
const x = box.transform.x + lastX - box.x;
const y = box.transform.y + lastY - box.y;
if (layoutFolder) {
// folder opened -- zero duration because we are just setting the positions of the dials, so whenever
// a resize occurs the animation will start from the right position
randTime = 0;
} else {
randTime = ((i / (total * 2)) + 0.6).toFixed(1);
}
// Tween to 0 to remove the transforms
TweenLite.set(box.node, {x, y});
TweenLite.to(box.node, randTime, {x: 0, y: 0, ease});
}
}
layoutFolder = false;
}
}
function ease(progress) {
const beta = Math.sqrt(1.0 - zeta * zeta);
progress = 1 - Math.cos(progress * Math.PI / 2);
progress = 1 / beta *
Math.exp(-zeta * omega * progress) *
Math.sin(beta * omega * progress + Math.atan(beta / zeta));
return 1 - progress;
}
}, 500);
function readURL(input) {
if (input.files && input.files[0]) {
reader.readAsDataURL(input.files[0]);
}
}
function resizeBackground(dataURI) {
return new Promise(function (resolve, reject) {
let img = new Image();
img.onload = function () {
if (this.height > screen.height) {
let height = screen.height;
let ratio = height / this.height;
let width = Math.round(this.width * ratio);
let canvas = document.createElement('canvas');
let ctx = canvas.getContext('2d');
ctx.imageSmoothingEnabled = true;
canvas.width = width;
canvas.height = height;
ctx.drawImage(this, 0, 0, width, height);
// todo: remove this whenever firefox supports webp. in meantime we fallback to jpg for speed
if (browser.runtime.getBrowserInfo) {
const newDataURI = canvas.toDataURL('image/jpeg', 0.8);
resolve(newDataURI);
} else {
const newDataURI = canvas.toDataURL('image/webp', 0.87);
resolve(newDataURI);
}
} else {
resolve(dataURI);
}
};
img.src = dataURI;
})
}
function resizeThumb(dataURI) {
return new Promise(function (resolve, reject) {
let img = new Image();
img.onload = function () {
if (this.height > 256 && this.width > 256) {
// when im less lazy check use optimal w/h based on image
// set height to 256 and scale
let height = 256;
let ratio = height / this.height;
let width = Math.round(this.width * ratio);
let canvas = document.createElement('canvas');
let ctx = canvas.getContext('2d');
ctx.imageSmoothingEnabled = true;
canvas.width = width;
canvas.height = height;
ctx.drawImage(this, 0, 0, width, height);
// webp encoding falls back to png on firefox
const newDataURI = canvas.toDataURL('image/webp', 0.7);
resolve(newDataURI);
} else {
resolve(dataURI);
}
};
img.src = dataURI;
})
}
function readImage(input) {
return new Promise(function (resolve, reject) {
let filereader = new FileReader();
filereader.onload = function (e) {
resolve(e.target.result);
};
if (input.files && input.files[0]) {
filereader.readAsDataURL(input.files[0]);
}
});
}
//todo: deletability yo
function addImage(image) {
let carousel = document.getElementById('carousel');
if (carousel) {
carousel.style.display = "none";
let customCarousel = document.getElementById('customCarousel');
if (customCarousel) {
customCarousel.remove();
}
customCarousel = document.createElement('div');
customCarousel.setAttribute('id', 'customCarousel');
customCarousel.style.height = "180px";
let preview = document.createElement('img');
preview.style.height = '100%';
preview.style.width = '100%';
preview.style.objectFit = 'contain';
preview.setAttribute('src', image);
customCarousel.appendChild(preview);
modalImgContainer.appendChild(customCarousel);
}
}
function hexToRgb(color) {
let colors = color.replace("#", "").match(/.{2}/g);
return colors.map(c => parseInt("0x" + c));
}
// given a color, return whether white or black has the most contrast
// approximates w3c accessibility algorithm
function contrast(rgb) {
let srgb = [];
rgb.forEach(function (c, i) {
c = c / 255;
if (c <= 0.03928) {
c = c / 12.92
} else {
c = Math.pow((c + 0.055) / 1.055, 2.4);
}
srgb[i] = c
});
let l = ((0.2126 * srgb[0]) + (0.7152 * srgb[1]) + (0.0722 * srgb[2]));
if (l > 0.179) {
return '#000000'
} else {
return '#ffffff'
}
}
function getAverageRGB(imgPath) {
return new Promise(function (resolve, reject) {
// todo: performance: use the bg preview image from the settings nav rather than using a constructor
let img = new Image();
img.onload = function () {
let blockSize = 5; // only visit every 5 pixels
let canvas = document.createElement('canvas');
let context = canvas.getContext && canvas.getContext('2d');
let data, width, height;
let i = -4;
let length;
let rgb = [0, 0, 0];
let count = 0;
height = canvas.height = img.naturalHeight || img.offsetHeight || img.height;
width = canvas.width = img.naturalWidth || img.offsetWidth || img.width;
context.drawImage(img, 0, 0);
data = context.getImageData(0, 0, width, height);
length = data.data.length;
while ((i += blockSize * 4) < length) {
++count;
rgb[0] += data.data[i];
rgb[1] += data.data[i + 1];
rgb[2] += data.data[i + 2];
}
rgb[0] = ~~(rgb[0] / count);
rgb[1] = ~~(rgb[1] / count);
rgb[2] = ~~(rgb[2] / count);
resolve(rgb);
};
img.src = imgPath;
});
}
function applySettings() {
return new Promise(function (resolve, reject) {
// apply settings to speed dial
if (settings.wallpaper && settings.wallpaperSrc) {
// perf hack for default gradient bg image. user selected images are data URIs
if (settings.wallpaperSrc.length < 65) {
document.body.style.background = `linear-gradient(135deg, #4387a2, #5b268d)`;
} else {
document.body.style.background = `url("${settings.wallpaperSrc}") no-repeat top center fixed`;
document.body.style.backgroundSize = 'cover';
}
} else {
document.body.style.background = settings.backgroundColor;
}
if (settings.textColor) {
document.documentElement.style.setProperty('--color', settings.textColor);
}
if (settings.maxCols && settings.maxCols !== "100") {
document.documentElement.style.setProperty('--columns', settings.maxCols * 220 + "px")
} else {
document.documentElement.style.setProperty('--columns', '100%')
}
if (settings.showFolders) {
document.documentElement.style.setProperty('--show-folders', 'inline');
} else {
document.documentElement.style.setProperty('--show-folders', 'none');
}
if (settings.showClock) {
clock.style.setProperty('--clock', 'block');
} else {
clock.style.setProperty('--clock', 'none');
}
if (settings.showSettingsBtn) {
settingsBtn.style.setProperty('--settings', 'block');
} else {
settingsBtn.style.setProperty('--settings', 'none');
}
if (!settings.showTitles) {
document.documentElement.style.setProperty('--title-opacity', '0');