Adds a --file flag to :jseval
This commit is contained in:
parent
fa3bb9a5c8
commit
6cb48ba2b6
@ -1927,12 +1927,13 @@ 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, file=False, quiet=False, *,
|
||||
world: typing.Union[usertypes.JsWorld, int]=None):
|
||||
"""Evaluate a JavaScript string.
|
||||
|
||||
Args:
|
||||
js_code: The string to evaluate.
|
||||
js_code: The string/file to evaluate.
|
||||
file: Interpret js-code as a path to a file.
|
||||
quiet: Don't show resulting JS object.
|
||||
world: Ignored on QtWebKit. On QtWebEngine, a world ID or name to
|
||||
run the snippet in.
|
||||
@ -1960,6 +1961,13 @@ class CommandDispatcher:
|
||||
out = out[:5000] + ' [...trimmed...]'
|
||||
message.info(out)
|
||||
|
||||
if file:
|
||||
try:
|
||||
with open(js_code, 'r', encoding='utf-8') as f:
|
||||
js_code = f.read()
|
||||
except OSError as e:
|
||||
raise cmdexc.CommandError(str(e))
|
||||
|
||||
widget = self._current_widget()
|
||||
widget.run_js_async(js_code, callback=jseval_cb, world=world)
|
||||
|
||||
|
2
tests/end2end/data/misc/jseval_file.js
Normal file
2
tests/end2end/data/misc/jseval_file.js
Normal file
@ -0,0 +1,2 @@
|
||||
console.log("Hello from JS!")
|
||||
console.log("Hello again from JS!")
|
@ -102,6 +102,17 @@ Feature: Various utility commands.
|
||||
And I run :jseval --world main do_log()
|
||||
Then the javascript message "Hello from the page!" should be logged
|
||||
|
||||
Scenario: :jseval --file using a file that exists as js-code
|
||||
When I set general -> log-javascript-console to info
|
||||
And I run :jseval --file (testdata)/misc/jseval_file.js
|
||||
Then the javascript message "Hello from JS!" should be logged
|
||||
And the javascript message "Hello again from JS!" should be logged
|
||||
|
||||
Scenario: :jseval --file using a file that doesn't exist as js-code
|
||||
When I run :jseval --file nonexistentfile
|
||||
Then the error "[Errno 2] No such file or directory: 'nonexistentfile'" should be shown
|
||||
And "No output or error" should not be logged
|
||||
|
||||
# :debug-webaction
|
||||
|
||||
Scenario: :debug-webaction with valid value
|
||||
|
Loading…
Reference in New Issue
Block a user