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?
Use
content.Use
content. Eg. The document rendered in the current tab iscontent.document.Incidentally, the top-level object you get with
windowrepresents 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
contentobject. Since it triggers automatically (unless you disable it), this means you can't even typecontent.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 theconten. (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:
Then, if you open the command line with :, type
myVarand press C-w, it will change the command-line toecho content.myVarand place the cursor at the end.(You can replace
echowithjsif you prefer - I findechomore convenient.)