This repository has been archived by the owner on Dec 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
/
bootstrap.js
291 lines (248 loc) · 9.99 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
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
/* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is OneLiner.
*
* The Initial Developer of the Original Code is The Mozilla Foundation.
* Portions created by the Initial Developer are Copyright (C) 2011
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
* Edward Lee <edilee@mozilla.com>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
"use strict";
const global = this;
const {classes: Cc, interfaces: Ci, utils: Cu} = Components;
Cu.import("resource://gre/modules/AddonManager.jsm");
Cu.import("resource://gre/modules/Services.jsm");
Cu.import("resource://gre/modules/XPCOMUtils.jsm");
// Keep a reference to various packaged images
const images = {};
// Define how wide the urlbar should be
const URLBAR_WIDTH = 400;
// Combine the navigation and tabs into one line
function makeOneLine(window) {
let {async, change, createNode, listen, unload} = makeWindowHelpers(window);
let {document, gBrowser, gURLBar} = window;
// Get aliases to various elements
let [commands,
navBar, tabsBar,
backForward, urlContainer, reload, stop,
backCmd, forwardCmd] =
["mainCommandSet",
"nav-bar", "TabsToolbar",
"unified-back-forward-button", "urlbar-container", "reload-button", "stop-button",
"Browser:Back", "Browser:Forward",
].map(function(id) document.getElementById(id));
// Save the order of elements in the navigation bar to restore later
let origNav = Array.slice(navBar.childNodes);
// Create a new search button that can prefill the search input box
let search = createNode("toolbarbutton");
search.setAttribute("class", "toolbarbutton-1 chromeclass-toolbar-additional");
search.setAttribute("image", images.search16);
search.addEventListener("command", function() {
let browser = gBrowser.selectedBrowser;
// See if we should copy over the value in the input when searching
let prefill = gURLBar.value.trim();
if (prefill.search(/[:\/\.]/) != -1)
prefill = "";
// Check for a focused plain textbox
let {focusedElement} = document.commandDispatcher;
let {nodeName, type, value} = focusedElement || {};
if (prefill == "" &&
focusedElement != gURLBar.inputField &&
nodeName != null &&
nodeName.search(/^(html:)?input$/i) == 0 &&
type.search(/^text$/i) == 0) {
prefill = value.trim();
}
// Check the page for selected text
if (prefill == "")
prefill = browser.contentWindow.getSelection().toString().trim();
// Check the clipboard for text
if (prefill == "")
prefill = (window.readFromClipboard() || "").trim();
// Make sure to not replace pinned tabs
if (gBrowser.selectedTab.pinned) {
let tab = gBrowser.addTab("about:home");
gBrowser.selectedTab = tab;
browser = tab.linkedBrowser;
}
// Replace the tab with search
else
browser.loadURI("about:home");
// Prepare about:home with a prefilled search once
browser.focus();
browser.addEventListener("DOMContentLoaded", function doPrefill() {
browser.removeEventListener("DOMContentLoaded", doPrefill, false);
// Prefill then select it for easy typing over
let input = browser.contentDocument.getElementById("searchText");
input.value = prefill;
input.setSelectionRange(0, prefill.length);
// Clear out the location bar so it shows the placeholder text
gURLBar.value = "";
}, false);
}, false);
// Move the navigation controls to the tabs bar
let navOrder = [backForward, urlContainer, reload, stop, search];
navOrder.reverse().forEach(function(node) {
if (node != null)
tabsBar.insertBefore(node, tabsBar.firstChild);
});
// Create a dummy backForward object if we don't have the node
backForward = backForward || {
boxObject: {
width: 0,
},
style: {},
};
// Fix up some styling of the now one-line toolbar
navBar.hidden = true;
urlContainer.removeAttribute("flex");
urlContainer.removeAttribute("width");
urlContainer.style.position = "relative";
// Clean up various changes when the add-on unloads
unload(function() {
tabsBar.removeChild(search);
origNav.forEach(function(node) navBar.appendChild(node));
backForward.style.marginRight = "";
navBar.hidden = false;
urlContainer.setAttribute("flex", 400);
urlContainer.style.position = "";
urlContainer.style.width = "";
});
// Figure out how much the back/forward button should get covered by urls
let buttonWidth = backForward.boxObject.width / 2;
function updateBackForward() {
let buttons = 0;
if (!forwardCmd.hasAttribute("disabled"))
buttons = 2;
else if (!backCmd.hasAttribute("disabled"))
buttons = 1;
// Cover up some buttons by shifting the urlbar left
let baseWidth = (gURLBar.focused ? 2 : 1) * URLBAR_WIDTH;
let width = baseWidth - buttonWidth * buttons;
let offset = -buttonWidth * (2 - buttons);
backForward.style.marginRight = offset + "px";
urlContainer.style.width = width + "px";
}
// Update the look immediately when activating
updateBackForward();
// Detect when the back/forward buttons change state to update UI
change(window, "UpdateBackForwardCommands", function(orig) {
return function(webnav) {
orig.call(this, webnav);
updateBackForward();
};
});
// Do the custom search button command instead of the original
listen(commands, "command", function(event) {
if (event.target.id == "Tools:Search") {
event.stopPropagation();
search.doCommand();
}
});
// Make sure we set the right size of the urlbar on blur or focus
listen(gURLBar, "blur", function() updateBackForward());
listen(gURLBar, "focus", function() updateBackForward());
// Detect escaping from the location bar when nothing changes
listen(gURLBar, "keydown", function(event) {
if (event.keyCode == event.DOM_VK_ESCAPE) {
let {popupOpen, value} = gURLBar;
async(function() {
// Only return focus to the page if nothing changed since escaping
if (gURLBar.popupOpen == popupOpen && gURLBar.value == value)
gBrowser.selectedBrowser.focus();
});
}
});
}
/**
* Handle the add-on being activated on install/enable
*/
function startup({id}) AddonManager.getAddonByID(id, function(addon) {
// Load various javascript includes for helper functions
["helper", "utils"].forEach(function(fileName) {
let fileURI = addon.getResourceURI("scripts/" + fileName + ".js");
Services.scriptloader.loadSubScript(fileURI.spec, global);
});
// Load various images to use later
["search16"].forEach(function(fileName) {
images[fileName] = addon.getResourceURI("images/" + fileName + ".png").spec;
});
// Load various stylesheets that automatically unload
loadStyles(addon, ["browser"]);
// Move the navigation bar into the tabs bar
watchWindows(function(window) {
let {async} = makeWindowHelpers(window);
// XXX Windows seems to reset toolbar items for new windows, so wait a bit
async(function() makeOneLine(window));
});
// Detect toolbar customization to temporarily disable the add-on
watchWindows(function(window) {
let {listen} = makeWindowHelpers(window);
// Disable the add-on when customizing
listen(window, "beforecustomization", function() {
// NB: Disabling will unload listeners, so manually add and remove below
addon.userDisabled = true;
// Listen for one customization finish to re-enable the addon
window.addEventListener("aftercustomization", function reenable() {
window.removeEventListener("aftercustomization", reenable, false);
addon.userDisabled = false;
}, false);
});
});
// Make sure fullscreen always shows the toolbar without animation
const AUTOHIDE_PREF = "browser.fullscreen.autohide";
Services.prefs.setBoolPref(AUTOHIDE_PREF, false);
unload(function() Services.prefs.clearUserPref(AUTOHIDE_PREF));
// Make the back/forward buttons tall enough for edge-clicks in fullscreen
watchWindows(function(window) {
let {unload} = makeWindowHelpers(window);
let {document} = window;
// Increase the height extra to fill in empty space above it
let back = document.getElementById("back-button");
let forward = document.getElementById("forward-button");
back.style.height = forward.style.height = "30px";
unload(function() back.style.height = forward.style.height = "");
});
})
/**
* Handle the add-on being deactivated on uninstall/disable
*/
function shutdown(data, reason) {
// Clean up with unloaders when we're deactivating
if (reason != APP_SHUTDOWN)
unload();
}
/**
* Handle the add-on being installed
*/
function install(data, reason) {}
/**
* Handle the add-on being uninstalled
*/
function uninstall(data, reason) {}