]> git.donarmstrong.com Git - dactyl.git/blob - common/content/browser.js
Imported Upstream version 1.1+hg7904
[dactyl.git] / common / content / browser.js
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>
4 //
5 // This work is licensed for reuse under an MIT license. Details are
6 // given in the LICENSE.txt file included with this file.
7 "use strict";
8
9 /** @scope modules */
10
11 /**
12  * @instance browser
13  */
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);
19
20         this._unoverlay = overlay.overlayObject(FullZoom, {
21             get siteSpecific() false,
22             set siteSpecific(val) {}
23         });
24     },
25
26     destroy: function () {
27         this.cleanupProgressListener();
28         this.observe.unregister();
29         this._unoverlay();
30     },
31
32     observers: {
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);
36
37             if (uri == "null")
38                 uri = null;
39
40             if (top == window && (win.location.href || uri))
41                 this._triggerLoadAutocmd("PageLoadPre", win.document, win.location.href || uri);
42         }
43     },
44
45     _triggerLoadAutocmd: function _triggerLoadAutocmd(name, doc, uri) {
46         if (!(uri || doc.location))
47             return;
48
49         uri = isObject(uri) ? uri : util.newURI(uri || doc.location.href);
50         let args = {
51             url: { toString: function () uri.spec, valueOf: function () uri },
52             title: doc.title
53         };
54
55         if (!dactyl.has("tabs"))
56             update(args, { doc: doc, win: doc.defaultView });
57         else {
58             args.tab = tabs.getContentIndex(doc) + 1;
59             args.doc = {
60                 valueOf: function () doc,
61                 toString: function () "tabs.getTab(" + (args.tab - 1) + ").linkedBrowser.contentDocument"
62             };
63             args.win = {
64                 valueOf: function () doc.defaultView,
65                 toString: function () "tabs.getTab(" + (args.tab - 1) + ").linkedBrowser.contentWindow"
66             };
67         }
68
69         autocommands.trigger(name, args);
70     },
71
72     events: {
73         DOMContentLoaded: function onDOMContentLoaded(event) {
74             let doc = event.originalTarget;
75             if (doc instanceof Ci.nsIDOMHTMLDocument)
76                 this._triggerLoadAutocmd("DOMLoad", doc);
77         },
78
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);
86
87             if (doc instanceof Ci.nsIDOMHTMLDocument) {
88                 if (doc.defaultView.frameElement) {
89                     // document is part of a frameset
90
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);
95                 }
96                 else {
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);
100
101                     this._triggerLoadAutocmd("PageLoad", doc);
102                 }
103             }
104         }
105     },
106
107     /**
108      * @property {Object} The document loading progress listener.
109      */
110     progressListener: {
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;
114
115             if (request)
116                 dactyl.applyTriggerObserver("browser.stateChange", arguments);
117
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))
124                         modes.pop();
125
126                 }
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();
132                 }
133             }
134
135             onStateChange.superapply(this, arguments);
136         }),
137         onSecurityChange: util.wrapCallback(function onSecurityChange(webProgress, request, state) {
138             onSecurityChange.superapply(this, arguments);
139             dactyl.applyTriggerObserver("browser.securityChange", arguments);
140         }),
141         onStatusChange: util.wrapCallback(function onStatusChange(webProgress, request, status, message) {
142             onStatusChange.superapply(this, arguments);
143             dactyl.applyTriggerObserver("browser.statusChange", arguments);
144         }),
145         onProgressChange: util.wrapCallback(function onProgressChange(webProgress, request, curSelfProgress, maxSelfProgress, curTotalProgress, maxTotalProgress) {
146             onProgressChange.superapply(this, arguments);
147             dactyl.applyTriggerObserver("browser.progressChange", arguments);
148         }),
149         // happens when the users switches tabs
150         onLocationChange: util.wrapCallback(function onLocationChange(webProgress, request, uri) {
151             onLocationChange.superapply(this, arguments);
152
153             dactyl.applyTriggerObserver("browser.locationChange", arguments);
154
155             let win = webProgress.DOMWindow;
156             if (win && uri) {
157                 Buffer(win).updateZoom();
158
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);
164
165                 overlay.setData(win.document, "uri", uri.spec);
166                 overlay.setData(win.document, "load-idx", webProgress.loadedTransIndex);
167             }
168
169             // Workaround for bugs 591425 and 606877, dactyl bug #81
170             let collapse = uri && uri.scheme === "dactyl" && webProgress.isLoadingDocument;
171             if (collapse)
172                 dactyl.focus(document.documentElement);
173             config.browser.mCurrentBrowser.collapsed = collapse;
174
175             util.timeout(function () {
176                 browser._triggerLoadAutocmd("LocationChange",
177                                             (win || content).document,
178                                             uri);
179             });
180         }),
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);
185         }),
186         setOverLink: util.wrapCallback(function setOverLink(link, b) {
187             setOverLink.superapply(this, arguments);
188             dactyl.triggerObserver("browser.overLink", link);
189         })
190     }
191 }, {
192 }, {
193     events: function initEvents(dactyl, modules, window) {
194         events.listen(config.browser, browser, "events", true);
195     },
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"); },
200             {
201                 completer: function (context) completion.url(context),
202                 domains: function (args) array.compact(dactyl.parseURLs(args[0] || "")
203                                                              .map(url => util.getHost(url))),
204                 literal: 0,
205                 privateData: true
206             });
207
208         commands.add(["redr[aw]"],
209             "Redraw the screen",
210             function () {
211                 statusline.overLink = null;
212                 statusline.updateStatus();
213                 commandline.clear();
214                 window.QueryInterface(Ci.nsIInterfaceRequestor).getInterface(Ci.nsIDOMWindowUtils)
215                       .redraw();
216             },
217             { argCount: "0" });
218     },
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"]
225         ]);
226
227         function open(mode, args) {
228             if (dactyl.forceTarget in openModes)
229                 mode = openModes[dactyl.forceTarget];
230
231             CommandExMode().open(mode + "open " + (args || ""));
232         }
233
234         function decode(uri) util.losslessDecodeURI(uri)
235                                  .replace(/%20(?!(?:%20)*$)/g, " ")
236                                  .replace(RegExp(options["urlseparator"], "g"), encodeURIComponent);
237
238         mappings.add([modes.NORMAL],
239             ["o"], "Open one or more URLs",
240             function () { open(""); });
241
242         mappings.add([modes.NORMAL], ["O"],
243             "Open one or more URLs, based on current location",
244             function () { open("", decode(buffer.uri.spec)); });
245
246         mappings.add([modes.NORMAL], ["s"],
247             "Open a search prompt",
248             function () { open("", options["defsearch"] + " "); });
249
250         mappings.add([modes.NORMAL], ["S"],
251             "Open a search prompt for a new tab",
252             function () { open("tab", options["defsearch"] + " "); });
253
254         mappings.add([modes.NORMAL], ["t"],
255             "Open one or more URLs in a new tab",
256             function () { CommandExMode().open("tabopen "); });
257
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)); });
261
262         mappings.add([modes.NORMAL], ["w"],
263             "Open one or more URLs in a new window",
264             function () { open("win"); });
265
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)); });
269
270         mappings.add([modes.NORMAL], ["<open-home-directory>", "~"],
271             "Open home directory",
272             function () { dactyl.open("~"); });
273
274         mappings.add([modes.NORMAL], ["<open-homepage>", "gh"],
275             "Open homepage",
276             function () { window.BrowserHome(); });
277
278         mappings.add([modes.NORMAL], ["<tab-open-homepage>", "gH"],
279             "Open homepage in a new tab",
280             function () {
281                 let homepages = window.gHomeButton.getHomePage();
282                 dactyl.open(homepages, { from: "homepage", where: dactyl.NEW_TAB });
283             });
284
285         mappings.add([modes.MAIN], ["<redraw-screen>", "<C-l>"],
286             "Redraw the screen",
287             function () { ex.redraw(); });
288     }
289 });
290
291 // vim: set fdm=marker sw=4 sts=4 ts=8 et: