Add --world to :jseval

This commit is contained in:
Florian Bruhin 2016-09-12 15:59:17 +02:00
parent a16c5a6a25
commit b2608d7697
3 changed files with 33 additions and 3 deletions

View File

@ -438,7 +438,7 @@ Note: Due a bug in Qt, the inspector will show incorrect request headers in the
[[jseval]]
=== jseval
Syntax: +:jseval [*--quiet*] 'js-code'+
Syntax: +:jseval [*--quiet*] [*--world* 'world'] 'js-code'+
Evaluate a JavaScript string.
@ -447,6 +447,8 @@ 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.
==== note
* This command does not split arguments after the last argument and handles quotes literally.

View File

@ -1918,12 +1918,14 @@ class CommandDispatcher:
@cmdutils.register(instance='command-dispatcher', scope='window',
maxsplit=0, no_cmd_split=True)
def jseval(self, js_code, quiet=False):
def jseval(self, js_code, quiet=False, *, world: 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.
"""
if quiet:
jseval_cb = None
@ -1945,7 +1947,11 @@ class CommandDispatcher:
out = out[:5000] + ' [...trimmed...]'
message.info(self._win_id, out)
self._current_widget().run_js_async(js_code, callback=jseval_cb)
widget = self._current_widget()
if world is None:
widget.run_js_async(js_code, callback=jseval_cb)
else:
widget.run_js_async(js_code, callback=jseval_cb, world=world)
@cmdutils.register(instance='command-dispatcher', scope='window')
def fake_key(self, keystring, global_=False):

View File

@ -87,6 +87,28 @@ Feature: Various utility commands.
When I run :jseval Array(5002).join("x")
Then the message "x* [...trimmed...]" should be shown
@qtwebengine_skip
Scenario: :jseval with --world on QtWebKit
When I set general -> log-javascript-console to info
And I run :jseval --world=1 console.log("Hello from JS!");
And I wait for the javascript message "Hello from JS!"
Then "Ignoring world ID 1" should be logged
@qtwebkit_skip
Scenario: :jseval uses separate world without --world
When I set general -> log-javascript-console to info
And I open data/misc/jseval.html
And I run :jseval do_log()
Then the javascript message "Hello from the page!" should not be logged
And the javascript message "Uncaught ReferenceError: do_log is not defined" should be logged
@qtwebkit_skip
Scenario: :jseval using the main world
When I set general -> log-javascript-console to info
And I open data/misc/jseval.html
And I run :jseval --world 0 do_log()
Then the javascript message "Hello from the page!" should be logged
# :debug-webaction
Scenario: :debug-webaction with valid value