1 // Copyright (c) 2006-2008 by Martin Stubenschrott <stubenschrott@vimperator.org>
2 // Copyright (c) 2007-2011 by Doug Kearns <dougkearns@gmail.com>
3 // Copyright (c) 2008-2014 Kris Maglione <maglione.k at Gmail>
5 // This work is licensed for reuse under an MIT license. Details are
6 // given in the LICENSE.txt file included with this file.
14 var Browser = Module("browser", XPCOM(Ci.nsISupportsWeakReference, ModuleBase), {
15 init: function init() {
16 this.cleanupProgressListener = overlay.overlayObject(window.XULBrowserWindow,
17 this.progressListener);
18 util.addObserver(this);
20 this._unoverlay = overlay.overlayObject(FullZoom, {
21 get siteSpecific() false,
22 set siteSpecific(val) {}
26 destroy: function () {
27 this.cleanupProgressListener();
28 this.observe.unregister();
33 "chrome-document-global-created": function (win, uri) { this.observe(win, "content-document-global-created", uri); },
34 "content-document-global-created": function (win, uri) {
35 let top = util.topWindow(win);
40 if (top == window && (win.location.href || uri))
41 this._triggerLoadAutocmd("PageLoadPre", win.document, win.location.href || uri);
45 _triggerLoadAutocmd: function _triggerLoadAutocmd(name, doc, uri) {
46 if (!(uri || doc.location))
49 uri = isObject(uri) ? uri : util.newURI(uri || doc.location.href);
51 url: { toString: function () uri.spec, valueOf: function () uri },
55 if (!dactyl.has("tabs"))
56 update(args, { doc: doc, win: doc.defaultView });
58 args.tab = tabs.getContentIndex(doc) + 1;
60 valueOf: function () doc,
61 toString: function () "tabs.getTab(" + (args.tab - 1) + ").linkedBrowser.contentDocument"
64 valueOf: function () doc.defaultView,
65 toString: function () "tabs.getTab(" + (args.tab - 1) + ").linkedBrowser.contentWindow"
69 autocommands.trigger(name, args);
73 DOMContentLoaded: function onDOMContentLoaded(event) {
74 let doc = event.originalTarget;
75 if (doc instanceof Ci.nsIDOMHTMLDocument)
76 this._triggerLoadAutocmd("DOMLoad", doc);
79 // TODO: see what can be moved to onDOMContentLoaded()
80 // event listener which is is called on each page load, even if the
81 // page is loaded in a background tab
82 load: function onLoad(event) {
83 let doc = event.originalTarget;
84 if (doc instanceof Document)
85 dactyl.initDocument(doc);
87 if (doc instanceof Ci.nsIDOMHTMLDocument) {
88 if (doc.defaultView.frameElement) {
89 // document is part of a frameset
91 // hacky way to get rid of "Transferring data from ..." on sites with frames
92 // when you click on a link inside a frameset, because asyncUpdateUI
93 // is not triggered there (Gecko bug?)
94 this.timeout(function () { statusline.updateStatus(); }, 10);
97 // code which should happen for all (also background) newly loaded tabs goes here:
98 if (doc != config.browser.contentDocument)
99 dactyl.echomsg({ domains: [util.getHost(doc.location)], message: _("buffer.backgroundLoaded", (doc.title || doc.location.href)) }, 3);
101 this._triggerLoadAutocmd("PageLoad", doc);
108 * @property {Object} The document loading progress listener.
111 // XXX: function may later be needed to detect a canceled synchronous openURL()
112 onStateChange: util.wrapCallback(function onStateChange(webProgress, request, flags, status) {
113 const L = Ci.nsIWebProgressListener;
116 dactyl.applyTriggerObserver("browser.stateChange", arguments);
118 if (flags & (L.STATE_IS_DOCUMENT | L.STATE_IS_WINDOW)) {
119 // This fires when the load event is initiated
120 // only thrown for the current tab, not when another tab changes
121 if (flags & L.STATE_START) {
122 while (document.commandDispatcher.focusedWindow == webProgress.DOMWindow
123 && modes.have(modes.INPUT))
127 else if (flags & L.STATE_STOP) {
128 // Workaround for bugs 591425 and 606877, dactyl bug #81
129 config.browser.mCurrentBrowser.collapsed = false;
130 if (!dactyl.focusedElement || dactyl.focusedElement === document.documentElement)
131 dactyl.focusContent();
135 onStateChange.superapply(this, arguments);
137 onSecurityChange: util.wrapCallback(function onSecurityChange(webProgress, request, state) {
138 onSecurityChange.superapply(this, arguments);
139 dactyl.applyTriggerObserver("browser.securityChange", arguments);
141 onStatusChange: util.wrapCallback(function onStatusChange(webProgress, request, status, message) {
142 onStatusChange.superapply(this, arguments);
143 dactyl.applyTriggerObserver("browser.statusChange", arguments);
145 onProgressChange: util.wrapCallback(function onProgressChange(webProgress, request, curSelfProgress, maxSelfProgress, curTotalProgress, maxTotalProgress) {
146 onProgressChange.superapply(this, arguments);
147 dactyl.applyTriggerObserver("browser.progressChange", arguments);
149 // happens when the users switches tabs
150 onLocationChange: util.wrapCallback(function onLocationChange(webProgress, request, uri) {
151 onLocationChange.superapply(this, arguments);
153 dactyl.applyTriggerObserver("browser.locationChange", arguments);
155 let win = webProgress.DOMWindow;
157 Buffer(win).updateZoom();
159 let oldURI = overlay.getData(win.document)["uri"];
160 if (overlay.getData(win.document)["load-idx"] === webProgress.loadedTransIndex
161 || !oldURI || uri.spec.replace(/#.*/, "") !== oldURI.replace(/#.*/, ""))
162 for (let frame in values(buffer.allFrames(win)))
163 overlay.setData(frame.document, "focus-allowed", false);
165 overlay.setData(win.document, "uri", uri.spec);
166 overlay.setData(win.document, "load-idx", webProgress.loadedTransIndex);
169 // Workaround for bugs 591425 and 606877, dactyl bug #81
170 let collapse = uri && uri.scheme === "dactyl" && webProgress.isLoadingDocument;
172 dactyl.focus(document.documentElement);
173 config.browser.mCurrentBrowser.collapsed = collapse;
175 util.timeout(function () {
176 browser._triggerLoadAutocmd("LocationChange",
177 (win || content).document,
181 // called at the very end of a page load
182 asyncUpdateUI: util.wrapCallback(function asyncUpdateUI() {
183 asyncUpdateUI.superapply(this, arguments);
184 util.timeout(function () { statusline.updateStatus(); }, 100);
186 setOverLink: util.wrapCallback(function setOverLink(link, b) {
187 setOverLink.superapply(this, arguments);
188 dactyl.triggerObserver("browser.overLink", link);
193 events: function initEvents(dactyl, modules, window) {
194 events.listen(config.browser, browser, "events", true);
196 commands: function initCommands(dactyl, modules, window) {
197 commands.add(["o[pen]"],
198 "Open one or more URLs in the current tab",
199 function (args) { dactyl.open(args[0] || "about:blank"); },
201 completer: function (context) completion.url(context),
202 domains: function (args) array.compact(dactyl.parseURLs(args[0] || "")
203 .map(url => util.getHost(url))),
208 commands.add(["redr[aw]"],
211 statusline.overLink = null;
212 statusline.updateStatus();
214 window.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowUtils)
219 mappings: function initMappings(dactyl, modules, window) {
220 let openModes = array.toObject([
221 [dactyl.CURRENT_TAB, ""],
222 [dactyl.NEW_TAB, "tab"],
223 [dactyl.NEW_BACKGROUND_TAB, "background tab"],
224 [dactyl.NEW_WINDOW, "win"]
227 function open(mode, args) {
228 if (dactyl.forceTarget in openModes)
229 mode = openModes[dactyl.forceTarget];
231 CommandExMode().open(mode + "open " + (args || ""));
234 function decode(uri) util.losslessDecodeURI(uri)
235 .replace(/%20(?!(?:%20)*$)/g, " ")
236 .replace(RegExp(options["urlseparator"], "g"), encodeURIComponent);
238 mappings.add([modes.NORMAL],
239 ["o"], "Open one or more URLs",
240 function () { open(""); });
242 mappings.add([modes.NORMAL], ["O"],
243 "Open one or more URLs, based on current location",
244 function () { open("", decode(buffer.uri.spec)); });
246 mappings.add([modes.NORMAL], ["s"],
247 "Open a search prompt",
248 function () { open("", options["defsearch"] + " "); });
250 mappings.add([modes.NORMAL], ["S"],
251 "Open a search prompt for a new tab",
252 function () { open("tab", options["defsearch"] + " "); });
254 mappings.add([modes.NORMAL], ["t"],
255 "Open one or more URLs in a new tab",
256 function () { CommandExMode().open("tabopen "); });
258 mappings.add([modes.NORMAL], ["T"],
259 "Open one or more URLs in a new tab, based on current location",
260 function () { open("tab", decode(buffer.uri.spec)); });
262 mappings.add([modes.NORMAL], ["w"],
263 "Open one or more URLs in a new window",
264 function () { open("win"); });
266 mappings.add([modes.NORMAL], ["W"],
267 "Open one or more URLs in a new window, based on current location",
268 function () { open("win", decode(buffer.uri.spec)); });
270 mappings.add([modes.NORMAL], ["<open-home-directory>", "~"],
271 "Open home directory",
272 function () { dactyl.open("~"); });
274 mappings.add([modes.NORMAL], ["<open-homepage>", "gh"],
276 function () { window.BrowserHome(); });
278 mappings.add([modes.NORMAL], ["<tab-open-homepage>", "gH"],
279 "Open homepage in a new tab",
281 let homepages = window.gHomeButton.getHomePage();
282 dactyl.open(homepages, { from: "homepage", where: dactyl.NEW_TAB });
285 mappings.add([modes.MAIN], ["<redraw-screen>", "<C-l>"],
287 function () { ex.redraw(); });
291 // vim: set fdm=marker sw=4 sts=4 ts=8 et: