Search code examples
javascriptvimperator

Vimperator and Javascript: getting value of an object


Let's say I've just open a random Stackoverflow page, then I open Console in Firefox and run something like this:

alert(document.getElementsByClassName("question-hyperlink")["0"]["firstChild"].textContent)

As a result, it will pop up me the question asked on that page (on this particular page it would be Vimperator and Javascript: getting value of an object).

Now, I want to use that value in a script with Vimperator. I add something like this to my .vimperatorrc

:js << EOF
function dummyFu() {
    var myElement = document.getElementsByClassName("question-hyperlink");
    alert(myElement["0"].firstChild.textContent);
}
EOF
map <F5> :js dummyFu()<CR>

However, when run, it says TypeError: myElement[0] is undefined.

So, what is wrong? Would be grateful for any suggestion, thanks.


Solution

  • See https://developer.mozilla.org/en-US/docs/Web/API/Window/content

    E.g., content.document.getElementsByClassName("question-hyperlink")[0].firstChild.textContent;

    (No, it's not me, but all the credits go to GitHub user dkearns, actually. Thanks them a lot. :)