welcome: please sign in
location: Focus

A lot of web designers out there seem to think they are doing us a favor by using javascript in their web pages to focus form elements like search boxes. For the mouse-oriented web surfer it probably is a favor because it saves them a click, but for the keyboard-oriented surfer these tricks can be incredibly annoying. Let's be clear about one thing: Mozilla provides no perfect solution that can allow Conkeror to do the right thing about focus-stealing 100% of the time. In the absence of perfection, we have figured out two pretty good hacks to fight back against the annoying web designers of the world. One of them works imperfectly for every focus-stealing event, and the other works perfectly for most focus-stealing events. You can choose which you want to use. The best combination might be both.

Imperfect, 100% of the Time

Conkeror's block-content-focus-change module watches for focus events, and permits or blocks them based on how recently the user hit a key or clicked the mouse. If the focus event happened within 20 milliseconds (by default) of a keypress or mouse click, it is assumed that the event resulted from that input event, and allowed to happen. Otherwise the newly focused element is immediately unfocused. To enable this mode, put this in your rc:

require("block-content-focus-change.js");

I hinted that the 20 millisecond duration could be configured. Here is how you could change it if Conkeror seems to be blocking focuses from your clicks (happens on slower computers):

block_content_focus_change_duration = 40;

Perfect, Most of the Time

This is a snippet you can put in your rc. It brutally goes through web pages as they download and replaces the "focus" method of form elements with a dummy function that does nothing. It never results in a false positive, but the one kind of focus event that it cannot deal with is when the focus call comes from inline javascript in the web page right after the form element that it focuses.

function focusblock (buffer) {
    var s = Components.utils.Sandbox(buffer.top_frame);
    s.document = buffer.document.wrappedJSObject;
    Components.utils.evalInSandbox(
        "(function () {\
            function nothing () {}\
            if (! document.forms)\
                return;\
            for (var i = 0, nforms = document.forms.length; i < nforms; i++) {\
              for (var j = 0, nels = document.forms[i].elements.length; j < nels; j++)\
                document.forms[i].elements[j].focus = nothing;\
            }\
          })();",
        s);
}
add_hook('content_buffer_progress_change_hook', focusblock);

Conkeror.org: Focus (last edited 2009-12-01 18:10:12 by df)