How to read Mozrepl Javascript result into a Python Variable?

508 Views Asked by At

With Mozrepl addon in Firefox and Python I do:

>>> import telnetlib
>>> tn = telnetlib.Telnet(r'127.0.0.1', 4242, 5)
>>> tn.read_eager()
'\nWelcome to MozRepl.\n\n - If you get stuck at the "'
>>> tn.read_until("repl> ")
...snip...
>>> tn.write(r'alert(window.content.location.href)'+"\n")

and I get an alert box with the URL of the active tab. But how do I read that URL into a python variable? Something like tn.write(r';var zz = window.content.location.href'+ "\n") but that doesn't get it into python.

I would be grateful for help.

2

There are 2 best solutions below

0
innvo On

Bit late in answering but hopefully useful...

You just read from the telnet connection again and mozrepl's output will be there. Be sure to watch out for newlines returned and quoted strings.

0
c2o93y50 On

This can be done using the pymozrepl module.

>>> import mozrepl
>>> repl = mozrepl.Mozrepl()
>>> 
>>> repl.execute('alert(window.content.location.href)')
>>> 
>>> #or
>>> from mozrepl.type import Raw
>>> repl.execute('alert')(Raw('window.content.location.href'))
>>> 
>>> #or
>>> repl.execute('alert')(repl.execute('window').content.location.href)
>>> 
>>> #or
>>> repl.execute('alert')(repl.execute('window.content.location.href'))
>>> 
>>> #pymozrepl module also can get javascript value.
>>> repl.execute('repl._name')
'repl'