-
-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathbackground.js
551 lines (508 loc) · 19.5 KB
/
background.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
// yet another speed dial
// copyright 2019 [email protected]
// absolutely no warranty is expressed or implied
'use strict';
let messagePorts = [];
let speedDialId = null;
let folderIds = [];
let settings = null;
let defaults = {
wallpaper: true,
wallpaperSrc: 'img/bg.jpg',
backgroundColor: '#111111',
largeTiles: true,
showTitles: true,
labelFontSize: 14,
showAddSite: true,
showFolders: true,
showSettingsBtn: true,
showClock: true,
maxCols: '100',
defaultSort: 'last',
textColor: '#ffffff'
};
let cache = {};
let ready = false;
let firstRun = true;
let tempTitle = '';
function getSpeedDialId() {
browser.bookmarks.search({title: 'Speed Dial', url: undefined}).then(result => {
if (result.length && result[0]) {
speedDialId = result[0].id;
// get subfolder ids
browser.bookmarks.getChildren(speedDialId).then(results => {
for (let result of results) {
if (!result.url && result.dateGroupModified) {
folderIds.push(result.id);
}
}
})
} else {
browser.bookmarks.create({title: 'Speed Dial'}).then(result => {
speedDialId = result.id;
});
}
ready = true;
if (messagePorts.length && firstRun) {
firstRun = false;
messagePorts[0].postMessage({ready, cache, settings, speedDialId});
}
});
}
function createBookmarkFromBrowser(tab) {
// check for doopz
let match = false;
browser.bookmarks.getSubTree(speedDialId).then(node => {
for (const bookmark of node[0].children) {
if (tab.url === bookmark.url) {
match = true;
break;
}
}
if (!match) {
browser.bookmarks.create({
parentId: speedDialId,
title: tab.title,
url: tab.url
})
}
});
}
function handleBrowserAction(tab) {
createBookmarkFromBrowser(tab)
browser.browserAction.disable(tab.id);
browser.browserAction.setBadgeText({text:"✅️", tabId:tab.id})
browser.browserAction.setBadgeBackgroundColor({color: [0, 0, 0, 0]});
}
function onClickHandler(info, tab) {
if (info.menuItemId === 'addToSpeedDial') {
createBookmarkFromBrowser(tab)
}
}
function refreshOpen() {
for (let port of messagePorts) {
port.postMessage({refresh:true, cache});
}
}
// convert relative url paths
function convertUrlToAbsolute(origin, path) {
if (path.indexOf('://') > 0) {
return path
} else if (path.indexOf('//') === 0) {
return 'https:' + path;
} else {
let url = new URL(origin);
if (path.slice(0,1) === "/") {
return url.origin + path;
} else {
if (url.pathname.slice(-1) !== "/") {
url.pathname = url.pathname + "/";
}
return url.origin + url.pathname + path;
}
}
}
function getThumbnails(url) {
return new Promise(function(resolve, reject) {
getOgImage(url)
.then(result => saveThumbnails(url, result))
.then(() => getScreenshot(url))
.then(result => resizeThumb(result))
.then(result => saveThumbnails(url, result))
.then(() => getLogo(url))
.then(result => saveThumbnails(url, result))
.then(() => resolve())
.catch(error => console.log(error));
});
}
function getOgImage(url) {
return new Promise(function(resolve, reject) {
// tracking protection hack. use local resource instead
let whitelist = [
"mail.google.com",
"gmail.com",
"www.facebook.com",
"www.reddit.com",
"twitter.com"
];
let hostname = new URL(url).hostname;
if (whitelist.includes(hostname)) {
resolve(['img/'+hostname+'.png']);
return;
}
let xhr = new XMLHttpRequest();
xhr.onerror = function(e) {
//console.log(e);
resolve([]);
};
xhr.onload = function () {
let images = [];
// get open graph images
if (!xhr.responseXML) {
resolve([]);
return
}
let metas = xhr.responseXML.getElementsByTagName("meta");
for (let meta of metas) {
if (meta.getAttribute("property") === "og:image" && meta.getAttribute("content")) {
let imageUrl = convertUrlToAbsolute(url, meta.getAttribute("content"));
images.push(imageUrl)
}
}
// get large icons
let sizes = [
"192x192",
"180x180",
"144x144",
"96x96"
];
for (let size of sizes) {
let icon = xhr.responseXML.querySelector(`link[rel="icon"][sizes="${size}"]`);
if (icon) {
let imageUrl = convertUrlToAbsolute(url, icon.getAttribute('href'));
images.push(imageUrl);
break;
}
}
// get apple touch icon
let appleIcon = xhr.responseXML.querySelector('link[rel="apple-touch-icon"]');
if (appleIcon) {
let imageUrl = convertUrlToAbsolute(url, appleIcon.getAttribute('href'));
images.push(imageUrl);
}
// get large favicon
let favicon = new URL(url).origin + "/favicon.ico";
fetch(new Request(favicon)).then(response => {
if (response.status === 200) {
let icon = new Image();
icon.onerror = function() {
resolve(images);
};
icon.onload = function() {
if (this.height >= 96) {
images.push(favicon);
}
resolve(images);
};
icon.src = favicon;
} else {
resolve(images);
}
}, err => {
console.log(err);
resolve([]);
});
};
xhr.open("GET", url);
xhr.responseType = "document";
// fix for shitty websites, like imdb
xhr.overrideMimeType("text/html");
xhr.send();
});
}
function saveThumbnails(url, images) {
return new Promise(function(resolve, reject) {
let thumbnails = [];
browser.storage.local.get(url)
.then(result => {
if (result[url] && result[url].thumbnails) {
thumbnails = result[url].thumbnails;
}
if (images && images.length) {
thumbnails.push(images);
}
thumbnails = thumbnails.flat();
browser.storage.local.set({[url]:{thumbnails, thumbIndex: 0}})
.then(() => resolve());
});
});
}
// requires <all_urls> permission to capture image without a user gesture
function getScreenshot(url) {
return new Promise(function(resolve, reject) {
// capture from an existing tab if its open
browser.tabs.query({active: true, windowId: browser.windows.WINDOW_ID_CURRENT})
.then(tabs => browser.tabs.get(tabs[0].id))
.then(tab => {
if (tab.url === url) {
tempTitle = tab.title ? tab.title : '';
browser.tabs.captureVisibleTab()
.then(imageUri => {
resolve(imageUri);
});
} else {
// open tab, capture screenshot, and close
// todo: complete loaded status sometimes !== actually loaded
let tabID = null;
function handleUpdatedTab(tabId, changeInfo, tabInfo) {
if (tabId === tabID && changeInfo.status === "complete") {
tempTitle = tabInfo.title ? tabInfo.title : '';
// workaround for chrome, which can only capture the active tab
if (!browser.runtime.getBrowserInfo) {
browser.tabs.update(tabID, {active:true}).then(tab => {
setTimeout(function() {
browser.tabs.captureVisibleTab().then(imageUri => {
browser.tabs.onUpdated.removeListener(handleUpdatedTab);
browser.tabs.remove(tabID);
resolve(imageUri);
}, (err) => {
console.log(err)
// carry on like it aint no tang
resolve([]);
});
}, 1240);
})
} else {
setTimeout(function() {
browser.tabs.captureTab(tabID).then(imageUri => {
browser.tabs.onUpdated.removeListener(handleUpdatedTab);
browser.tabs.remove(tabID);
resolve(imageUri);
}, (err) => {
console.log(err);
resolve([]);
});
}, 1240);
}
}
}
browser.tabs.onUpdated.addListener(handleUpdatedTab);
browser.tabs.create({url, active:false}).then(tab => {
tabID = tab.id;
// timeout for site to load
// todo: add a cancel button to UI
let timer = setTimeout(function() {
browser.tabs.get(tabID).then(tab => {
browser.tabs.onUpdated.removeListener(handleUpdatedTab);
browser.tabs.remove(tabID);
resolve([])
}, (err) => {
if (timer) clearTimeout(timer);
// tab was already closed, we all good
});
}, 15000)
});
}
}, (err) => {
console.log(err);
});
});
}
function getLogo(url) {
return new Promise(function(resolve, reject) {
// todo: setting to enable/disable this?
let logoUrl = 'https://logo.clearbit.com/' + new URL(url).hostname + '?size=200';
fetch(new Request(logoUrl)).then(response => {
if (response.status === 200) {
resolve(logoUrl);
} else {
resolve([]);
}
});
});
}
function resizeThumb(dataURI) {
return new Promise(function(resolve, reject) {
if (dataURI && dataURI.length) {
let img = new Image();
img.onload = function () {
if (this.height > 512 && this.width > 512) {
let canvas = document.createElement('canvas');
let ctx = canvas.getContext('2d');
let canvas2 = document.createElement('canvas');
let ctx2 = canvas2.getContext('2d');
ctx2.imageSmoothingEnabled = true;
ctx2.imageSmoothingQuality = "high";
// first pass: crop scrollbars, blur filter as an approximation for resampling
canvas.width = this.width - 20;
canvas.height = this.height - 20;
ctx.filter = `blur(1px)`;
ctx.drawImage(this, 0, 0, this.width - 18, this.height - 18, 0, 0, canvas.width, canvas.height);
// second pass: downscale to target size
let height = 256;
let ratio = height / this.height;
let width = Math.round(this.width * ratio);
canvas2.width = width;
canvas2.height = height;
ctx2.drawImage(canvas, 0, 0, width, height);
const newDataURI = canvas2.toDataURL('image/webp');
resolve(newDataURI);
} else {
resolve(dataURI);
}
};
img.src = dataURI;
} else {
resolve([]);
}
});
}
function removeBookmark(id, bookmarkInfo) {
if (bookmarkInfo.node.url && (bookmarkInfo.parentId === speedDialId || folderIds.indexOf(bookmarkInfo.parentId) !== -1)) {
browser.storage.local.remove(bookmarkInfo.node.url).catch((err) => {
console.log(err)
});
}
}
function pushToCache(url, i=0) {
return new Promise(function(resolve, reject) {
browser.storage.local.get(url).then(result => {
if (result[url]) {
cache[url] = result[url].thumbnails[i];
}
resolve();
});
});
}
function updateSettings() {
browser.storage.local.get('settings').then(result => {
settings = Object.assign({}, defaults, result.settings);
});
}
// todo: test behavior on chrome
function moved(id, info) {
//console.log("onMoved", info);
changeBookmark(id, info);
}
function changed(id, info) {
//console.log("onChanged", info);
changeBookmark(id, info);
}
function created(id, info) {
//console.log("onCreated", info);
changeBookmark(id, info);
}
function manualRefresh(url) {
browser.storage.local.remove(url).then(() => {
getThumbnails(url).then(() => {
pushToCache(url).then(() => {
refreshOpen()
})
})
})
}
// todo: allow editing URLs from speed dial page
// todo: something f'd up with getthumbnails
function changeBookmark(id, info) {
// info may only contain "changed" info -- ex. it may not contain url for moves, just old and new folder ids
// so we always "get" the bookmark to access all its info
browser.bookmarks.get(id).then(bookmark => {
// only interested in speed dial and its subfolders
if (bookmark[0].parentId === speedDialId || folderIds.indexOf(bookmark[0].parentId) !== -1) {
if (bookmark[0].url) {
if (bookmark[0].url !== "data:" && bookmark[0].url !== "about:blank") {
browser.storage.local.get(bookmark[0].url).then(result => {
if (result[bookmark[0].url]) {
// a pre-existing bookmark is being modified; dont fetch new thumbnails
// todo: broken with folders -- doesnt allow same site to have separate images in 2 folders.. who cares
// todo: there might be a race condition here for bookmarks created via context menu
refreshOpen();
} else {
getThumbnails(bookmark[0].url).then(() => {
pushToCache(bookmark[0].url).then(() => {
if (tempTitle !== '' && tempTitle !== bookmark[0].title) {
browser.bookmarks.update(id, {
title: tempTitle
});
// updating the bookmark title will trigger changebookmark to rerun and refresh above
} else {
refreshOpen()
}
})
})
}
});
}
} else {
// folder
// todo: support manual import of other folder: other folder wont have thumbs for individual sites
// but only triggers the change listener for the folder
if (bookmark[0].title === "New Folder") {
// firefox creates a placeholder for the folder when created via bookmark manager
return
}
// new folder
folderIds.push(id);
refreshOpen()
}
}
});
}
function connected(p) {
messagePorts.push(p);
p.onMessage.addListener(function(m) {
if (m.getCache) {
if (ready && speedDialId) {
p.postMessage({ready, cache, settings, speedDialId});
} else {
p.postMessage({ready:false});
}
}
else if (m.refreshThumbs) {
manualRefresh(m.url)
}
else if (m.updateCache) {
pushToCache(m.url, m.i);
}
else if (m.updateSettings) {
updateSettings();
}
});
p.onDisconnect.addListener(function(p) {
let i = messagePorts.indexOf(p);
messagePorts.splice(i, 1);
});
browser.browserAction.disable(p.sender.tab.id);
}
function handleInstalled(details) {
if (details.reason === "install") {
// set uninstall URL
browser.runtime.setUninstallURL("https://forms.gle/6vJPx6eaMV5xuxQk9");
//todo: detect existing speed dial folder
} else if (details.reason === 'update') {
// perform any migrations here...
// details.previousVersion
const url = chrome.runtime.getURL("updated.html");
chrome.tabs.create({ url });
}
}
function init() {
browser.runtime.onConnect.addListener(connected);
// ff triggers 'moved' for bookmarks saved to different folder than default
browser.bookmarks.onMoved.addListener(moved);
// ff triggers 'changed' for bookmarks created manually? todo: confirm
browser.bookmarks.onChanged.addListener(changed);
// chrome triggers oncreated for bookmarks created manually in bookmark mgr. todo: make sure this doesnt hurt ff
browser.bookmarks.onCreated.addListener(created);
browser.bookmarks.onRemoved.addListener(removeBookmark);
browser.contextMenus.onClicked.addListener(onClickHandler);
browser.browserAction.onClicked.addListener(handleBrowserAction);
// build a thumbnail cache of url:thumbUrl pairs
browser.storage.local.get().then(result => {
if (result) {
if (result.settings) {
settings = Object.assign({}, defaults, result.settings);
} else {
settings = defaults;
}
const entries = Object.entries(result);
for (let e of entries) {
// todo: filter folder ids
if (e[0] !== "settings" && e[1].thumbnails) {
let index = e[1].thumbIndex;
cache[e[0]] = e[1].thumbnails[index];
}
}
}
getSpeedDialId();
});
// context menu -> "add to speed dial"
browser.contextMenus.create({
"title": "Add to Speed Dial",
"contexts":['page'],
"documentUrlPatterns":['<all_urls>'],
"id": "addToSpeedDial"
});
browser.runtime.onInstalled.addListener(handleInstalled);
}
init();