diff --git a/doc/help/commands.asciidoc b/doc/help/commands.asciidoc index 42ed2b331..5ac7dc935 100644 --- a/doc/help/commands.asciidoc +++ b/doc/help/commands.asciidoc @@ -447,7 +447,7 @@ Evaluate a JavaScript string. ==== optional arguments * +*-q*+, +*--quiet*+: Don't show resulting JS object. -* +*-w*+, +*--world*+: Ignored on QtWebKit. On QtWebEngine, which JS world/context to run the snippet in. +* +*-w*+, +*--world*+: Ignored on QtWebKit. On QtWebEngine, a world ID or name to run the snippet in. ==== note diff --git a/qutebrowser/browser/commands.py b/qutebrowser/browser/commands.py index 9a7f9d3b2..bde6bd10c 100644 --- a/qutebrowser/browser/commands.py +++ b/qutebrowser/browser/commands.py @@ -1918,14 +1918,15 @@ class CommandDispatcher: @cmdutils.register(instance='command-dispatcher', scope='window', maxsplit=0, no_cmd_split=True) - def jseval(self, js_code, quiet=False, *, world: int=None): + def jseval(self, js_code, quiet=False, *, + world: typing.Union[usertypes.JsWorld, int]=None): """Evaluate a JavaScript string. Args: js_code: The string to evaluate. quiet: Don't show resulting JS object. - world: Ignored on QtWebKit. On QtWebEngine, a world ID to run the - snippet in. + world: Ignored on QtWebKit. On QtWebEngine, a world ID or name to + run the snippet in. """ if quiet: jseval_cb = None diff --git a/qutebrowser/browser/webengine/webenginetab.py b/qutebrowser/browser/webengine/webenginetab.py index 5fcc4dacb..6d139150b 100644 --- a/qutebrowser/browser/webengine/webenginetab.py +++ b/qutebrowser/browser/webengine/webenginetab.py @@ -48,6 +48,14 @@ def init(): req_interceptor.install(QWebEngineProfile.defaultProfile()) +# Mapping worlds from usertypes.JsWorld to QWebEngineScript world IDs. +_JS_WORLD_MAP = { + usertypes.JsWorld.main: QWebEngineScript.MainWorld, + usertypes.JsWorld.application: QWebEngineScript.ApplicationWorld, + usertypes.JsWorld.user: QWebEngineScript.UserWorld, +} + + class WebEnginePrinting(browsertab.AbstractPrinting): """QtWebEngine implementations related to printing.""" @@ -478,13 +486,11 @@ class WebEngineTab(browsertab.AbstractTab): 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)) + elif isinstance(world, int): world_id = world + else: + world_id = _JS_WORLD_MAP[world] + try: if callback is None: self._widget.page().runJavaScript(code, world_id) diff --git a/qutebrowser/utils/usertypes.py b/qutebrowser/utils/usertypes.py index 26f9202ff..ef7fe529f 100644 --- a/qutebrowser/utils/usertypes.py +++ b/qutebrowser/utils/usertypes.py @@ -261,6 +261,10 @@ arg2backend = { } +# JS world for QtWebEngine +JsWorld = enum('JsWorld', ['main', 'application', 'user']) + + # Where a download should be saved class DownloadTarget: diff --git a/tests/end2end/features/misc.feature b/tests/end2end/features/misc.feature index d36982770..b21dbb6b0 100644 --- a/tests/end2end/features/misc.feature +++ b/tests/end2end/features/misc.feature @@ -109,6 +109,13 @@ Feature: Various utility commands. And I run :jseval --world 0 do_log() Then the javascript message "Hello from the page!" should be logged + @qtwebkit_skip + Scenario: :jseval using the main world as name + When I set general -> log-javascript-console to info + And I open data/misc/jseval.html + And I run :jseval --world main do_log() + Then the javascript message "Hello from the page!" should be logged + # :debug-webaction Scenario: :debug-webaction with valid value