2 * (C) Copyright 2007-2009 John J. Foerch
3 * (C) Copyright 2007-2008 Jeremy Maitin-Shepard
5 * Portions of this file are derived from Vimperator,
6 * (C) Copyright 2006-2007 Martin Stubenschrott.
8 * Use, modification, and distribution are subject to the terms specified in the
14 require("mime-type-override.js");
15 require("minibuffer-read-mime-type.js");
17 var browser_object_classes = {};
20 * browser_object_class
22 * In normal cases, make a new browser_object_class with the function,
23 * `define_browser_object_class'.
25 * name: See note on `define_browser_object_class'.
29 * handler: a coroutine called as: handler(I, prompt). `I' is a normal
30 * interactive context. `prompt' is there to pass along as the
31 * $prompt of various minibuffer read procedures, if needed.
33 * $hint: short string (usually verb and noun) to describe the UI
34 * of the browser object class to the user. Only used by
35 * browser object classes which make use of the minibuffer.
37 define_keywords("$hint");
38 function browser_object_class (name, doc, handler) {
41 this.handler = handler;
43 this.hint = arguments.$hint;
47 * define_browser_object_class
49 * In normal cases, make a new browser_object_class with the function,
50 * `define_browser_object_class'.
52 * name: the name of the browser object class. multiword names should be
53 * hyphenated. From this name, a variable browser_object_NAME and
54 * an interactive command browser-object-NAME will be generated.
56 * Other arguments are as for `browser_object_class'.
59 function define_browser_object_class (name, doc, handler) {
61 var varname = 'browser_object_'+name.replace('-','_','g');
62 var ob = conkeror[varname] =
63 new browser_object_class(name, doc, handler,
64 forward_keywords(arguments));
65 interactive("browser-object-"+name,
66 "A prefix command to specify that the following command operate "+
67 "on objects of type: "+name+".",
68 function (I) { I.browser_object = ob; },
74 * xpath_browser_object_handler
76 * This generates a function of the type needed for a handler of a
77 * browser object class. The handler uses `read_hinted_element' of
78 * hints.js to let the user pick a DOM node from those matched by
81 function xpath_browser_object_handler (xpath_expression) {
82 return function (I, prompt) {
83 var result = yield I.buffer.window.minibuffer.read_hinted_element(
86 $hint_xpath_expression = xpath_expression);
87 yield co_return(result);
91 define_browser_object_class("images",
92 "Browser object class for selecting an html:img via hinting.",
93 xpath_browser_object_handler("//img | //xhtml:img"),
94 $hint = "select image");
96 define_browser_object_class("frames",
97 "Browser object class for selecting a frame or iframe via hinting.",
98 function (I, prompt) {
99 var doc = I.buffer.document;
100 // Check for any frames or visible iframes
101 var skip_hints = true;
102 if (doc.getElementsByTagName("frame").length > 0)
105 let topwin = I.buffer.top_frame;
106 let iframes = doc.getElementsByTagName("iframe");
107 for (var i = 0, nframes = iframes.length; i < nframes; i++) {
108 let style = topwin.getComputedStyle(iframes[i], "");
109 if (style.display == "none" || style.visibility == "hidden")
116 // only one frame (the top-level one), no need to use the hints system
117 yield co_return(I.buffer.top_frame);
119 var result = yield I.buffer.window.minibuffer.read_hinted_element(
122 $hint_xpath_expression = "//iframe | //frame | //xhtml:iframe | //xhtml:frame");
123 yield co_return(result);
125 $hint = "select frame");
127 define_browser_object_class("links",
128 "Browser object class for selecting a hyperlink, form field, "+
129 "or link-like element, via hinting.",
130 xpath_browser_object_handler(
131 "//*[@onclick or @onmouseover or @onmousedown or @onmouseup or @oncommand or @role='link'] | " +
132 "//input[not(@type='hidden')] | //a | //area | //iframe | //textarea | //button | //select | " +
133 "//*[@contenteditable = 'true'] |" +
134 "//xhtml:*[@onclick or @onmouseover or @onmousedown or @onmouseup or @oncommand or @role='link'] | " +
135 "//xhtml:input[not(@type='hidden')] | //xhtml:a | //xhtml:area | //xhtml:iframe | //xhtml:textarea | //xhtml:button | //xhtml:select | " +
136 "//xhtml:*[@contenteditable = 'true']"),
137 $hint = "select link");
139 define_browser_object_class("mathml",
140 "Browser object class for selecting a MathML node via hinting.",
141 xpath_browser_object_handler("//m:math"),
142 $hint = "select MathML element");
144 define_browser_object_class("top",
145 "Browser object class which returns the top frame of the document.",
146 function (I, prompt) { return I.buffer.top_frame; });
148 define_browser_object_class("url",
149 "Browser object class which prompts the user for an url or webjump.",
150 function (I, prompt) {
151 var result = yield I.buffer.window.minibuffer.read_url($prompt = prompt);
152 yield co_return(result);
154 $hint = "enter URL/webjump");
156 define_browser_object_class("paste-url",
157 "Browser object which reads an url from the X Primary Selection, "+
158 "falling back on the clipboard for operating systems which lack one.",
159 function (I, prompt) {
160 var url = read_from_x_primary_selection();
162 url = url.replace(/^\s*|\s*$/,"");
163 // add http:// if needed
164 if (url.match(/^[^:]+\./)) {
165 url = "http://" + url;
168 return make_uri(url).spec;
170 throw new interactive_error("error: malformed url: "+url);
174 define_browser_object_class("file",
175 "Browser object which prompts for a file name.",
176 function (I, prompt) {
177 var result = yield I.buffer.window.minibuffer.read_file(
179 $history = I.command.name+"/file",
180 $initial_value = I.local.cwd.path);
181 yield co_return(result);
183 $hint = "enter file name");
185 define_browser_object_class("alt",
186 "Browser object class which returns the alt text of an html:img, "+
187 "selected via hinting",
188 function (I, prompt) {
189 var result = yield I.buffer.window.minibuffer.read_hinted_element(
192 $hint_xpath_expression = "//img[@alt] | //xhtml:img[@alt]");
193 yield co_return(result.alt);
195 $hint = "select image for alt-text");
197 define_browser_object_class("title",
198 "Browser object class which returns the title attribute of an element, "+
199 "selected via hinting",
200 function (I, prompt) {
201 var result = yield I.buffer.window.minibuffer.read_hinted_element(
204 $hint_xpath_expression = "//*[@title] | //xhtml:*[@title]");
205 yield co_return(result.title);
207 $hint = "select element for title attribute");
209 define_browser_object_class("title-or-alt",
210 "Browser object which is the union of browser-object-alt and "+
211 "browser-object-title, with title having higher precedence in "+
212 "the case of an element that has both.",
213 function (I, prompt) {
214 var result = yield I.buffer.window.minibuffer.read_hinted_element(
217 $hint_xpath_expression = "//img[@alt] | //*[@title] | //xhtml:img[@alt] | //xhtml:*[@title]");
218 yield co_return(result.title ? result.title : result.alt);
220 $hint = "select element for title or alt-text");
222 define_browser_object_class("scrape-url",
223 "Browser object which lets the user choose an url from a list of "+
224 "urls scraped from the source code of the document.",
225 function (I, prompt) {
226 var completions = I.buffer.document.documentElement.innerHTML
227 .match(/http:[^\s>"]*/g)
228 .filter(remove_duplicates_filter());
229 var completer = prefix_completer($completions = completions);
230 var result = yield I.buffer.window.minibuffer.read(
232 $completer = completer,
233 $initial_value = null,
234 $auto_complete = "url",
236 $match_required = false);
237 yield co_return(result);
239 $hint = "choose scraped URL");
241 define_browser_object_class("up-url",
242 "Browser object which returns the url one level above the current one.",
243 function (I, prompt) {
244 var up = compute_url_up_path(I.buffer.current_uri.spec);
245 return I.buffer.current_uri.resolve(up);
248 define_browser_object_class("focused-element",
249 "Browser object which returns the focused element.",
250 function (I, prompt) { return I.buffer.focused_element; });
252 define_browser_object_class("dom-node", null,
253 xpath_browser_object_handler("//* | //xhtml:*"),
254 $hint = "select DOM node");
256 interactive("browser-object-text",
257 "Composable browser object which returns the text of another object.",
259 // our job here is to modify the interactive context.
260 // set I.browser_object to a browser_object which calls the
261 // original one, then returns its text.
262 var b = I.browser_object;
263 I.browser_object = function (I) {
264 I.browser_object = b;
265 var e = yield read_browser_object(I);
266 if (e instanceof Ci.nsIDOMHTMLImageElement)
267 yield co_return(e.getAttribute("alt"));
268 yield co_return(e.textContent);
273 function read_browser_object (I) {
274 var browser_object = I.browser_object;
276 // literals cannot be overridden
277 if (browser_object instanceof Function) {
278 result = yield browser_object(I);
279 yield co_return(result);
281 if (! (browser_object instanceof browser_object_class))
282 yield co_return(browser_object);
284 var prompt = I.command.prompt;
286 prompt = I.command.name.split(/-|_/).join(" ");
287 prompt = prompt[0].toUpperCase() + prompt.substring(1);
289 if (I.target != null)
290 prompt += TARGET_PROMPTS[I.target];
291 if (browser_object.hint)
292 prompt += " (" + browser_object.hint + ")";
295 result = yield browser_object.handler.call(null, I, prompt);
296 yield co_return(result);
301 * This is a simple wrapper function that sets focus to elem, and
302 * bypasses the automatic focus prevention system, which might
303 * otherwise prevent this from happening.
305 function browser_set_element_focus (buffer, elem, prevent_scroll) {
306 if (!element_dom_node_or_window_p(elem))
309 set_focus_no_scroll(buffer.window, elem);
314 function browser_element_focus (buffer, elem) {
315 if (!element_dom_node_or_window_p(elem))
318 if (elem instanceof Ci.nsIDOMXULTextBoxElement)
319 elem = elem.wrappedJSObject.inputField; // focus the input field
321 browser_set_element_focus(buffer, elem);
322 if (elem instanceof Ci.nsIDOMWindow)
325 // If it is not a window, it must be an HTML element
328 if (elem instanceof Ci.nsIDOMHTMLFrameElement ||
329 elem instanceof Ci.nsIDOMHTMLIFrameElement)
331 elem.contentWindow.focus();
334 if (elem instanceof Ci.nsIDOMHTMLAreaElement) {
335 var coords = elem.getAttribute("coords").split(",");
336 x = Number(coords[0]);
337 y = Number(coords[1]);
340 var doc = elem.ownerDocument;
341 var evt = doc.createEvent("MouseEvents");
343 evt.initMouseEvent("mouseover", true, true, doc.defaultView, 1, x, y, 0, 0, 0, 0, 0, 0, 0, null);
344 elem.dispatchEvent(evt);
347 function browser_object_follow (buffer, target, elem) {
348 // XXX: would be better to let nsILocalFile objects be load_specs
349 if (elem instanceof Ci.nsILocalFile)
353 if (elem instanceof load_spec)
354 e = load_spec_element(elem);
358 browser_set_element_focus(buffer, e, true /* no scroll */);
360 var no_click = (((elem instanceof load_spec) &&
361 load_spec_forced_charset(elem)) ||
362 (e instanceof load_spec) ||
363 (e instanceof Ci.nsIDOMWindow) ||
364 (e instanceof Ci.nsIDOMHTMLFrameElement) ||
365 (e instanceof Ci.nsIDOMHTMLIFrameElement) ||
366 (e instanceof Ci.nsIDOMHTMLLinkElement) ||
367 (e instanceof Ci.nsIDOMHTMLImageElement &&
368 !e.hasAttribute("onmousedown") && !e.hasAttribute("onclick")));
370 if (target == FOLLOW_DEFAULT && !no_click) {
372 if (e instanceof Ci.nsIDOMHTMLAreaElement) {
373 var coords = e.getAttribute("coords").split(",");
374 if (coords.length >= 2) {
375 x = Number(coords[0]) + 1;
376 y = Number(coords[1]) + 1;
379 dom_node_click(e, x, y);
383 var spec = load_spec(elem);
385 if (load_spec_uri_string(spec).match(/^\s*javascript:/)) {
386 // it is nonsensical to follow a javascript url in a different
388 target = FOLLOW_DEFAULT;
389 } else if (!(buffer instanceof content_buffer) &&
390 (target == FOLLOW_CURRENT_FRAME ||
391 target == FOLLOW_DEFAULT ||
392 target == OPEN_CURRENT_BUFFER))
394 target = OPEN_NEW_BUFFER;
398 case FOLLOW_CURRENT_FRAME:
399 var current_frame = load_spec_source_frame(spec);
400 if (current_frame && current_frame != buffer.top_frame) {
401 var target_obj = get_web_navigation_for_frame(current_frame);
402 apply_load_spec(target_obj, spec);
406 case OPEN_CURRENT_BUFFER:
409 case OPEN_NEW_WINDOW:
410 case OPEN_NEW_BUFFER:
411 case OPEN_NEW_BUFFER_BACKGROUND:
412 create_buffer(buffer.window,
413 buffer_creator(content_buffer,
421 * Follow a link-like element by generating fake mouse events.
423 function dom_node_click (elem, x, y) {
424 var doc = elem.ownerDocument;
425 var view = doc.defaultView;
427 var evt = doc.createEvent("MouseEvents");
428 evt.initMouseEvent("mousedown", true, true, view, 1, x, y, 0, 0, /*ctrl*/ 0, /*event.altKey*/0,
429 /*event.shiftKey*/ 0, /*event.metaKey*/ 0, 0, null);
430 elem.dispatchEvent(evt);
432 evt = doc.createEvent("MouseEvents");
433 evt.initMouseEvent("click", true, true, view, 1, x, y, 0, 0, /*ctrl*/ 0, /*event.altKey*/0,
434 /*event.shiftKey*/ 0, /*event.metaKey*/ 0, 0, null);
435 elem.dispatchEvent(evt);
439 function follow (I, target) {
441 target = FOLLOW_DEFAULT;
443 if (target == OPEN_CURRENT_BUFFER)
444 check_buffer(I.buffer, content_buffer);
445 var element = yield read_browser_object(I);
447 element = load_spec(element);
448 if (I.forced_charset)
449 element.forced_charset = I.forced_charset;
451 browser_object_follow(I.buffer, target, element);
454 function follow_new_buffer (I) {
455 yield follow(I, OPEN_NEW_BUFFER);
458 function follow_new_buffer_background (I) {
459 yield follow(I, OPEN_NEW_BUFFER_BACKGROUND);
462 function follow_new_window (I) {
463 yield follow(I, OPEN_NEW_WINDOW);
466 function follow_current_frame (I) {
467 yield follow(I, FOLLOW_CURRENT_FRAME);
470 function follow_current_buffer (I) {
471 yield follow(I, OPEN_CURRENT_BUFFER);
475 function element_get_load_target_label (element) {
476 if (element instanceof Ci.nsIDOMWindow)
478 if (element instanceof Ci.nsIDOMHTMLFrameElement)
480 if (element instanceof Ci.nsIDOMHTMLIFrameElement)
485 function element_get_operation_label (element, op_name, suffix) {
486 var target_label = element_get_load_target_label(element);
487 if (target_label != null)
488 target_label = " " + target_label;
493 suffix = " " + suffix;
497 return op_name + target_label + suffix + ":";
501 function browser_element_copy (buffer, elem) {
504 spec = load_spec(elem);
507 if (typeof elem == "string" || elem instanceof String)
510 text = load_spec_uri_string(spec);
512 if (!(elem instanceof Ci.nsIDOMNode))
513 throw interactive_error("Element has no associated text to copy.");
514 switch (elem.localName) {
520 if (elem.selectedIndex >= 0)
521 text = elem.item(elem.selectedIndex).text;
524 text = elem.textContent;
528 browser_set_element_focus(buffer, elem);
529 writeToClipboard(text);
530 buffer.window.minibuffer.message("Copied: " + text);
534 define_variable("view_source_use_external_editor", false,
535 "When true, the `view-source' command will send its document to "+
536 "your external editor.");
538 define_variable("view_source_function", null,
539 "May be set to a user-defined function for viewing source code. "+
540 "The function should accept an nsILocalFile of the filename as "+
541 "its one positional argument, and it will also be called with "+
542 "the keyword `$temporary', whose value will be true if the file "+
543 "is considered temporary, and therefore the function must take "+
544 "responsibility for deleting it.");
546 function browser_object_view_source (buffer, target, elem) {
547 if (view_source_use_external_editor || view_source_function) {
548 var spec = load_spec(elem);
550 let [file, temp] = yield download_as_temporary(spec,
552 $action = "View source");
553 if (view_source_use_external_editor)
554 yield open_file_with_external_editor(file, $temporary = temp);
556 yield view_source_function(file, $temporary = temp);
561 var window = buffer.window;
562 if (elem.localName) {
563 switch (elem.localName.toLowerCase()) {
564 case "frame": case "iframe":
565 win = elem.contentWindow;
568 view_mathml_source(window, charset, elem);
571 throw new Error("Invalid browser element");
577 var url_s = win.location.href;
578 if (url_s.substring (0,12) != "view-source:") {
580 browser_object_follow(buffer, target, "view-source:" + url_s);
581 } catch(e) { dump_error(e); }
583 window.minibuffer.message ("Already viewing source");
587 function view_source (I, target) {
590 target = OPEN_CURRENT_BUFFER;
591 var element = yield read_browser_object(I);
592 yield browser_object_view_source(I.buffer, target, element);
595 function view_source_new_buffer (I) {
596 yield view_source(I, OPEN_NEW_BUFFER);
599 function view_source_new_window (I) {
600 yield view_source(I, OPEN_NEW_WINDOW);
604 function browser_element_shell_command (buffer, elem, command, cwd) {
605 var spec = load_spec(elem);
606 yield download_as_temporary(spec,
608 $shell_command = command,
609 $shell_command_cwd = cwd);