2 * (C) Copyright 2008 Nicholas Zigarovich
4 * Use, modification, and distribution are subject to the terms specified in the
8 define_variable("clicks_in_new_buffer_button", 1,
9 "Which mouse button should open links in a new buffer. " +
10 "0 = left, 1 = middle, 2 = right. Default is 1.");
12 define_variable("clicks_in_new_buffer_target", OPEN_NEW_BUFFER,
13 "How to open links in a new buffer, in the foreground or " +
14 "the background. Set to one the constants OPEN_NEW_BUFFER " +
15 "or OPEN_NEW_BUFFER_BACKGROUND. Default is OPEN_NEW_BUFFER.");
17 // Should mouse click event propagation be stopped?
18 var clicks_in_new_buffer_ev_stop_prop = true;
20 function find_tag_in_parents(tag, element) {
21 // FIXME If tag names will always be upper-case, toLowerCase() can
22 // be eliminated. Also not sure that p will ever be null.
23 tag = tag.toLowerCase();
24 for (let p = element.parentNode;
25 p != null && p.tagName.toLowerCase() != "html";
28 if (p.tagName.toLowerCase() == tag)
34 function open_link_in_new_buffer(event) {
35 if (event.button != clicks_in_new_buffer_button)
37 let element = event.target;
39 if (element instanceof Ci.nsIDOMHTMLAnchorElement)
41 // FIXME The 'tostring() ==' is a terrible kludge.
42 else if (element.wrappedJSObject.toString() == "[object HTMLSpanElement]" ||
43 element instanceof Ci.nsIDOMHTMLImageElement)
45 anchor = find_tag_in_parents("a", element);
49 event.preventDefault();
50 if (clicks_in_new_buffer_ev_stop_prop)
51 event.stopPropagation();
52 let spec = element_get_load_spec(anchor);
53 // FIXME Is there a better way to get the window?
54 let window = window_watcher.activeWindow;
55 let buffer = window.buffers.current;
57 buffer_creator(content_buffer, $load = spec),
58 clicks_in_new_buffer_target);
61 function clicks_in_new_buffer_add_listener(buffer) {
62 buffer.browser.addEventListener("click",
63 open_link_in_new_buffer,
67 function clicks_in_new_buffer_remove_listener(buffer) {
68 buffer.browser.removeEventListener("click",
69 open_link_in_new_buffer,
73 function for_each_buffer(f) {
74 for_each_window(function (w) { w.buffers.for_each(f); });
77 function clicks_in_new_buffer_mode_enable() {
78 add_hook("create_buffer_hook",
79 clicks_in_new_buffer_add_listener);
80 for_each_buffer(clicks_in_new_buffer_add_listener);
83 function clicks_in_new_buffer_mode_disable() {
84 remove_hook("create_buffer_hook",
85 clicks_in_new_buffer_add_listener);
86 for_each_buffer(clicks_in_new_buffer_remove_listener);
89 define_global_mode("clicks_in_new_buffer_mode",
90 clicks_in_new_buffer_mode_enable,
91 clicks_in_new_buffer_mode_disable);
93 clicks_in_new_buffer_mode(true);