How do I get the window object of a page from Vimperator?

111 views Asked by At

I'm using:

  • Firefox 46
  • Vimperator 3.13

I want to use Vimperator to inject javascript inside a webpage, a bit like Grease Monkey would do, but by using Vimperator and its :js command.

But the Vimperator :js command and the current html page have different scopes, and I can't find out how to ref to the current page window object, from :js.

For example, if I open the Firefox web console, and I run:

window.myvar = 10

Then, if I run the following inside the Vimperator's toolbar:

:js alert(window.myvar)

, then it displays undefined.

So, how do I get the current page window object from Vimperator?

1

There are 1 answers

0
pyrocrasty On

Use content.

Use content. Eg. The document rendered in the current tab is content.document.

Incidentally, the top-level object you get with window represents the Firefox GUI.

warning:

There's an ancient bug in both Vimperator and Pentadactyl that crashes Firefox if you try to use Vimperator's command-line autocompletion on the content object. Since it triggers automatically (unless you disable it), this means you can't even type content. in the command-line - it will trigger the autocompletion and immediately crash the browser.

What you have to do instead is either paste in the command, or type say, t.myVar, and then jump back to fill in the conten. (Don't move the cursor directly back over . - use Home or something.) It's really a pain in the arse, but there it is.

You can use autocompletion once you're deeper in, just not with content.

If you need to explore the window object, you have to do it in another tool - Firebug, developer tools, etc... Browser Console (C-S-J) is a good choice since it matches what you see in Vimperator closely.

workaround

You could use a key binding like this:

:cmap <C-w> <Home>echo content.<End>

Then, if you open the command line with :, type myVar and press C-w, it will change the command-line to echo content.myVar and place the cursor at the end.
(You can replace echo with js if you prefer - I find echo more convenient.)