2 * (C) Copyright 2004-2007 Shawn Betts
3 * (C) Copyright 2007-2009,2011-2012 John J. Foerch
4 * (C) Copyright 2007-2008 Jeremy Maitin-Shepard
6 * Use, modification, and distribution are subject to the terms specified in the
11 require("load-spec.js");
12 require("history.js");
14 define_variable("homepage", "chrome://conkeror-help/content/help.html",
15 "The url loaded by default for new content buffers.");
17 define_buffer_local_hook("content_buffer_finished_loading_hook");
18 define_buffer_local_hook("content_buffer_started_loading_hook");
19 define_buffer_local_hook("content_buffer_progress_change_hook");
20 define_buffer_local_hook("content_buffer_location_change_hook");
21 define_buffer_local_hook("content_buffer_status_change_hook");
22 define_buffer_local_hook("content_buffer_focus_change_hook");
23 define_buffer_local_hook("content_buffer_dom_link_added_hook");
24 define_buffer_local_hook("content_buffer_popup_blocked_hook");
26 define_current_buffer_hook("current_content_buffer_finished_loading_hook", "content_buffer_finished_loading_hook");
27 define_current_buffer_hook("current_content_buffer_progress_change_hook", "content_buffer_progress_change_hook");
28 define_current_buffer_hook("current_content_buffer_location_change_hook", "content_buffer_location_change_hook");
29 define_current_buffer_hook("current_content_buffer_status_change_hook", "content_buffer_status_change_hook");
30 define_current_buffer_hook("current_content_buffer_focus_change_hook", "content_buffer_focus_change_hook");
33 function content_buffer_modality (buffer) {
34 var elem = buffer.focused_element;
35 function push_keymaps (tag) {
36 buffer.content_modalities.map(
39 buffer.keymaps.push(m[tag]);
43 push_keymaps('normal');
45 let p = elem.parentNode;
46 while (p && !(p instanceof Ci.nsIDOMHTMLFormElement))
51 if (elem instanceof Ci.nsIDOMHTMLInputElement) {
52 var type = (elem.getAttribute("type") || "").toLowerCase();
53 if ({checkbox:1, radio:1, submit:1, reset:1}[type])
54 return push_keymaps(type);
56 return push_keymaps('text');
58 if (elem instanceof Ci.nsIDOMHTMLTextAreaElement)
59 return push_keymaps('textarea');
60 if (elem instanceof Ci.nsIDOMHTMLSelectElement)
61 return push_keymaps('select');
62 if (elem instanceof Ci.nsIDOMHTMLAnchorElement)
63 return push_keymaps('anchor');
64 if (elem instanceof Ci.nsIDOMHTMLButtonElement)
65 return push_keymaps('button');
66 if (elem instanceof Ci.nsIDOMHTMLEmbedElement ||
67 elem instanceof Ci.nsIDOMHTMLObjectElement)
69 return push_keymaps('embed');
71 var frame = buffer.focused_frame;
72 if (frame && frame.document.designMode &&
73 frame.document.designMode == "on")
75 return push_keymaps('richedit');
78 switch (elem.contentEditable) {
80 return push_keymaps('richedit');
84 elem = elem.parentNode;
91 define_keywords("$load");
92 function content_buffer (window) {
94 this.constructor_begin();
96 conkeror.buffer.call(this, window, forward_keywords(arguments));
98 this.browser.addProgressListener(this);
100 this.browser.addEventListener("DOMTitleChanged", function (event) {
101 buffer_title_change_hook.run(buffer);
102 }, true /* capture */);
104 this.browser.addEventListener("scroll", function (event) {
105 buffer_scroll_hook.run(buffer);
106 }, true /* capture */);
108 this.browser.addEventListener("focus", function (event) {
109 content_buffer_focus_change_hook.run(buffer, event);
110 }, true /* capture */);
112 this.browser.addEventListener("DOMLinkAdded", function (event) {
113 content_buffer_dom_link_added_hook.run(buffer, event);
114 }, true /* capture */);
116 this.browser.addEventListener("DOMPopupBlocked", function (event) {
117 dumpln("Blocked popup: " + event.popupWindowURI.spec);
118 content_buffer_popup_blocked_hook.run(buffer, event);
119 }, true /* capture */);
121 this.page_modes = [];
123 this.ignore_initial_blank = true;
125 var lspec = arguments.$load;
127 if (load_spec_uri_string(lspec) == "about:blank")
128 this.ignore_initial_blank = false;
130 /* Ensure that an existing load of about:blank is stopped */
131 this.web_navigation.stop(Ci.nsIWebNavigation.STOP_ALL);
137 this.modalities.push(content_buffer_modality);
138 this.content_modalities = [{
139 normal: content_buffer_normal_keymap,
140 form: content_buffer_form_keymap,
141 checkbox: content_buffer_checkbox_keymap,
142 radio: content_buffer_checkbox_keymap,
143 submit: content_buffer_button_keymap,
144 reset: content_buffer_button_keymap,
145 text: content_buffer_text_keymap,
146 textarea: content_buffer_textarea_keymap,
147 select: content_buffer_select_keymap,
148 anchor: content_buffer_anchor_keymap,
149 button: content_buffer_button_keymap,
150 embed: content_buffer_embed_keymap,
151 richedit: content_buffer_richedit_keymap
154 this.constructor_end();
157 content_buffer.prototype = {
158 constructor: content_buffer,
159 toString: function () "#<content_buffer>",
161 destroy: function () {
162 this.browser.removeProgressListener(this);
163 buffer.prototype.destroy.call(this);
166 content_modalities: null,
168 get scrollX () { return this.top_frame.scrollX; },
169 get scrollY () { return this.top_frame.scrollY; },
170 get scrollMaxX () { return this.top_frame.scrollMaxX; },
171 get scrollMaxY () { return this.top_frame.scrollMaxY; },
173 /* Used to display the correct URI when the buffer opens initially
174 * even before loading has progressed far enough for currentURI to
175 * contain the correct URI. */
178 get display_uri_string () {
179 if (this._display_uri)
180 return this._display_uri;
181 if (this.current_uri)
182 return this.current_uri.spec;
186 get title () { return this.browser.contentTitle; },
187 get description () { return this.display_uri_string; },
189 load: function (load_spec) {
190 apply_load_spec(this, load_spec);
197 // nsIWebProgressListener interface
199 QueryInterface: generate_QI(Ci.nsIWebProgressListener, Ci.nsISupportsWeakReference),
201 // This method is called to indicate state changes.
202 onStateChange: function (aWebProgress, aRequest, aStateFlags, aStatus) {
205 if (aStateFlags & Ci.nsIWebProgressListener.STATE_START) {
206 this._request_count++;
207 } else if (aStateFlags & Ci.nsIWebProgressListener.STATE_STOP) {
208 const NS_ERROR_UNKNOWN_HOST = 2152398878;
209 if (--this._request_count > 0 && aStatus == NS_ERROR_UNKNOWN_HOST) {
210 // to prevent bug 235825: wait for the request handled
211 // by the automatic keyword resolver
214 // since we (try to) only handle STATE_STOP of the last request,
215 // the count of open requests should now be 0
216 this._request_count = 0;
219 if (aStateFlags & Ci.nsIWebProgressListener.STATE_START &&
220 aStateFlags & Ci.nsIWebProgressListener.STATE_IS_NETWORK) {
221 // It's okay to clear what the user typed when we start
222 // loading a document. If the user types, this counter gets
223 // set to zero, if the document load ends without an
224 // onLocationChange, this counter gets decremented
225 // (so we keep it while switching tabs after failed loads)
227 content_buffer_started_loading_hook.run(this);
228 } else if (aStateFlags & Ci.nsIWebProgressListener.STATE_STOP &&
229 aStateFlags & Ci.nsIWebProgressListener.STATE_IS_NETWORK) {
230 if (this.loading == true) {
231 this.loading = false;
232 content_buffer_finished_loading_hook.run(this);
236 if (aStateFlags & (Ci.nsIWebProgressListener.STATE_STOP |
237 Ci.nsIWebProgressListener.STATE_START)) {
239 this.set_default_message("Done");
243 // This method is called to indicate progress changes for the
244 // currently loading page.
245 onProgressChange: function (webProgress, request, curSelf, maxSelf,
248 content_buffer_progress_change_hook.run(this, request, curSelf, maxSelf, curTotal, maxTotal);
251 // This method is called to indicate a change to the current location.
252 // The url can be gotten as location.spec.
253 onLocationChange: function (webProgress, request, location) {
254 /* Attempt to ignore onLocationChange calls due to the initial
255 * loading of about:blank by all xul:browser elements. */
256 if (location.spec == "about:blank" && this.ignore_initial_blank)
259 this.ignore_initial_blank = false;
261 //dumpln("spec: " + location.spec +" ;;; " + this.display_uri_string);
262 /* Use the real location URI now */
263 this._display_uri = null;
264 this.set_input_mode();
266 local: { __proto__: this.local }
268 this.default_browser_object_classes = {};
269 content_buffer_location_change_hook.run(this, request, location);
270 buffer_description_change_hook.run(this);
273 // This method is called to indicate a status changes for the currently
274 // loading page. The message is already formatted for display.
275 // Status messages could be displayed in the minibuffer output area.
276 onStatusChange: function (webProgress, request, status, msg) {
277 this.set_default_message(msg);
278 content_buffer_status_change_hook.run(this, request, status, msg);
281 // This method is called when the security state of the browser changes.
282 onSecurityChange: function (webProgress, request, state) {
283 //FIXME: implement this.
285 const WPL = Components.interfaces.nsIWebProgressListener;
287 if (state & WPL.STATE_IS_INSECURE) {
288 // update visual indicator
290 var level = "unknown";
291 if (state & WPL.STATE_IS_SECURE) {
292 if (state & WPL.STATE_SECURE_HIGH)
294 else if (state & WPL.STATE_SECURE_MED)
296 else if (state & WPL.STATE_SECURE_LOW)
298 } else if (state & WPL_STATE_IS_BROKEN) {
301 // provide a visual indicator of the security state here.
306 /* Inherit from buffer */
307 __proto__: buffer.prototype
311 add_hook("current_content_buffer_finished_loading_hook",
313 buffer.window.minibuffer.show("Done");
316 add_hook("current_content_buffer_status_change_hook",
317 function (buffer, request, status, msg) {
318 buffer.set_default_message(msg);
323 define_variable("read_url_handler_list", [],
324 "A list of handler functions which transform a typed url into a valid " +
325 "url or webjump. If the typed input is not valid then each function " +
326 "on this list is tried in turn. The handler function is called with " +
327 "a single string argument and it should return either a string or " +
328 "null. The result of the first function on the list that returns a " +
329 "string is used in place of the input.");
332 * read_url_make_default_webjump_handler returns a function that
333 * transforms any input into the given webjump. It should be the last
334 * handler on read_url_handler_list (because any input is
337 function read_url_make_default_webjump_handler (default_webjump) {
338 return function (input) {
339 return default_webjump + " " + input;
344 * read_url_make_blank_url_handler returns a function that replaces a
345 * blank (empty) input with the given url (or webjump). The url may
346 * perform some function, eg. "javascript:location.reload()".
348 function read_url_make_blank_url_handler (url) {
349 return function (input) {
350 if (input.length == 0)
356 function try_read_url_handlers (input) {
358 for (var i = 0; i < read_url_handler_list.length; ++i) {
359 if ((result = read_url_handler_list[i](input)))
365 define_variable("url_completion_use_webjumps", true,
366 "Specifies whether URL completion should complete webjumps.");
368 define_variable("url_completion_use_bookmarks", true,
369 "Specifies whether URL completion should complete bookmarks.");
371 define_variable("url_completion_use_history", false,
372 "Specifies whether URL completion should complete using browser "+
375 define_variable("url_completion_sort_order", "visitcount_descending",
376 "Gives the default sort order for history and bookmark completion.\n"+
377 "The value is given as a string, and the available options include: "+
378 "'none', 'title_ascending', 'date_ascending', 'uri_ascending', "+
379 "'visitcount_ascending', 'keyword_ascending', 'dateadded_ascending', "+
380 "'lastmodified_ascending', 'tags_ascending', and 'annotation_ascending'. "+
381 "For every 'ascending' option, there is a corresponding 'descending' "+
382 "option. Additionally, with XULRunner 6 and later, the options "+
383 "'frecency_ascending' and 'frecency_descending' are available. See also "+
384 "<https://developer.mozilla.org/en/NsINavHistoryQueryOptions#Sorting_methods>.");
386 define_variable("minibuffer_read_url_select_initial", true,
387 "Specifies whether a URL presented in the minibuffer for editing "+
388 "should be selected. This affects find-alternate-url.");
391 minibuffer_auto_complete_preferences["url"] = true;
392 minibuffer.prototype.read_url = function () {
393 keywords(arguments, $prompt = "URL:", $history = "url", $initial_value = "",
394 $use_webjumps = url_completion_use_webjumps,
395 $use_history = url_completion_use_history,
396 $use_bookmarks = url_completion_use_bookmarks,
397 $sort_order = url_completion_sort_order);
398 var completer = url_completer($use_webjumps = arguments.$use_webjumps,
399 $use_bookmarks = arguments.$use_bookmarks,
400 $use_history = arguments.$use_history,
401 $sort_order = arguments.$sort_order);
402 var result = yield this.read(
403 $prompt = arguments.$prompt,
404 $history = arguments.$history,
405 $completer = completer,
406 $initial_value = arguments.$initial_value,
407 $auto_complete = "url",
408 $select = minibuffer_read_url_select_initial,
409 $match_required = false);
410 if (!possibly_valid_url(result) && !get_webjump(result))
411 result = try_read_url_handlers(result);
412 if (result == "") // well-formedness check. (could be better!)
413 throw ("invalid url or webjump (\""+ result +"\")");
414 yield co_return(load_spec(result));
421 function overlink_update_status (buffer, node) {
422 if (node && node.href.length > 0)
423 buffer.window.minibuffer.show("Link: " + node.href);
425 buffer.window.minibuffer.clear();
428 function overlink_predicate (node) {
429 while (node && !(node instanceof Ci.nsIDOMHTMLAnchorElement))
430 node = node.parentNode;
434 function overlink_initialize (buffer) {
435 buffer.current_overlink = null;
436 buffer.overlink_mouseover = function (event) {
437 if (buffer != buffer.window.buffers.current ||
438 event.target == buffer.browser)
442 var node = overlink_predicate(event.target);
444 buffer.current_overlink = event.target;
445 overlink_update_status(buffer, node);
448 buffer.overlink_mouseout = function (event) {
449 if (buffer != buffer.window.buffers.current)
451 if (buffer.current_overlink == event.target) {
452 buffer.current_overlink = null;
453 overlink_update_status(buffer, null);
456 buffer.browser.addEventListener("mouseover", buffer.overlink_mouseover, true);
457 buffer.browser.addEventListener("mouseout", buffer.overlink_mouseout, true);
460 define_global_mode("overlink_mode",
462 add_hook("create_buffer_hook", overlink_initialize);
463 for_each_buffer(overlink_initialize);
465 function disable () {
466 remove_hook("create_buffer_hook", overlink_initialize);
467 for_each_buffer(function (b) {
468 b.browser.removeEventListener("mouseover", b.overlink_mouseover, true);
469 b.browser.removeEventListener("mouseout", b.overlink_mouseout, true);
470 delete b.current_overlink;
471 delete b.overlink_mouseover;
472 delete b.overlink_mouseout;
475 $doc = "Overlink-mode is a programmable mode for showing information "+
476 "about DOM nodes (such as link URLs) in the minibuffer when "+
477 "hovering with the mouse.");
483 * Navigation Commands
485 function go_back (b, prefix) {
487 go_forward(b, -prefix);
489 check_buffer(b, content_buffer);
491 if (b.web_navigation.canGoBack) {
492 var hist = b.web_navigation.sessionHistory;
493 var idx = hist.index - prefix;
496 b.web_navigation.gotoIndex(idx);
498 throw interactive_error("Can't go back");
502 "Go back in the session history for the current buffer.",
503 function (I) {go_back(I.buffer, I.p);});
506 function go_forward (b, prefix) {
510 check_buffer(b, content_buffer);
512 if (b.web_navigation.canGoForward) {
513 var hist = b.web_navigation.sessionHistory;
514 var idx = hist.index + prefix;
515 if (idx >= hist.count) idx = hist.count-1;
516 b.web_navigation.gotoIndex(idx);
518 throw interactive_error("Can't go forward");
521 interactive("forward",
522 "Go forward in the session history for the current buffer.",
523 function (I) {go_forward(I.buffer, I.p);});
526 function stop_loading (b) {
527 check_buffer(b, content_buffer);
528 b.web_navigation.stop(Ci.nsIWebNavigation.STOP_NETWORK);
531 interactive("stop-loading",
532 "Stop loading the current document.",
533 function (I) {stop_loading(I.buffer);});
536 function reload (b, bypass_cache, element, forced_charset) {
537 check_buffer(b, content_buffer);
539 if (element instanceof Ci.nsIDOMHTMLImageElement) {
541 var cache = Cc['@mozilla.org/image/cache;1']
542 .getService(Ci.imgICache);
543 cache.removeEntry(make_uri(element.src));
546 element.parentNode.replaceChild(element.cloneNode(true), element);
547 } else if (b.current_uri.spec != b.display_uri_string) {
548 apply_load_spec(b, load_spec({ uri: b.display_uri_string,
549 forced_charset: forced_charset }));
551 var flags = bypass_cache == null ?
552 Ci.nsIWebNavigation.LOAD_FLAGS_NONE :
553 Ci.nsIWebNavigation.LOAD_FLAGS_BYPASS_CACHE;
555 if (! forced_charset && forced_charset_list)
556 forced_charset = predicate_alist_match(forced_charset_list,
559 if (forced_charset) {
561 var atomservice = Cc['@mozilla.org/atom-service;1']
562 .getService(Ci.nsIAtomService);
563 b.web_navigation.documentCharsetInfo.forcedCharset =
564 atomservice.getAtom(forced_charset);
567 b.web_navigation.reload(flags);
571 interactive("reload",
572 "Reload the current document.\n" +
573 "If a prefix argument is specified, the cache is bypassed. If a "+
574 "DOM node is supplied via browser object, that node will be "+
577 check_buffer(I.buffer, content_buffer);
578 var element = yield read_browser_object(I);
579 reload(I.buffer, I.P, element, I.forced_charset);
581 $browser_object = null);
584 * browserDOMWindow: intercept window opening
586 function initialize_browser_dom_window (window) {
587 window.QueryInterface(Ci.nsIDOMChromeWindow).browserDOMWindow =
588 new browser_dom_window(window);
591 define_variable("browser_default_open_target", OPEN_NEW_BUFFER,
592 "Specifies how new window requests by content pages (e.g. by "+
593 "window.open from JavaScript or by using the target attribute of "+
594 "anchor and form elements) will be handled. This will generally "+
595 "be `OPEN_NEW_BUFFER', `OPEN_NEW_BUFFER_BACKGROUND', or "+
596 "`OPEN_NEW_WINDOW'.");
599 function browser_dom_window (window) {
600 this.window = window;
601 this.next_target = null;
603 browser_dom_window.prototype = {
604 constructor: browser_dom_window,
605 QueryInterface: generate_QI(Ci.nsIBrowserDOMWindow),
607 openURI: function (aURI, aOpener, aWhere, aContext) {
608 // Reference: http://www.xulplanet.com/references/xpcomref/ifaces/nsIBrowserDOMWindow.html
609 var target = this.next_target;
610 if (target == null || target == FOLLOW_DEFAULT)
611 target = browser_default_open_target;
612 this.next_target = null;
614 /* Determine the opener buffer */
615 var opener = get_buffer_from_frame(this.window, aOpener);
617 switch (browser_default_open_target) {
618 case OPEN_CURRENT_BUFFER:
620 case FOLLOW_CURRENT_FRAME:
622 case OPEN_NEW_BUFFER:
623 var buffer = new content_buffer(this.window, $opener = opener);
624 this.window.buffers.current = buffer;
625 return buffer.top_frame;
626 case OPEN_NEW_BUFFER_BACKGROUND:
627 var buffer = new content_buffer(this.window, $opener = opener);
628 return buffer.top_frame;
629 case OPEN_NEW_WINDOW:
630 default: /* shouldn't be needed */
632 /* We don't call make_window here, because that will result
633 * in the URL being loaded as the top-level document,
634 * instead of within a browser buffer. Instead, we can
635 * rely on Mozilla using browser.chromeURL. */
636 window_set_extra_arguments(
637 {initial_buffer_creator: buffer_creator(content_buffer, $opener = opener)}
644 add_hook("window_initialize_early_hook", initialize_browser_dom_window);
651 define_keywords("$test");
652 function page_mode (name, enable, disable) {
654 buffer_mode.call(this, name, enable, disable,
655 forward_keywords(arguments));
656 this.test = make_array(arguments.$test);
658 page_mode.prototype = {
659 constructor: page_mode,
660 __proto__: buffer_mode.prototype,
662 enable: function (buffer) {
663 buffer_mode.prototype.enable.call(this, buffer);
664 buffer.page_modes.push(this.name);
665 buffer.set_input_mode();
668 disable: function (buffer) {
669 buffer_mode.prototype.disable.call(this, buffer);
670 var i = buffer.page_modes.indexOf(this.name);
672 buffer.page_modes.splice(i, 1);
673 buffer.set_input_mode();
677 function define_page_mode (name, test, enable, disable) {
678 keywords(arguments, $constructor = page_mode);
679 define_buffer_mode(name, enable, disable,
681 forward_keywords(arguments));
683 ignore_function_for_get_caller_source_code_reference("define_page_mode");
686 define_keywords("$modality");
687 function keymaps_page_mode (name, enable, disable) {
689 page_mode.call(this, name, enable, disable,
690 forward_keywords(arguments));
691 this.modality = arguments.$modality;
693 keymaps_page_mode.prototype = {
694 constructor: keymaps_page_mode,
695 __proto__: page_mode.prototype,
697 _enable: function (buffer) {
698 buffer.content_modalities.push(this.modality);
700 _disable: function (buffer) {
701 var i = buffer.content_modalities.indexOf(this.modality);
703 buffer.content_modalities.splice(i, 1);
706 function define_keymaps_page_mode (name, test, modality) {
707 keywords(arguments, $constructor = keymaps_page_mode);
708 define_buffer_mode(name, null, null,
710 $modality = modality,
711 forward_keywords(arguments));
713 ignore_function_for_get_caller_source_code_reference("define_keymaps_page_mode");
716 var active_page_modes = [];
718 function page_mode_activate (page_mode) {
719 var i = active_page_modes.indexOf(page_mode.name);
721 active_page_modes.push(page_mode.name);
724 function page_mode_deactivate (page_mode) {
725 var i = active_page_modes.indexOf(page_mode.name);
727 active_page_modes.splice(i, 1);
731 function page_mode_update (buffer) {
732 for (var i = buffer.page_modes.length - 1; i >= 0; --i) {
733 var p = buffer.page_modes[i];
734 conkeror[p].disable(buffer);
736 var uri = buffer.current_uri;
737 for each (var name in active_page_modes) {
738 var m = conkeror[name];
741 if (test instanceof RegExp) {
742 if (test.exec(uri.spec))
743 return m.enable(buffer);
744 } else if (test(uri))
745 return m.enable(buffer);
751 add_hook("content_buffer_location_change_hook", page_mode_update);
753 provide("content-buffer");