-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbackground.js
More file actions
131 lines (110 loc) · 4.09 KB
/
background.js
File metadata and controls
131 lines (110 loc) · 4.09 KB
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
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import {l10n} from '/fluent/bundle.js';
const MENUID = 'menuitem';
l10n.formatValue('menu').then(menuLabel =>
browser.menus.create({
id: MENUID,
title: menuLabel,
contexts: ['bookmark'],
visible: false,
}));
browser.menus.onShown.addListener(async function onShown(info) {
if (!info.contexts.includes('bookmark')) return;
onShown.currentShowing = 1 + (onShown.currentShowing || 0);
let thisShowing = onShown.currentShowing;
let bookmarks = await browser.bookmarks.get(info.bookmarkId);
let bookmark = bookmarks[0];
if (thisShowing != onShown.currentShowing) return;
browser.menus.update(MENUID, {
visible: (bookmark.type === 'folder'),
});
browser.menus.refresh();
});
browser.menus.onClicked.addListener(async function (info) {
let bookmarks = await getBookmarks(info.bookmarkId);
if (bookmarks.length === 0) return;
let bookmarkFolder = (await browser.bookmarks.get([info.bookmarkId]))[0];
let folderName = bookmarkFolder.title;
browser.runtime.onMessage.addListener(function f(msg, sender, respond) {
browser.runtime.onMessage.removeListener(f);
respond({bookmarks, folderName});
});
let tab = await getReusableTab();
if (tab) {
browser.tabs.update(tab.id, { url: '/status.html' });
} else {
browser.tabs.create({ url: '/status.html' });
}
});
// Returns the Tab object for the current tab if it is suitable to reuse for
// opening a new page; otherwise returns undefined. The tab is suitable if
// it's a fresh blank tab that's the last in its window.
async function getReusableTab() {
const BLANK_URLS = ['about:blank', 'about:home', 'about:newtab'];
let queryResult = await browser.tabs.query({
currentWindow: true,
active: true,
});
if (queryResult.length != 1) return;
let currentTab = queryResult[0];
if (currentTab.status != 'complete') return;
if (currentTab.pinned) return;
if (!BLANK_URLS.includes(currentTab.url)) return;
// Make sure this is the last tab in its window.
queryResult = await browser.tabs.query({
windowId: currentTab.windowId,
index: currentTab.index + 1,
});
if (queryResult.length != 0) return;
return currentTab;
}
async function getBookmarks(bookmarkId) {
let bookmarks = await browser.bookmarks.getChildren(bookmarkId);
return bookmarks.filter(bookmark =>
bookmark.type === 'bookmark' && bookmark.url);
}
/* There may be multiple sets of bookmarks being opened slowly, but we should
* maintain the limit on how many can load at once across all sessions. This
* part of the background script keeps track of how many slots for loading a
* tab are available or in use and allocates them to sessions.
*/
import {prefsReady} from '/prefs.js';
let total_slots_in_use = 0;
let queue = [];
async function advance_queue() {
let prefs = await prefsReady;
while (total_slots_in_use < prefs.inflight_max && queue.length) {
let port = queue.shift();
port.slots_in_use += 1;
total_slots_in_use += 1;
port.postMessage('slot assigned');
}
}
browser.runtime.onConnect.addListener(port => {
port.slots_in_use = 0;
port.onDisconnect.addListener(port => {
if (port.error) {
console.error("Port error", port.error);
}
total_slots_in_use -= port.slots_in_use;
let index = queue.indexOf(port);
if (index != -1) {
queue.splice(index, 1);
}
advance_queue();
});
port.onMessage.addListener(message => {
if (message == 'request slot') {
queue.push(port);
advance_queue();
} else if (message == 'release slot') {
port.slots_in_use -= 1;
total_slots_in_use -= 1;
advance_queue();
} else {
console.error("Unrecognized message", message);
}
});
});