Add run_js_blocking to tab API
This commit is contained in:
parent
4f97b6342d
commit
5b1cca92ab
@ -611,6 +611,14 @@ class AbstractTab(QWidget):
|
|||||||
"""
|
"""
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
|
def run_js_blocking(self, code):
|
||||||
|
"""Run javascript and block.
|
||||||
|
|
||||||
|
This returns the result to the caller. Its use should be avoided when
|
||||||
|
possible as it runs a local event loop for QtWebEngine.
|
||||||
|
"""
|
||||||
|
raise NotImplementedError
|
||||||
|
|
||||||
def shutdown(self):
|
def shutdown(self):
|
||||||
raise NotImplementedError
|
raise NotImplementedError
|
||||||
|
|
||||||
|
@ -343,6 +343,21 @@ class WebEngineTab(browsertab.AbstractTab):
|
|||||||
else:
|
else:
|
||||||
self._widget.page().runJavaScript(code, callback)
|
self._widget.page().runJavaScript(code, callback)
|
||||||
|
|
||||||
|
def run_js_blocking(self, code):
|
||||||
|
loop = qtutils.EventLoop()
|
||||||
|
js_ret = None
|
||||||
|
|
||||||
|
def js_cb(val):
|
||||||
|
nonlocal js_ret
|
||||||
|
js_ret = val
|
||||||
|
loop.quit()
|
||||||
|
|
||||||
|
self.run_js_async(code, js_cb)
|
||||||
|
loop.exec_() # blocks until loop.quit() in js_cb
|
||||||
|
assert js_ret is not None
|
||||||
|
|
||||||
|
return js_ret
|
||||||
|
|
||||||
def shutdown(self):
|
def shutdown(self):
|
||||||
log.stub()
|
log.stub()
|
||||||
|
|
||||||
|
@ -525,10 +525,13 @@ class WebKitTab(browsertab.AbstractTab):
|
|||||||
callback(frame.toHtml())
|
callback(frame.toHtml())
|
||||||
|
|
||||||
def run_js_async(self, code, callback=None):
|
def run_js_async(self, code, callback=None):
|
||||||
result = self._widget.page().mainFrame().evaluateJavaScript(code)
|
result = self.run_js_blocking(code)
|
||||||
if callback is not None:
|
if callback is not None:
|
||||||
callback(result)
|
callback(result)
|
||||||
|
|
||||||
|
def run_js_blocking(self, code):
|
||||||
|
return self._widget.page().mainFrame().evaluateJavaScript(code)
|
||||||
|
|
||||||
def icon(self):
|
def icon(self):
|
||||||
return self._widget.icon()
|
return self._widget.icon()
|
||||||
|
|
||||||
|
@ -121,6 +121,12 @@ def test_tab(qtbot, view, config_stub, tab_registry):
|
|||||||
assert view.parent() is tab_w
|
assert view.parent() is tab_w
|
||||||
|
|
||||||
|
|
||||||
|
class TestJs:
|
||||||
|
|
||||||
|
def test_blocking(self, tab):
|
||||||
|
assert tab.run_js_blocking('1 + 1') == 2
|
||||||
|
|
||||||
|
|
||||||
class TestTabData:
|
class TestTabData:
|
||||||
|
|
||||||
def test_known_attr(self):
|
def test_known_attr(self):
|
||||||
|
Loading…
Reference in New Issue
Block a user