From 12f4940ef36c5f032ed2b75f4a40886b36438bd2 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Wed, 22 Nov 2017 09:35:16 +0100 Subject: [PATCH] Make :jseval use a fixed path with relative paths --- doc/changelog.asciidoc | 2 ++ doc/help/commands.asciidoc | 5 ++++- qutebrowser/browser/commands.py | 8 +++++++- tests/end2end/features/misc.feature | 4 ++-- 4 files changed, 15 insertions(+), 4 deletions(-) diff --git a/doc/changelog.asciidoc b/doc/changelog.asciidoc index 4931c1199..78edb1ddf 100644 --- a/doc/changelog.asciidoc +++ b/doc/changelog.asciidoc @@ -85,6 +85,8 @@ Changed again to navigate through completion items when a text was entered. - `:debug-pyeval` now has a `--file` arguemnt so it takes a filename instead of a line of code. +- `:jseval --file` now searches relative paths in a js/ subdir in qutebrowser's + data dir, e.g. `~/.local/share/qutebrowser/js`. Fixed ~~~~~ diff --git a/doc/help/commands.asciidoc b/doc/help/commands.asciidoc index 11a8970e2..95caf24f1 100644 --- a/doc/help/commands.asciidoc +++ b/doc/help/commands.asciidoc @@ -639,7 +639,10 @@ Evaluate a JavaScript string. * +'js-code'+: The string/file to evaluate. ==== optional arguments -* +*-f*+, +*--file*+: Interpret js-code as a path to a file. +* +*-f*+, +*--file*+: Interpret js-code as a path to a file. If the path is relative, the file is searched in a js/ subdir + in qutebrowser's data dir, e.g. + `~/.local/share/qutebrowser/js`. + * +*-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 e007de76d..8c80cd590 100644 --- a/qutebrowser/browser/commands.py +++ b/qutebrowser/browser/commands.py @@ -39,7 +39,7 @@ from qutebrowser.browser import (urlmarks, browsertab, inspector, navigate, webelem, downloads) from qutebrowser.keyinput import modeman from qutebrowser.utils import (message, usertypes, log, qtutils, urlutils, - objreg, utils, debug) + objreg, utils, debug, standarddir) from qutebrowser.utils.usertypes import KeyMode from qutebrowser.misc import editor, guiprocess from qutebrowser.completion.models import urlmodel, miscmodels @@ -2029,6 +2029,9 @@ class CommandDispatcher: Args: js_code: The string/file to evaluate. file: Interpret js-code as a path to a file. + If the path is relative, the file is searched in a js/ subdir + in qutebrowser's data dir, e.g. + `~/.local/share/qutebrowser/js`. quiet: Don't show resulting JS object. world: Ignored on QtWebKit. On QtWebEngine, a world ID or name to run the snippet in. @@ -2058,6 +2061,9 @@ class CommandDispatcher: if file: path = os.path.expanduser(js_code) + if not os.path.isabs(path): + path = os.path.join(standarddir.data(), 'js') + try: with open(path, 'r', encoding='utf-8') as f: js_code = f.read() diff --git a/tests/end2end/features/misc.feature b/tests/end2end/features/misc.feature index 5b1f8371f..70505c1f8 100644 --- a/tests/end2end/features/misc.feature +++ b/tests/end2end/features/misc.feature @@ -118,8 +118,8 @@ Feature: Various utility commands. And "No output or error" 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 + 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