forked from sblask-webextensions/webextension-bookmark-dial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbackground.js
345 lines (299 loc) · 10.4 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
/* global Hermite_class */
const OPTION_BACKGROUND_COLOR = "option_background_color";
const OPTION_BACKGROUND_IMAGE_URL = "option_background_image_url";
const OPTION_BACKGROUND_SIZE = "option_background_size";
const OPTION_BOOKMARK_FOLDER = "option_bookmark_folder";
const OPTION_CUSTOM_CSS = "option_custom_css";
const THUMBNAIL_STORAGE_PREFIX = "thumbnail_";
const HERMITE = new Hermite_class(); // jscs:ignore
const SCROLLBAR_WIDTH = __getScrollbarWidth();
const THUMBNAIL_WIDTH = 300;
const THUMBNAIL_HEIGHT = 200;
const THUMBNAIL_STORAGE_MAXBYTES = 10 * 1024 * 1024;
class CleanURLSet extends Set {
add(url) {
return super.add(__cleanURL(url));
}
has(url) {
return super.has(__cleanURL(url));
}
delete(url) {
return super.delete(__cleanURL(url));
}
}
let bookmarkFolder = undefined;
const bookmarkFolderRegistry = new CleanURLSet();
const thumbnailRegistry = new CleanURLSet();
__initThumbnailRegistry();
browser.storage.local.get([
OPTION_BACKGROUND_COLOR,
OPTION_BACKGROUND_IMAGE_URL,
OPTION_BACKGROUND_SIZE,
OPTION_BOOKMARK_FOLDER,
OPTION_CUSTOM_CSS,
])
.then(
(result) => {
if (result[OPTION_BACKGROUND_COLOR] === undefined) {
browser.storage.local.set({[OPTION_BACKGROUND_COLOR]: "#000000"});
}
if (result[OPTION_BACKGROUND_IMAGE_URL] === undefined) {
browser.storage.local.set({[OPTION_BACKGROUND_IMAGE_URL]: ""});
}
if (result[OPTION_BACKGROUND_SIZE] === undefined) {
browser.storage.local.set({[OPTION_BACKGROUND_SIZE]: "auto"});
}
if (result[OPTION_CUSTOM_CSS] === undefined) {
browser.storage.local.set({[OPTION_CUSTOM_CSS]: ""});
}
bookmarkFolder = result[OPTION_BOOKMARK_FOLDER];
__initBookmarkFolderRegistry();
}
);
function onPreferencesChanged(changes) {
if (changes[OPTION_BOOKMARK_FOLDER]) {
bookmarkFolder = changes[OPTION_BOOKMARK_FOLDER].newValue;
__initBookmarkFolderRegistry();
}
}
function createThumbnail(bookmarkURL) {
const collectData = Promise.all([
browser.tabs.captureVisibleTab(),
browser.tabs.executeScript({ code: "window.innerWidth" }),
browser.tabs.executeScript({ code: "window.innerHeight" }),
]);
chainPromises([
() => { return collectData; },
(result) => { return __dataURLToCanvas(...__flatten(result)); },
(canvas) => { return __resize(canvas); },
(canvas) => { return __storeThumbnail(bookmarkURL, canvas.toDataURL()); },
]);
}
function __flatten(list) {
return [].concat.apply([], list);
}
function __dataURLToCanvas(dataURL, originalWidth, originalHeight) {
const [newWidth, newHeight] = __getNewSizing(originalWidth - SCROLLBAR_WIDTH, originalHeight);
return new Promise(
function(resolve, _reject) {
const canvas = document.createElement("canvas");
canvas.width = newWidth;
canvas.height = newHeight;
const image = new Image();
image.onload = function() {
canvas.getContext("2d").drawImage(image, 0, 0, newWidth, newHeight, 0, 0, newWidth, newHeight);
return resolve(canvas);
};
image.src = dataURL;
}
);
}
function __getNewSizing(originalWidth, originalHeight) {
const targetRatio = 3 / 2;
const currentRatio = originalWidth / originalHeight;
if (currentRatio > targetRatio) {
// cut off width
return [originalHeight * targetRatio, originalHeight];
} else {
// cut off height
return [originalWidth, originalWidth / targetRatio];
}
}
function __resize(canvas) {
HERMITE.resample_single(canvas, THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT, true); // jscs:ignore
return canvas;
}
function __simpleResize(canvas) { // eslint-disable-line no-unused-vars
const resizeCanvas = document.createElement("canvas");
resizeCanvas.width = THUMBNAIL_WIDTH;
resizeCanvas.height = THUMBNAIL_HEIGHT;
resizeCanvas.getContext("2d").drawImage(
canvas,
0, 0, canvas.width, canvas.height,
0, 0, THUMBNAIL_WIDTH, THUMBNAIL_HEIGHT,
);
return resizeCanvas;
}
function __getThumbnailURLs() {
return browser.storage.local.get().then(
preferenceItems => {
return Object.keys(preferenceItems)
.filter(key => { return key.indexOf(THUMBNAIL_STORAGE_PREFIX) === 0; })
.map(key => { return key.substring(THUMBNAIL_STORAGE_PREFIX.length); });
}
);
}
function __initThumbnailRegistry() {
return __getThumbnailURLs().then(thumbnailURLs => thumbnailURLs.map(thumbnailRegistry.add.bind(thumbnailRegistry)));
}
function __storeThumbnail(bookmarkURL, thumbnailDataURL) {
chainPromises([
() => { return browser.storage.local.set({[THUMBNAIL_STORAGE_PREFIX + bookmarkURL]: thumbnailDataURL}); },
() => { return thumbnailRegistry.add(bookmarkURL); },
() => { return browser.storage.local.getBytesInUse(); },
(bytesInUse) => { return __maybeRemoveUnusedThumbnails(bytesInUse); },
]);
}
function __maybeRemoveUnusedThumbnails(bytesInUse) {
if (bytesInUse > THUMBNAIL_STORAGE_MAXBYTES) {
__getThumbnailURLs().then(
thumbnailURLs => {
for (const url of thumbnailURLs) {
if (!bookmarkFolderRegistry.has(url)) {
browser.storage.local.remove(THUMBNAIL_STORAGE_PREFIX + url);
thumbnailRegistry.delete(url);
}
}
}
);
}
}
function __initBookmarkFolderRegistry() {
if (!bookmarkFolder) {
return false;
}
bookmarkFolderRegistry.clear();
browser.bookmarks.getChildren(bookmarkFolder).then(
bookmarks => {
bookmarks
.filter(bookmark => Object.prototype.hasOwnProperty.call(bookmark, "url"))
.filter(bookmark => bookmark.url) // filter out folders and separators
.map(bookmark => bookmark.url)
.map(url => bookmarkFolderRegistry.add(url));
}
);
}
function __updateBookmarkFolderRegistry(bookmark) {
if (bookmark.parentId === bookmarkFolder) {
bookmarkFolderRegistry.add(bookmark.url);
} else {
bookmarkFolderRegistry.delete(bookmark.url);
}
}
function maybeCreateThumbnail(url) {
Promise.all([
__hasNoThumbnail(url),
__isURLFromBookmarkFolder(url),
__isURLOpenInActiveTabAndComplete(url),
]).then(
conditions => {
if (conditions.every(Boolean)) {
createThumbnail(url);
}
}
);
}
function __hasNoThumbnail(url) {
return !thumbnailRegistry.has(url);
}
function __isURLFromBookmarkFolder(url) {
return bookmarkFolderRegistry.has(url);
}
function __isURLOpenInActiveTabAndComplete(url) {
if (!url) {
return false;
}
return chainPromises([
() => browser.tabs.query({ active: true, currentWindow: true }),
(tabs) => tabs[0],
(tab) => tab.status === "complete" && __cleanURL(tab.url) === __cleanURL(url),
]);
}
function __cleanURL(url) {
return url.replace(/https?:\/\//, "").replace(/\/+$/, "");
}
function handleBookmarkChange(bookmark) {
if (!bookmark.url) {
// folder or separator
return;
}
__updateBookmarkFolderRegistry(bookmark);
maybeCreateThumbnail(bookmark.url);
}
browser.bookmarks.onCreated.addListener(
(_id, bookmark) => handleBookmarkChange(bookmark)
);
browser.bookmarks.onChanged.addListener(
(id, _changeInfo) => browser.bookmarks.get(id).then(bookmarks => handleBookmarkChange(bookmarks[0]))
);
browser.bookmarks.onMoved.addListener(
(id, _moveInfo) => browser.bookmarks.get(id).then(bookmarks => handleBookmarkChange(bookmarks[0]))
);
browser.bookmarks.onRemoved.addListener(
(_id, removeInfo) => bookmarkFolderRegistry.delete(removeInfo.node.url)
);
browser.tabs.onUpdated.addListener(
(_tabId, _changeInfo, tabInfo) => {
if (tabInfo.status !== "complete") {
return;
}
return maybeCreateThumbnail(tabInfo.url);
}
);
browser.tabs.onActivated.addListener(
(activeInfo) => {
return chainPromises([
() => browser.tabs.get(activeInfo.tabId),
// delay as Chrome fails to capture image otherwise
(tab) => setTimeout(() => maybeCreateThumbnail(tab.url), 100),
]);
}
);
browser.storage.onChanged.addListener(onPreferencesChanged);
function handleRequest(request) {
if (request.message === "isGenerateThumbnailEnabled") {
return chainPromises([
() => browser.tabs.query({ active: true, currentWindow: true }),
(tabs) => __isURLFromBookmarkFolder(tabs[0].url),
]);
}
if (request.message === "generateThumbnail") {
chainPromises([
() => browser.tabs.query({ active: true, currentWindow: true }),
(tabs) => tabs[0],
(tab) => __isURLFromBookmarkFolder(tab.url) ? createThumbnail(tab.url) : null,
]);
}
}
browser.runtime.onMessage.addListener(handleRequest);
function chainPromises(functions) {
let promise = Promise.resolve();
for (const function_ of functions) {
promise = promise.then(function_);
}
return promise.catch((error) => { console.warn(error); });
}
function __makeStyle() {
const style = document.createElement("style");
style.type = "text/css";
document.head.appendChild(style);
return style;
}
function __getScrollbarWidth() {
const css = `
.scrollbar-measure {
height: 100px;
overflow: scroll;
position: absolute;
top: -9999px;
width: 100px;
}
`;
const style = __makeStyle();
style.appendChild(document.createTextNode(css));
const div = document.createElement("div");
div.className = "scrollbar-measure";
document.body.appendChild(div);
const scrollbarWidth = div.offsetWidth - div.clientWidth;
document.body.removeChild(div);
document.head.removeChild(style);
return scrollbarWidth;
}
function handleInstalled(details) {
if (details.reason === "update" && details.previousVersion === "1.1.2") {
browser.tabs.create({
url: "update.html",
});
}
}
browser.runtime.onInstalled.addListener(handleInstalled);