-
Notifications
You must be signed in to change notification settings - Fork 848
/
startup-panel.js
70 lines (63 loc) · 2.15 KB
/
startup-panel.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
/* 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/. */
// Shows a panel (typically describing the addon) the first time an addon is installed.
const { Panel } = require("panel");
const ss = require("simple-storage");
const { setTimeout } = require("timers");
const { publicConstructor } = require("api-utils");
const { env } = require('api-utils/environment');
const self = require("self");
// Show panel 1 second after startup:
var DEFAULT_TIMEOUT = 1000;
function StartupPanel(options) {
this.name = options.name;
if (! this.name) {
throw 'You must give the panel a name';
}
options.width = options.width || 400;
options.height = options.height || 400;
// FIXME: should append this if necessary:
options.contentScript = SCRIPT;
this.options = options;
var seen = ss.storage[this.name + '-seen'];
if (env.SHOW_STARTUP_PANEL) {
// Primarily for development/testing
seen = false;
}
if (self.loadReason == "install" || self.loadReason == "enable") {
// Re-installed or re-enabled:
seen = false;
}
if (! seen) {
setTimeout(this.display.bind(this), options.startupTimeout || DEFAULT_TIMEOUT);
}
};
StartupPanel.prototype = {
display: function () {
this.panel = Panel(this.options);
this.panel.on("hide", (function () {
ss.storage[this.name + '-seen'] = true;
}).bind(this));
this.panel.port.on("Close", (function () {
this.panel.hide();
}).bind(this));
this.panel.show();
}
};
var SCRIPT = [
'window.addEventListener("load", function () {',
' var el = document.getElementById("close");',
' if (! el) {',
' el = document.createElement("A");',
' el.innerHTML = "×";',
' el.href = "#";',
' el.setAttribute("style", "position: absolute; font-weight: bold; top: 2px; right: 6px; text-decoration: none; color: #000;");',
' document.body.appendChild(el);',
' }',
' el.addEventListener("click", function () {',
' self.port.emit("Close");',
' }, false);',
'}, false);'
].join("\n");
exports.StartupPanel = publicConstructor(StartupPanel);