-
Notifications
You must be signed in to change notification settings - Fork 0
/
bootstrap.js
192 lines (147 loc) · 6.36 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
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
"use strict";
//==================== Modulize JS Scripts START ====================//
/* Includes a javascript file with loadSubScript
*
* @param src (String)
* The url of a javascript file to include.
*/
(function(global) global.include = function include(src) {
var o = {};
Components.utils.import("resource://gre/modules/Services.jsm", o);
var uri = o.Services.io.newURI(
src, null, o.Services.io.newURI(__SCRIPT_URI_SPEC__, null, null));
o.Services.scriptloader.loadSubScript(uri.spec, global);
})(this);
/* Imports a commonjs style javascript file with loadSubScript
*
* @param src (String)
* The url of a javascript file.
*/
(function(global) {
var modules = {};
global.require = function require(src) {
if (modules[src]) return modules[src];
var scope = {require: global.require, exports: {}};
var tools = {};
Components.utils.import("resource://gre/modules/Services.jsm", tools);
var baseURI = tools.Services.io.newURI(__SCRIPT_URI_SPEC__, null, null);
try {
var uri = tools.Services.io.newURI(
"packages/" + src + ".js", null, baseURI);
tools.Services.scriptloader.loadSubScript(uri.spec, scope);
} catch (e) {
var uri = tools.Services.io.newURI(src, null, baseURI);
tools.Services.scriptloader.loadSubScript(uri.spec, scope);
}
return modules[src] = scope.exports;
}
})(this);
//==================== Modulize JS Scripts END ====================//
// Constants
const { classes: Cc, Constructor: CC, interfaces: Ci, utils: Cu,
results: Cr, manager: Cm } = Components;
// Import any Firefox modules here
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/AddonManager.jsm");
// Import any custom JS files
// But they are still part of this addon
// Just like code separated into different files
include("includes/prefs.js");
include("includes/init-listeners.js");
include("includes/tab-unread.js");
// Require any CommonJS style files
var utils = require("utils");
var {unload} = require("unload");
var {log, debug, dump} = require("console");
var prefUtils = require("pref-utils");
var sessionStore = require("session-store");
//==================== Code to run with DOM Window START ====================//
// Origin Code from http://www.oxymoronical.com/blog/2011/01/Playing-with-windows-in-restartless-bootstrapped-extensions
var WindowListener = {
setupBrowserUI: function(window) {
let document = window.document;
// Take any steps to add UI or anything to the browser window
// document.getElementById() etc. will work here
// === Mapping preference to attribute (for styling) ===
prefUtils.mapBoolPrefToAttribute("highlightCurrentTab", window.gBrowser.tabContainer, "tabkit-highlight-current-tab");
prefUtils.mapBoolPrefToAttribute("boldCurrentTab", window.gBrowser.tabContainer, "tabkit-bold-current-tab");
prefUtils.mapBoolPrefToAttribute("highlightUnreadTab", window.gBrowser.tabContainer, "tabkit-highlight-unread-tab");
prefUtils.mapBoolPrefToAttribute("highlightProtectedTab", window.gBrowser.tabContainer, "tabkit-highlight-protected-tab");
prefUtils.mapBoolPrefToAttribute("makeTabBackgroundSolid", window.gBrowser.tabContainer, "tabkit-make-tab-background-solid");
prefUtils.mapBoolPrefToAttribute("makeTabBarBackgroundSolid", window.gBrowser.tabContainer, "tabkit-make-tab-bar-background-solid");
// Run all the listeners (after a timeout)
runPostInitListeners(window);
},
tearDownBrowserUI: function(window) {
let document = window.document;
// Take any steps to remove UI or anything from the browser window
// document.getElementById() etc. will work here
},
// nsIWindowMediatorListener functions
onOpenWindow: function(xulWindow) {
// A new window has opened
let domWindow = xulWindow.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIDOMWindow);
// Wait for it to finish loading
domWindow.addEventListener("load", function listener() {
domWindow.removeEventListener("load", listener, false);
// If this is a browser window then setup its UI
if (domWindow.document.documentElement.getAttribute("windowtype") == "navigator:browser")
WindowListener.setupBrowserUI(domWindow);
}, false);
},
onCloseWindow: function(xulWindow) {
},
onWindowTitleChange: function(xulWindow, newTitle) {
}
};
//==================== Code to run with DOM Window END ====================//
//==================== Bootstrap addon methods START ====================//
function install(data, reasonCode) {
//empty
};
function uninstall(data, reasonCode) {
//empty
};
function startup(data, reasonCode) {
AddonManager.getAddonByID(data.id, function(addon) {
utils.loadStyles(addon, ["main"]);
});
// Always set the default prefs as they disappear on restart
prefUtils.setDefaultPrefs();
// === Register the preference listeners ===
// Otherwise the preference attribute mapping does not work
prefUtils.registerPrefObserver(localPrefs);
// === Register methods that should be run after most extensions ===
// Since some event listeners can only be added at that time
postInitListeners.push(initTabUnread);
let wm = Cc["@mozilla.org/appshell/window-mediator;1"].
getService(Ci.nsIWindowMediator);
// Get the list of browser windows already open
let windows = wm.getEnumerator("navigator:browser");
while (windows.hasMoreElements()) {
let domWindow = windows.getNext().QueryInterface(Ci.nsIDOMWindow);
WindowListener.setupBrowserUI(domWindow);
}
// Wait for any new browser windows to open
wm.addListener(WindowListener);
};
function shutdown(data, reasonCode) {
// When the application is shutting down we normally don't have to clean
// up any UI changes made
if (reasonCode == APP_SHUTDOWN)
return;
// Run all the unload callbacks on shutdown
unload();
let wm = Cc["@mozilla.org/appshell/window-mediator;1"].
getService(Ci.nsIWindowMediator);
// Get the list of browser windows already open
let windows = wm.getEnumerator("navigator:browser");
while (windows.hasMoreElements()) {
let domWindow = windows.getNext().QueryInterface(Ci.nsIDOMWindow);
WindowListener.tearDownBrowserUI(domWindow);
}
// Stop listening for any new browser windows to open
wm.removeListener(WindowListener);
};
//==================== Bootstrap addon methods END ====================//