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 ||
40 element instanceof Ci.nsIDOMHTMLAreaElement)
44 anchor = find_tag_in_parents("a", element);
47 event.preventDefault();
48 if (clicks_in_new_buffer_ev_stop_prop)
49 event.stopPropagation();
50 let spec = load_spec(anchor);
51 let window = this.ownerDocument.defaultView;
52 let buffer = window.buffers.current;
54 buffer_creator(content_buffer,
57 clicks_in_new_buffer_target);
60 function clicks_in_new_buffer_add_listener (buffer) {
61 buffer.browser.addEventListener("click",
62 open_link_in_new_buffer,
66 function clicks_in_new_buffer_remove_listener (buffer) {
67 buffer.browser.removeEventListener("click",
68 open_link_in_new_buffer,
72 function clicks_in_new_buffer_mode_enable () {
73 add_hook("create_buffer_hook",
74 clicks_in_new_buffer_add_listener);
75 for_each_buffer(clicks_in_new_buffer_add_listener);
78 function clicks_in_new_buffer_mode_disable () {
79 remove_hook("create_buffer_hook",
80 clicks_in_new_buffer_add_listener);
81 for_each_buffer(clicks_in_new_buffer_remove_listener);
84 define_global_mode("clicks_in_new_buffer_mode",
85 clicks_in_new_buffer_mode_enable,
86 clicks_in_new_buffer_mode_disable);
88 clicks_in_new_buffer_mode(true);
90 provide("clicks-in-new-buffer");