Add a world argument to tab.run_js_async

This commit is contained in:
Florian Bruhin 2016-09-12 15:59:03 +02:00
parent fa78cc9f69
commit a16c5a6a25
3 changed files with 24 additions and 6 deletions

View File

@ -747,11 +747,17 @@ class AbstractTab(QWidget):
"""
raise NotImplementedError
def run_js_async(self, code, callback=None):
def run_js_async(self, code, callback=None, *, world=None):
"""Run javascript async.
The given callback will be called with the result when running JS is
complete.
Args:
code: The javascript code to run.
callback: The callback to call with the result, or None.
world: An int world ID to run the JS in the main world or in another
isolated world.
"""
raise NotImplementedError

View File

@ -475,14 +475,24 @@ class WebEngineTab(browsertab.AbstractTab):
else:
self._widget.page().toHtml(callback)
def run_js_async(self, code, callback=None):
world = QWebEngineScript.ApplicationWorld
def run_js_async(self, code, callback=None, *, world=None):
if world is None:
world_id = QWebEngineScript.ApplicationWorld
else:
# We need to make this sure here, as otherwise we get an unexpected
# TypeError later...
if not isinstance(world, int):
raise TypeError("Expected int as world id, got {!r}".format(
world))
world_id = world
try:
if callback is None:
self._widget.page().runJavaScript(code, world)
self._widget.page().runJavaScript(code, world_id)
else:
self._widget.page().runJavaScript(code, world, callback)
self._widget.page().runJavaScript(code, world_id, callback)
except TypeError:
if world is not None:
log.webview.warning("Ignoring world ID on Qt < 5.7")
# Qt < 5.7
if callback is None:
self._widget.page().runJavaScript(code)

View File

@ -621,7 +621,9 @@ class WebKitTab(browsertab.AbstractTab):
else:
callback(frame.toHtml())
def run_js_async(self, code, callback=None):
def run_js_async(self, code, callback=None, *, world=None):
if world is not None:
log.webview.warning("Ignoring world ID {}".format(world))
result = self._widget.page().mainFrame().evaluateJavaScript(code)
if callback is not None:
callback(result)