summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorian Bruhin <git@the-compiler.org>2017-02-25 17:26:59 +0100
committerFlorian Bruhin <git@the-compiler.org>2017-02-25 17:26:59 +0100
commitb5d4de17a7dbe4a5717876028d6db46c54eb03bd (patch)
tree40bc45ce7b74a7d85186ee2c7914ddee28800b72
parent3cc32e0b6a983dd8e0bd0fc28e1cea5e337cf08c (diff)
parent40152d1827065b62fb62d8763c9834dbdfa2994b (diff)
downloadqutebrowser-b5d4de17a7dbe4a5717876028d6db46c54eb03bd.tar.gz
qutebrowser-b5d4de17a7dbe4a5717876028d6db46c54eb03bd.zip
Merge branch 'pkill-nine-upstream-master-jseval-file'
-rw-r--r--README.asciidoc2
-rw-r--r--doc/help/commands.asciidoc5
-rw-r--r--qutebrowser/browser/commands.py12
-rw-r--r--tests/end2end/data/misc/jseval_file.js2
-rw-r--r--tests/end2end/features/misc.feature11
5 files changed, 27 insertions, 5 deletions
diff --git a/README.asciidoc b/README.asciidoc
index be3211dc4..8aae70fb4 100644
--- a/README.asciidoc
+++ b/README.asciidoc
@@ -231,6 +231,7 @@ Contributors, sorted by the number of commits in descending order:
* Ismail
* Edgar Hipp
* Daryl Finlay
+* pkill9
* arza
* adam
* Samir Benmendil
@@ -244,7 +245,6 @@ Contributors, sorted by the number of commits in descending order:
* zwarag
* xd1le
* rmortens
-* pkill9
* oniondreams
* issue
* haxwithaxe
diff --git a/doc/help/commands.asciidoc b/doc/help/commands.asciidoc
index 1885d227a..77d7026c8 100644
--- a/doc/help/commands.asciidoc
+++ b/doc/help/commands.asciidoc
@@ -465,14 +465,15 @@ Note: Due a bug in Qt, the inspector will show incorrect request headers in the
[[jseval]]
=== jseval
-Syntax: +:jseval [*--quiet*] [*--world* 'world'] 'js-code'+
+Syntax: +:jseval [*--file*] [*--quiet*] [*--world* 'world'] 'js-code'+
Evaluate a JavaScript string.
==== positional arguments
-* +'js-code'+: The string to evaluate.
+* +'js-code'+: The string/file to evaluate.
==== optional arguments
+* +*-f*+, +*--file*+: Interpret js-code as a path to a file.
* +*-q*+, +*--quiet*+: Don't show resulting JS object.
* +*-w*+, +*--world*+: Ignored on QtWebKit. On QtWebEngine, a world ID or name to run the snippet in.
diff --git a/qutebrowser/browser/commands.py b/qutebrowser/browser/commands.py
index 5013d6730..34bf0fcf5 100644
--- a/qutebrowser/browser/commands.py
+++ b/qutebrowser/browser/commands.py
@@ -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)
diff --git a/tests/end2end/data/misc/jseval_file.js b/tests/end2end/data/misc/jseval_file.js
new file mode 100644
index 000000000..5f6464ef3
--- /dev/null
+++ b/tests/end2end/data/misc/jseval_file.js
@@ -0,0 +1,2 @@
+console.log("Hello from JS!")
+console.log("Hello again from JS!")
diff --git a/tests/end2end/features/misc.feature b/tests/end2end/features/misc.feature
index 39919ae98..5e13e1feb 100644
--- a/tests/end2end/features/misc.feature
+++ b/tests/end2end/features/misc.feature
@@ -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