2 * (C) Copyright 2007-2008 Jeremy Maitin-Shepard
3 * (C) Copyright 2008-2009 John J. Foerch
5 * Use, modification, and distribution are subject to the terms specified in the
10 * block-content-focus-change-mode tries very hard to prevent
11 * rude websites from changing the element focus via javascript.
14 define_variable("block_content_focus_change_duration", 20,
15 "Duration (in milliseconds) during which an element is "+
16 "allowed to gain focus following a mouse click or key press, "+
17 "if `block_content_focus_change_mode' is enabled.");;
19 function block_content_focus_change_track_input () {
21 browser
.block_content_focus_change_timestamp
= Date
.now();
24 function block_content_focus_change_reset (buffer
) {
25 buffer
.browser
.block_content_focus_change_timestamp
= null;
28 function block_content_focus_change_test (buffer
, event
) {
29 return !(event
.target
instanceof Ci
.nsIDOMHTMLAnchorElement
) &&
30 (buffer
.browser
.block_content_focus_change_timestamp
== null ||
31 (Date
.now() - buffer
.browser
.block_content_focus_change_timestamp
)
32 > block_content_focus_change_duration
);
35 function block_content_focus_change_buffer_setup (buffer
) {
36 if (! (buffer
instanceof content_buffer
) || buffer
.dead
)
38 block_content_focus_change_reset(buffer
);
39 buffer
.focusblocker
= block_content_focus_change_test
;
40 buffer
.browser
.addEventListener("mousedown",
41 block_content_focus_change_track_input
,
43 buffer
.browser
.addEventListener("keypress",
44 block_content_focus_change_track_input
,
48 function block_content_focus_change_buffer_teardown (buffer
) {
49 if (! (buffer
instanceof content_buffer
) || buffer
.dead
)
51 delete buffer
.browser
.block_content_focus_change_timestamp
;
52 buffer
.focusblocker
= null;
53 buffer
.browser
.removeEventListener("mousedown",
54 block_content_focus_change_track_input
,
56 buffer
.browser
.removeEventListener("keypress",
57 block_content_focus_change_track_input
,
62 let foo
= browser_set_element_focus
;
63 define_global_mode("block_content_focus_change_mode",
64 function () { // enable
65 for_each_buffer(function (buffer
) {
66 block_content_focus_change_buffer_setup(buffer
);
68 add_hook("create_buffer_hook",
69 block_content_focus_change_buffer_setup
);
70 add_hook("content_buffer_started_loading_hook",
71 block_content_focus_change_reset
);
72 add_hook("content_buffer_location_change_hook",
73 block_content_focus_change_reset
);
74 // this is a really sick hack, but still preferable to
75 // considered alternatives.
76 conkeror
.browser_set_element_focus
=
77 function (buffer
, elem
, prevent_scroll
) {
78 buffer
.browser
.block_content_focus_change_timestamp
= Date
.now();
79 foo
.call(this, buffer
, elem
, prevent_scroll
);
82 function () { // disable
83 for_each_buffer(function (buffer
) {
84 block_content_focus_change_buffer_teardown(buffer
);
86 remove_hook("create_buffer_hook",
87 block_content_focus_change_buffer_setup
);
88 remove_hook("content_buffer_started_loading_hook",
89 block_content_focus_change_reset
);
90 remove_hook("content_buffer_location_change_hook",
91 block_content_focus_change_reset
);
92 conkeror
.browser_set_element_focus
= foo
;
96 block_content_focus_change_mode(true);
98 provide("block-content-focus-change");