-
-
Notifications
You must be signed in to change notification settings - Fork 44
/
index.ts
558 lines (490 loc) · 18.2 KB
/
index.ts
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
/* c8 ignore start -- main entry point for the background page */
import type {Menus} from "webextension-polyfill";
import browser from "webextension-polyfill";
import {copyIf} from "./model/index.js";
import type {ShowWhatOpt, StashWhatOpt} from "./model/options.js";
import type {Tab} from "./model/tabs.js";
import service_model from "./service-model.js";
import {
asyncEvent,
backingOff,
filterMap,
nonReentrant,
urlToOpen,
} from "./util/index.js";
import {logErrorsFrom} from "./util/oops.js";
logErrorsFrom(async () => {
// BEGIN FILE-WIDE ASYNC BLOCK
//
// Start our various services and set global variables used throughout the rest
// of this file.
//
const model = await service_model();
(<any>globalThis).model = model;
//
// Migrations
//
// Delete old DBs that are in the wrong format
indexedDB.deleteDatabase("cache:favicons");
indexedDB.deleteDatabase("cache:bookmarks");
// Tag hidden tabs which were hidden before upgrading to a version of Tab
// Stash that keeps track of which tabs it was responsible for hiding.
if (!model.options.local.state.migrated_tab_markers_applied) {
logErrorsFrom(async () => {
if (!!browser.tabs.hide && model.bookmarks.stash_root.value) {
const tabs = await browser.tabs.query({hidden: true});
await model.bookmarks.loadedStash();
const stashed_hidden_tabs = tabs.filter(t =>
model.bookmarks.isURLLoadedInStash(t.url!),
);
// This applies the tag as a side effect
await model.tabs.hide(
filterMap(stashed_hidden_tabs, t => model.tabs.tab(t.id!)),
);
}
await model.options.local.set({migrated_tab_markers_applied: true});
});
}
//
// User-triggered commands thru menu items, etc. IDs in the menu items
// correspond to field names in the commands object.
//
function menu(
idprefix: string,
contexts: Menus.ContextType[],
def: string[][],
) {
// Only create menus in contexts this browser understands.
const allowed_ctxs = Object.values((<any>browser.contextMenus).ContextType);
contexts = contexts.filter(x => allowed_ctxs.includes(x));
for (let [id, title] of def) {
if (id) {
browser.contextMenus.create({contexts, title, id: idprefix + id});
} else {
browser.contextMenus.create({
contexts,
type: "separator",
enabled: false,
});
}
}
}
const SHOW_TAB_NAME = browser.sidebarAction
? "Show Stashed Tabs in a Tab"
: "Show Stashed Tabs";
menu(
"1:",
["tab", "page", "tools_menu"],
[
["show_tab", SHOW_TAB_NAME],
...(browser.sidebarAction
? [["show_sidebar_or_tab", "Show Stashed Tabs in Sidebar"]]
: []),
["", ""],
["stash_all", "Stash Tabs"],
["stash_one", "Stash This Tab"],
["stash_one_newgroup", "Stash This Tab to a New Group"],
["", ""],
["copy_all", "Copy Tabs to Stash"],
["copy_one", "Copy This Tab to Stash"],
["", ""],
["options", "Options..."],
],
);
// These should only have like 6 items each
menu(
"2:",
["browser_action"],
[
["show_tab", SHOW_TAB_NAME],
...(browser.sidebarAction
? [["show_sidebar_or_tab", "Show Stashed Tabs in Sidebar"]]
: []),
["", ""],
["stash_all", "Stash Tabs"],
["copy_all", "Copy Tabs to Stash"],
],
);
menu(
"3:",
["page_action"],
[
["show_tab", SHOW_TAB_NAME],
...(browser.sidebarAction
? [["show_sidebar_or_tab", "Show Stashed Tabs in Sidebar"]]
: []),
["", ""],
["stash_one", "Stash This Tab"],
["stash_one_newgroup", "Stash This Tab to a New Group"],
["copy_one", "Copy This Tab to Stash"],
],
);
const commands: {[key: string]: (t?: Tab) => Promise<void>} = {
// NOTE: Several of these commands open the sidebar. We have to open the
// sidebar before the first "await" call, otherwise we won't actually have
// permission to do so per Firefox's API rules.
//
// Also note that some browsers don't support the sidebar at all; in these
// cases, we open the tab instead.
show_sidebar_or_tab: () =>
browser.sidebarAction
? browser.sidebarAction.open().catch(console.log)
: commands.show_tab(),
async show_popup() {
// Ugh, this hack where we set and then clear the popup is necessary
// because if the (Chrome) browser thinks ANY popup is set, either
// programmatically or thru manifest.json, it will just show the popup
// rather than running the browserAction.onClicked callback (which might
// do other things besides setting the popup).
try {
await browser.browserAction.setPopup({
popup: "stash-list.html?view=popup",
});
await browser.browserAction.openPopup();
} finally {
await browser.browserAction.setPopup({popup: ""});
}
},
async show_tab() {
await model.restoreTabs(
[
{
title: "Tab Stash",
url: browser.runtime.getURL("stash-list.html"),
},
],
{},
);
},
async stash_all(tab?: Tab) {
show_something(model.options.sync.state.open_stash_in);
await stash_something({what: "all", copy: false, tab});
},
async stash_one(tab?: Tab) {
show_something(model.options.sync.state.open_stash_in);
await stash_something({what: "single", copy: false, tab});
},
async stash_one_newgroup(tab?: Tab) {
show_something(model.options.sync.state.open_stash_in);
if (!tab) return;
await model.putItemsInFolder({
items: [tab],
toFolder: await model.bookmarks.createStashFolder(),
});
},
async copy_all(tab?: Tab) {
show_something(model.options.sync.state.open_stash_in);
await stash_something({what: "all", copy: true, tab});
},
async copy_one(tab?: Tab) {
show_something(model.options.sync.state.open_stash_in);
await stash_something({what: "single", copy: true, tab});
},
async options() {
await browser.runtime.openOptionsPage();
},
};
// Shows the Tab Stash UI in the manner requested by /show_what/. NOTE that to
// be able to open the sidebar, this function must be invoked in a
// user-initiated event handler context BEFORE any async operations are done.
function show_something(show_what?: ShowWhatOpt) {
switch (show_what) {
case "none":
break;
case "tab":
model.attempt(commands.show_tab);
break;
case "popup":
model.attempt(commands.show_popup);
case "sidebar":
model.attempt(commands.show_sidebar_or_tab);
break;
default:
show_setup_page();
break;
}
}
async function stash_something(options: {
what?: StashWhatOpt;
copy?: boolean;
tab?: Tab;
}) {
if (!options.tab || options.tab.position === undefined) return;
switch (options.what) {
case "all":
await model.stashAllTabsInWindow(options.tab.position.parent, {
copy: !!options.copy,
});
break;
case "single":
await model.putItemsInFolder({
items: copyIf(!!options.copy, [options.tab]),
toFolder: await model.ensureRecentUnnamedFolder(),
});
break;
case "none":
default:
break;
}
}
function show_setup_page() {
model.attempt(() =>
model.restoreTabs(
[
{
title: "Tab Stash - Setup",
url: browser.runtime.getURL("setup.html"),
},
],
{},
),
);
}
//
// Top-level/user facing event bindings, which mostly just call commands.
//
browser.contextMenus.onClicked.addListener((info, tab) => {
// #cast We only ever create menu items with string IDs
const cmd = (<string>info.menuItemId).replace(/^[^:]*:/, "");
console.assert(!!commands[cmd]);
const t = tab?.id ? model.tabs.tab(tab?.id) : undefined;
commands[cmd](t).catch(console.log);
});
if (browser.browserAction) {
// In order for show_something('popup') to work, we must preconfigure the
// browser to know which popup to show. This cannot be done at the time of
// show_something() because doing so requires an async call, and Firefox
// doesn't allow us to then show the popup after the async call
// returns--because we're no longer in a user event context.
function setupPopup() {
model.attempt(async () => {
if (model.options.sync.state.browser_action_show === "popup") {
// As soon as we configure a popup, the onClicked handler below
// will no longer run (the popup will be shown instead). This
// unfortunately means that we can't stash and show the popup at
// the same time. Sigh.
await browser.browserAction.setPopup({
popup: "stash-list.html?view=popup",
});
} else {
// If the user turns off the popup, we must clear the popup in
// the browser if we expect anything else to work.
await browser.browserAction.setPopup({popup: ""});
}
});
}
setupPopup();
model.options.sync.onChanged.addListener(setupPopup);
browser.browserAction.onClicked.addListener(
asyncEvent(async tab => {
const opts = model.options.sync.state;
// Special case so the user doesn't think Tab Stash is broken
if (!opts.browser_action_show || !opts.browser_action_stash) {
show_setup_page();
return;
}
show_something(opts.browser_action_show);
await stash_something({
what: opts.browser_action_stash,
tab: model.tabs.tab(tab.id!)!,
});
}),
);
}
if (browser.pageAction) {
browser.pageAction.onClicked.addListener(
asyncEvent(async tab => {
if (!model.options.sync.state.open_stash_in) {
// User hasn't decided what this button should do yet
show_setup_page();
return;
}
commands.stash_one(model.tabs.tab(tab.id));
}),
);
}
//
// Check for a fresh install and note which version we are, so we can notify the
// user when updates are installed.
//
if (model.options.local.state.last_notified_version === undefined) {
// This looks like a fresh install, (or an upgrade from a version that
// doesn't keep track of the last-notified version, in which case, we
// just assume it's a fresh install). Record our current version number
// here so we can detect upgrades in the future and show the user a
// whats-new notification.
model.attempt(async () =>
model.options.local.set({
last_notified_version: (await browser.management.getSelf()).version,
}),
);
}
// Check which options are selected for the browser and page actions, and change
// their icons accordingly.
model.options.sync.onChanged.addListener(opts =>
model.attempt(async () => {
function getTitle(stash?: StashWhatOpt): string {
switch (stash) {
case "all":
return "Stash all (or selected) tabs";
case "single":
return "Stash this tab";
case "none":
return "Show stashed tabs";
default:
return "Set up Tab Stash";
}
}
if (browser.browserAction) {
await browser.browserAction.setTitle({
title: getTitle(opts.state.browser_action_stash),
});
}
}),
);
//
// Setup GC events to close hidden tabs which are removed from the stash. This
// GC is triggered by any bookmark event which could possibly change the set of
// URLs stored in the stash.
//
// We garbage-collect (close) hidden tabs with URLs that correspond to bookmarks
// which are removed from the stash. Unfortunately, because Firefox doesn't
// provide a comprehensive accounting of all bookmarks that are removed (in
// particular, if a subtree is removed, we only get one notification for the
// top-level folder and NO information about the children that were deleted),
// the only way we can reliably identify which hidden tabs to throw away is by
// diffing the bookmark trees.
//
// This may be a bit over-aggressive if the user is using multiple extensions to
// manage hidden tabs, but there's unfortunately not much we can do about this.
// The alternative is to allow hidden tabs which belong to deleted folders to
// pile up, which will cause browser slowdowns over time.
//
logErrorsFrom(async () => {
let managed_urls = await model.bookmarks.urlsInStash();
const close_removed_bookmarks = backingOff(() =>
model.attempt(async () => {
// Garbage-collect hidden tabs by diffing the old and new sets of URLs
// in the tree.
const new_urls = await model.bookmarks.urlsInStash();
// Ugh, why am I open-coding a set-difference operation? This
// should be built-in!
let removed_urls = new Set();
for (let url of managed_urls) {
if (!new_urls.has(url)) removed_urls.add(url);
}
await model.tabs.remove(
model.tabs
.allTabs()
.filter(t => t.hidden && removed_urls.has(urlToOpen(t.url))),
);
managed_urls = new_urls;
}),
);
browser.bookmarks.onChanged.addListener(close_removed_bookmarks);
browser.bookmarks.onMoved.addListener(close_removed_bookmarks);
browser.bookmarks.onRemoved.addListener(close_removed_bookmarks);
});
//
// Setup a background job to discard (unload, but keep open) hidden tabs that
// haven't been touched in a while.
//
// Since under normal usage, we can accumulate a LOT of hidden tabs if the user
// leaves their browser open for a while, this is mostly a light-touch,
// precautionary measure to keep the user's memory usage from becoming
// surprisingly high over time.
//
// We could immediately discard a tab when stashing/hiding it, but this causes
// performance problems if the user wants to temporarily stash a bunch of tabs
// for a short period of time (e.g. if they are interrupted at their desk by,
// "Can you just check on this thing for me really quick?").
//
// We try to be relatively intelligent about the age (defined as "time since
// last access") of hidden tabs, to account for the fact that there will be
// periods of higher and lower activity (where more or fewer hidden tabs might
// be generated). We do this by setting a target tab count and age, and scaling
// the age boundary according to the number of loaded tabs. The target
// count/age are used as a reference point--when the target number of tabs are
// open, we want to discard tabs older than the target age (in this case, 50
// tabs and 10 minutes). If there are MORE than the target number of tabs open,
// the age will scale asymptotically towards 0. If there are FEWER than the
// target number of tabs open, we are more lax on the age, and we will always
// keep a certain minimum number of tabs open (for which the age is effectively
// infinite).
//
// Note that active (non-hidden) tabs are counted towards the total, so if the
// user has a lot of tabs open, we will discard hidden tabs more aggressively to
// stay within reasonable memory limits.
//
const discard_old_hidden_tabs = nonReentrant(async function () {
// We setTimeout() first because the enable/disable flag could change at
// runtime.
setTimeout(
discard_old_hidden_tabs,
model.options.local.state.autodiscard_interval_min * 60 * 1000,
);
if (!model.options.local.state.autodiscard_hidden_tabs) return;
let now = Date.now();
let tabs = await browser.tabs.query({discarded: false});
let tab_count = tabs.length;
let candidate_tabs = tabs
.filter(t => t.hidden && t.id !== undefined)
.filter(t => !t.audible || t.mutedInfo?.muted)
.sort((a, b) => (a.lastAccessed ?? 0) - (b.lastAccessed ?? 0));
const min_keep_tabs = model.options.local.state.autodiscard_min_keep_tabs;
const target_tab_count =
model.options.local.state.autodiscard_target_tab_count;
const target_age_ms =
model.options.local.state.autodiscard_target_age_min * 60 * 1000;
while (tab_count > min_keep_tabs) {
// Keep discarding tabs until we have the minimum number of tabs
// remaining, we run out of candidates, OR the age of the oldest tab
// is less than the cutoff (as a function of the number of
// non-discarded tabs).
//
// You'll have to graph /age_cutoff/ as a function of /tab_count/ to
// (literally) see why this makes sense--it's basically a hyperbola
// with the vertical asymptote at /MIN_KEEP_TABS/ and the horizontal
// asymptote at 0. I recommend https://www.desmos.com/calculator
// for a good graphing calculator.
let age_cutoff =
((target_tab_count - min_keep_tabs) * target_age_ms) /
(tab_count - min_keep_tabs);
let oldest_tab = candidate_tabs.pop();
if (!oldest_tab) break;
const age = now - (oldest_tab.lastAccessed ?? 0);
if (age > age_cutoff) {
--tab_count;
// #undef We filter no-id tabs out of /candidate_tabs/ above
await browser.tabs.discard([oldest_tab.id!]);
} else {
break;
}
}
});
// We use setTimeout rather than setInterval here because the interval could
// change at runtime if the corresponding option is changed. This will
// cause some drift but it's not a big deal--the interval doesn't need to be
// exact.
setTimeout(
discard_old_hidden_tabs,
model.options.local.state.autodiscard_interval_min * 60 * 1000,
);
//
// Setup a periodic background job to cleanup various deleted items and caches.
// Needed to prevent Tab Stash from consuming an unbounded amount of the user's
// local storage.
//
const gc = nonReentrant(() =>
model.attempt(async () => {
// Hard-coded to a day for now, for people who don't restart their browsers
// regularly. If this ever needs to be changed, we can always add an option
// for it later.
setTimeout(gc, 24 * 60 * 60 * 1000);
await model.gc();
}),
);
// Here we call gc() on browser restart to ensure it happens
// at least once.
gc();
}); // END FILE-WIDE ASYNC BLOCK