JavaScript Tricks for Pentadactyl
As I wrote plugins for Pentadactyl, I learned about by practice about some JavaScript tricks which are specific to this overwhelmingly good Firefox add-on.
I struggled to find my way in the JavaScript API. While most of the time you can and probably should get away with plain JavaScript, it can be useful to use some of the higher-level functions defined in Pentadactyl.
1 Useful Functions and Objects
Using the javascript
command, you will get to come across some useful functions and namespaces such as:
- The
content.document
object, which is the current document. That's how you can e.g. usecontent.document.getElementById()
. - The
dactyl
module may provide useful functions, such asbeep()
. For all I know, it could be used to set the urgency hint on Firefox windows, should Firefox allow it. It provides functions likeechomsg()
, undoubtedly used by:echomsg
. - The
editor
module offers theeditFieldExternally()
function, that tremendous feature which lets you edit forms with external editors.
2 The Buffer Module
The buffer
module lets you work with buffers which, in essence, are tabs (see :help buffer
).
- Functions like
scrollHorizontal()
,scrollVertical()
,scrollToPercent()
inbuffer
can help you scroll the way home row keys,<Space>
,<C-d>
, etc. would normally let you do it. Note that theunit
should be e.g.'lines'
or'pages'
. - It may be that the JavaScript plain
focus()
function isn't always good enough. I've had the case with atextarea
which stubbornly wouldn't take the focus. You may then prefer usingbuffer.focusElement()
instead. - While following links with the JavaScript plain
click()
function works, it may be useful to cause the click to follow the link in e.g. a background tab. This can be done with the Pentadactylbuffer.followLink()
function and thedactyl.NEW_TAB
,dactyl.NEW_WINDOW
, etc. parameters.
3 Development and Debugging
As explained in :help starting
, you can use the :rehash
command to have Pentadactyl reread everything, which is useful when you're in the development/testing loop.
The services.console.logStringMessage()
function occurring in common/content/dactyl.js
writes messages to the console. For some reason it doesn't always seem to work. You can also conveniently use the plain JavaScript throw "oops"
to display error messages in the status bar.
4 More Information
You may want to have a look at the function definitions and their usage in the Pentadactyl source itself:
- The
dactyl/common/modules/buffer.jsm
file defines the buffer-related functions. - While
followLink()
is defined indactyl/common/modules/buffer.jsm
, there are a few good examples on how to use it indactyl/content/hints.js
. - The
dactyl/content/editor.js
file defines e.g.editFieldExternally
.
Generally speaking, if you want to find out which functions is called for a particular command, you can try to:
- Look it up in
:help
to find the canonical name for the command: e.g.:help gg
is<scroll-top>
. - Go
grep
for the canonical name in the Pentadactyl source: e.g.grep -r '<scroll-top>' *
. - Find out how the mapping is defined for this command and which function is called: e.g.
<scroll-top>
triggers ascrollToPercent()
.