Focus the Right Frame with Pentadactyl
In the spirit of Vim, beyond giving your browser good key bindings, Pentadactyl lets you reprogram the web if needs be. For instance, focusing the right frame.
1 The Idea
Many websites have the despicable habit of having too many frames and somehow always focusing the wrong one. This is particularly annoying when you don't use your mouse and typing key bindings acts in the wrong frame. Pentadactyl can help there. The idea is to have an autocmd
focus it.
2 Feeding Keys Won't Work Well
The Pentadactyl key binding for focusing a frame is ;
to enter extended hints mode, then Shift F , then n where n is the on-screen number the hint system will give you – let's say 2. It's very tempting then to try and set up an autocmd
to type this key sequence once the page is fully loaded, something like:
au DOMLoad example.com/foo :normal ;F2
Unfortunately, the semicolon appears to be completely ignored and the end result comes across as just F 2 . This is perhaps because the semicolon is regarded as a command separator. To address this, Pentadactyl offers the :feedkeys
command which could be just what we need here. All that being said, would it not be altogether tidier to directly execute some JavaScript code to focus the right frame, instead of feeding keys to do so?
3 JavaScript and Choosing the Right Event
It is easier and more reliable to resort to JavaScript to focus an object in the right frame such that the usual motion key bindings are captured by that frame. You can obviously not focus any object – if you choose a textarea
for instance, motion keys would end up being typed verbatim into it, which isn't what you want. Choosing a larger container, e.g. the body
of the frame is probably best.
Having JavaScript focus an object in a background tab won't work. For instance, going for something along the lines of:
au DOMLoad example.com/foo -js doc.getElementById('bar').focus()
... will only work if you are looking at the page at this time. This is slightly frustrating, as Pentadactyl does provide you with the right doc
object to fiddle with. A working solution is to focus only when the page is being displayed, i.e. when the page becomes visible as you change tab. Rather than using the DOMLoad
event, this is made possible with a LocationChange
. Use this event and reach out for the content.document
object to make it work:
au LocationChange example.com/foo -js content.document.getElementById('bar').focus()