bard / mozrepl

Remotely control Firefox and other Mozilla apps with JavaScript
https://github.com/bard/mozrepl/wiki/
510 stars 64 forks source link

Execute javascript in current web page context #48

Closed nisaacson closed 10 years ago

nisaacson commented 10 years ago

How would can I execute javascript in the currently loaded page via the repl?

# load https://example.com
repl> content.location.href = 'https://example.com'

# say that example.com exposes a global function called pageStatus(),
# how do I call that function and get the results back in the repl
repl> content.window.pageStatus() 
// returns !!! TypeError: content.window.pageStatus is not a function

Loading the page in firefox and using the developer tools console I can execute any of the following correctly

window.pageStatus()
content.pageStatus()
content.window.pageStatus()
pageStatus()
nisaacson commented 10 years ago

It turns out I need to enter the content first via the repl

repl> repl.enter(content)
repl> window.pageStatus()
# correct output returned here
repl> repl.back() # exit out of content context