-
Notifications
You must be signed in to change notification settings - Fork 39
/
bootstrap.js
47 lines (38 loc) · 1.46 KB
/
bootstrap.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
// This bootstrap.js file now acts as a background.js file in terms of a Web Extension. See here:
// https://developer.mozilla.org/en-US/Add-ons/WebExtensions/Anatomy_of_a_WebExtension#Specifying_background_scripts
// The onOpenWindow event handler was slightly modified to be compatible with standard Firefox.
var browser = browser || chrome
browser.action.onClicked.addListener((tab) => showTracerWindow());
var tracerWindow = null;
function showTracerWindow() {
if (tracerWindow != null) {
// Window already opened -- just give it focus.
browser.windows.update(tracerWindow.id, { focused: true }, null);
return;
}
// If it wasn't yet opened or it was already closed -- create a new instance.
let url = browser.runtime.getURL("/src/TraceWindow.html");
let creating = browser.windows.create({
url: url,
type: "popup",
height: 600,
width: 800
}, onCreated);
if (creating) {
// Firefox uses a promise for window creation
creating.then(onCreated, onError);
}
}
function onCreated(windowInfo) {
console.log(`Created window: ${windowInfo.id}`);
// memorize the extension window, so that we can give it focus, if it's already opened.
tracerWindow = windowInfo;
browser.windows.onRemoved.addListener(onCloseExtensionWindow);
}
function onError(error) {
console.log(`Error: ${error}`);
}
function onCloseExtensionWindow(windowId) {
console.log(`Window ${windowId} is closed. Setting "traceWindow" to null.`)
tracerWindow = null
}