jsbrowser: API to get resources from current page

This commit is contained in:
Kovid Goyal 2013-06-10 13:24:20 +05:30
parent 6306fbcd8c
commit 1651eedea8

View File

@ -238,10 +238,11 @@ class NetworkAccessManager(QNetworkAccessManager): # {{{
def report_reply(self, reply): def report_reply(self, reply):
reply_url = unicode(reply.url().toString()) reply_url = unicode(reply.url().toString())
self.reply_count += 1 self.reply_count += 1
err = reply.error()
if reply.error(): if err:
self.log.warn("Reply error: %s - %d (%s)" % l = self.log.debug if err == reply.OperationCanceledError else self.log.warn
(reply_url, reply.error(), reply.errorString())) l("Reply error: %s - %d (%s)" % (reply_url, err, unicode(reply.errorString())))
else: else:
debug = [] debug = []
debug.append("Reply successful: %s" % reply_url) debug.append("Reply successful: %s" % reply_url)
@ -542,6 +543,28 @@ class Browser(QObject, FormsMixin):
if iod is not None: if iod is not None:
return bytes(bytearray(iod.readAll())) return bytes(bytearray(iod.readAll()))
def wait_for_resources(self, urls, timeout=default_timeout):
timeout = self.default_timeout if timeout is default_timeout else timeout
start_time = time.time()
ans = {}
urls = set(urls)
def get_resources():
for url in tuple(urls):
raw = self.get_cached(url)
if raw is not None:
ans[url] = raw
urls.discard(url)
while urls and time.time() - start_time > timeout and self.page.ready_state not in {'complete', 'completed'}:
get_resources()
if urls:
self.run_for_a_time(0.1)
if urls:
get_resources()
return ans
def get_resource(self, url, rtype='img', use_cache=True, timeout=default_timeout): def get_resource(self, url, rtype='img', use_cache=True, timeout=default_timeout):
''' '''
Download a resource (image/stylesheet/script). The resource is Download a resource (image/stylesheet/script). The resource is
@ -595,11 +618,14 @@ class Browser(QObject, FormsMixin):
def html(self): def html(self):
return unicode(self.page.mainFrame().toHtml()) return unicode(self.page.mainFrame().toHtml())
def close(self): def blank(self):
try: try:
self.visit('about:blank', timeout=0.01) self.visit('about:blank', timeout=0.01)
except Timeout: except Timeout:
pass pass
def close(self):
self.blank()
self.nam = self.page = None self.nam = self.page = None
def __enter__(self): def __enter__(self):