Accept a name for --world with :jseval
This commit is contained in:
parent
97edc59f03
commit
c9e3cc04cf
@ -447,7 +447,7 @@ Evaluate a JavaScript string.
|
|||||||
|
|
||||||
==== optional arguments
|
==== optional arguments
|
||||||
* +*-q*+, +*--quiet*+: Don't show resulting JS object.
|
* +*-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
|
==== note
|
||||||
|
@ -1918,14 +1918,15 @@ class CommandDispatcher:
|
|||||||
|
|
||||||
@cmdutils.register(instance='command-dispatcher', scope='window',
|
@cmdutils.register(instance='command-dispatcher', scope='window',
|
||||||
maxsplit=0, no_cmd_split=True)
|
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.
|
"""Evaluate a JavaScript string.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
js_code: The string to evaluate.
|
js_code: The string to evaluate.
|
||||||
quiet: Don't show resulting JS object.
|
quiet: Don't show resulting JS object.
|
||||||
world: Ignored on QtWebKit. On QtWebEngine, a world ID to run the
|
world: Ignored on QtWebKit. On QtWebEngine, a world ID or name to
|
||||||
snippet in.
|
run the snippet in.
|
||||||
"""
|
"""
|
||||||
if quiet:
|
if quiet:
|
||||||
jseval_cb = None
|
jseval_cb = None
|
||||||
|
@ -48,6 +48,14 @@ def init():
|
|||||||
req_interceptor.install(QWebEngineProfile.defaultProfile())
|
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):
|
class WebEnginePrinting(browsertab.AbstractPrinting):
|
||||||
|
|
||||||
"""QtWebEngine implementations related to printing."""
|
"""QtWebEngine implementations related to printing."""
|
||||||
@ -478,13 +486,11 @@ class WebEngineTab(browsertab.AbstractTab):
|
|||||||
def run_js_async(self, code, callback=None, *, world=None):
|
def run_js_async(self, code, callback=None, *, world=None):
|
||||||
if world is None:
|
if world is None:
|
||||||
world_id = QWebEngineScript.ApplicationWorld
|
world_id = QWebEngineScript.ApplicationWorld
|
||||||
else:
|
elif isinstance(world, int):
|
||||||
# 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
|
world_id = world
|
||||||
|
else:
|
||||||
|
world_id = _JS_WORLD_MAP[world]
|
||||||
|
|
||||||
try:
|
try:
|
||||||
if callback is None:
|
if callback is None:
|
||||||
self._widget.page().runJavaScript(code, world_id)
|
self._widget.page().runJavaScript(code, world_id)
|
||||||
|
@ -261,6 +261,10 @@ arg2backend = {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# JS world for QtWebEngine
|
||||||
|
JsWorld = enum('JsWorld', ['main', 'application', 'user'])
|
||||||
|
|
||||||
|
|
||||||
# Where a download should be saved
|
# Where a download should be saved
|
||||||
class DownloadTarget:
|
class DownloadTarget:
|
||||||
|
|
||||||
|
@ -109,6 +109,13 @@ Feature: Various utility commands.
|
|||||||
And I run :jseval --world 0 do_log()
|
And I run :jseval --world 0 do_log()
|
||||||
Then the javascript message "Hello from the page!" should be logged
|
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
|
# :debug-webaction
|
||||||
|
|
||||||
Scenario: :debug-webaction with valid value
|
Scenario: :debug-webaction with valid value
|
||||||
|
Loading…
Reference in New Issue
Block a user