Accept a name for --world with :jseval

This commit is contained in:
Florian Bruhin 2016-09-12 18:23:23 +02:00
parent 97edc59f03
commit c9e3cc04cf
5 changed files with 28 additions and 10 deletions

View File

@ -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

View File

@ -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

View File

@ -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)

View File

@ -261,6 +261,10 @@ arg2backend = {
}
# JS world for QtWebEngine
JsWorld = enum('JsWorld', ['main', 'application', 'user'])
# Where a download should be saved
class DownloadTarget:

View File

@ -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