Commit 8b7a8e11 authored by Melroy van den Berg's avatar Melroy van den Berg

my own mozrepl class

parent 87d6da92
#!/usr/bin/env python
#https://addons.mozilla.org/en-US/firefox/addon/mozrepl/
from time import sleep
import telnetlib
import socket
NR_RETRIES=800 # 240 sec (4 min.)/ 0.3 sec
READ_UNTIL_CHARS="repl1>"
class Mozrepl:
def __init__(self):
# telnet localhost 4242
try:
self.t = telnetlib.Telnet("localhost", 4242)
except socket.error:
print "ERROR: Could not connect to Firefox. Did you install Mozrepl plugin in Firefox?\nIf so, did you also start the service. In the menu: Extra->Mozrepl->Start."
exit(1)
self.skipData()
def load_page_blocking(self, url):
# Open url in current tab/window
self.writeData("content.location.href='"+url.strip()+"'")
self.skipData()
tries=0
while True:
# Get the page load state
self.writeData("document.readyState")
state = self.readData()
sleep(0.1)
if state == "\"complete\"":
# When completed wait a bit longer, in order to set repl again.
sleep(0.5)
break
tries+=1
if tries == NR_RETRIES:
print "ERROR: Too many retries. Do you have internet connection at all? Exit." # Time-out
exit(1)
# Set a temporally variable
self.writeData("repl1.enter(content)")
self.skipData()
self.writeData("document.temp=1")
# Experimental. TODO
# repl.execute('content').document.hidden1 = repl.execute('content').document.createElement("input")
# repl.execute('content').document.hidden1.type = "hidden"
# repl.execute('content').document.hidden1.value = "abc"
# repl.execute('content').document.hidden1.name = "aaa"
# repl.execute('content').document.getElementById("syi-form").appendChild(document.hidden1)
# Polling until 'temp' is becoming undefined again, meaning it's back to creation content (context unloading)
while True:
temp = self.writeData("document.temp")
temp = self.readData()
if temp != "1":
# Form is submitted or page is closed, we continue!
break
else:
print "Form not yet submitted"
sleep(3)
def writeData(self, str_buffer):
"""
Write data via write. New line at the end.
"""
self.t.write(str_buffer + "\n")
def readData(self):
"""
Read the buffer via read until, strip 'reply>' from the message
"""
i = len(READ_UNTIL_CHARS)
return self.t.read_until(READ_UNTIL_CHARS)[:-i].strip()
def skipData(self):
"""
Also read data until, but ignore the output,
get the buffer clean
"""
self.t.read_until(READ_UNTIL_CHARS)
def close(self):
self.t.close()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment