5digits / dactyl

Pentadactyl and other related Gecko extensions
Other
469 stars 98 forks source link

Pentadactyl + JavaScript: getting value of an object? #196

Closed for-coursera closed 8 years ago

for-coursera commented 8 years ago

Version:

Pentadactyl hg7290 (created 2016/07/19 07:00:08) running on:
Mozilla/5.0 (Macintosh; Intel Mac OS X 10.8; rv:45.0) Gecko/20100101 Firefox/45.0

Firefox.app in question is actually Firefox Extended Support Release, which still seems to be the latest Firefox available.

Issue:

Let's say I open a random issue page on a GitHub tracker (like this one), then I open Console in Firefox and run something like this:

alert(document.getElementsByClassName("js-issue-title")["0"]["firstChild"].textContent)

As a result, it pops me up with the title of an issue (speaking of this particular issue, it would be How to achieve Ctrl-t functionality).

Now, I want to use that value in a script with Pentadactyl. Thus, I add something like this to my .pentadactylrc

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

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

I'm do not know much of JavaScript, unfortunately, so there's probably some mistake in my thinking? I just want to be able to get an element from a page by a class and then use it my script... Would be grateful for any suggestion, thanks.

dkearns commented 8 years ago

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