diff --git a/CHANGELOG.asciidoc b/CHANGELOG.asciidoc index 887e4f604..fe09de709 100644 --- a/CHANGELOG.asciidoc +++ b/CHANGELOG.asciidoc @@ -111,6 +111,8 @@ Changed when using `--backend webengine`. - `content -> javascript-can-open-windows` got renamed to `javascript-can-open-windows-automatically`. +- `:prompt-accept` now optionally accepts a value which overrides the one + entered in the input box. Deprecated ~~~~~~~~~~ diff --git a/doc/help/commands.asciidoc b/doc/help/commands.asciidoc index d13f20ead..b9fff178a 100644 --- a/doc/help/commands.asciidoc +++ b/doc/help/commands.asciidoc @@ -1219,8 +1219,14 @@ The editor which should be launched can be configured via the `general -> editor [[prompt-accept]] === prompt-accept +Syntax: +:prompt-accept ['value']+ + Accept the current prompt. +==== positional arguments +* +'value'+: If given, uses this value instead of the entered one. For boolean prompts, "yes"/"no" are accepted as value. + + [[prompt-no]] === prompt-no Answer no to a yes/no prompt. diff --git a/qutebrowser/mainwindow/statusbar/prompter.py b/qutebrowser/mainwindow/statusbar/prompter.py index 337b71e1d..66f3d58d0 100644 --- a/qutebrowser/mainwindow/statusbar/prompter.py +++ b/qutebrowser/mainwindow/statusbar/prompter.py @@ -26,7 +26,7 @@ from PyQt5.QtCore import pyqtSlot, pyqtSignal, QTimer, QObject from PyQt5.QtWidgets import QLineEdit from qutebrowser.keyinput import modeman -from qutebrowser.commands import cmdutils +from qutebrowser.commands import cmdutils, cmdexc from qutebrowser.utils import usertypes, log, qtutils, objreg, utils @@ -236,49 +236,65 @@ class Prompter(QObject): @cmdutils.register(instance='prompter', hide=True, scope='window', modes=[usertypes.KeyMode.prompt, usertypes.KeyMode.yesno]) - def prompt_accept(self): + def prompt_accept(self, value=None): """Accept the current prompt. // This executes the next action depending on the question mode, e.g. asks for the password or leaves the mode. + + Args: + value: If given, uses this value instead of the entered one. + For boolean prompts, "yes"/"no" are accepted as value. """ prompt = objreg.get('prompt', scope='window', window=self._win_id) + text = value if value is not None else prompt.lineedit.text() + if (self._question.mode == usertypes.PromptMode.user_pwd and self._question.user is None): # User just entered a username - self._question.user = prompt.lineedit.text() + self._question.user = text prompt.txt.setText("Password:") prompt.lineedit.clear() prompt.lineedit.setEchoMode(QLineEdit.Password) elif self._question.mode == usertypes.PromptMode.user_pwd: # User just entered a password - password = prompt.lineedit.text() - self._question.answer = AuthTuple(self._question.user, password) + self._question.answer = AuthTuple(self._question.user, text) modeman.maybe_leave(self._win_id, usertypes.KeyMode.prompt, 'prompt accept') self._question.done() elif self._question.mode == usertypes.PromptMode.text: # User just entered text. - self._question.answer = prompt.lineedit.text() + self._question.answer = text modeman.maybe_leave(self._win_id, usertypes.KeyMode.prompt, 'prompt accept') self._question.done() elif self._question.mode == usertypes.PromptMode.download: # User just entered a path for a download. - target = usertypes.FileDownloadTarget(prompt.lineedit.text()) + target = usertypes.FileDownloadTarget(text) self._question.answer = target modeman.maybe_leave(self._win_id, usertypes.KeyMode.prompt, 'prompt accept') self._question.done() elif self._question.mode == usertypes.PromptMode.yesno: # User wants to accept the default of a yes/no question. - self._question.answer = self._question.default + if value is None: + self._question.answer = self._question.default + elif value == 'yes': + self._question.answer = True + elif value == 'no': + self._question.answer = False + else: + raise cmdexc.CommandError("Invalid value {} - expected " + "yes/no!".format(value)) modeman.maybe_leave(self._win_id, usertypes.KeyMode.yesno, 'yesno accept') self._question.done() elif self._question.mode == usertypes.PromptMode.alert: + if value is not None: + raise cmdexc.CommandError("No value is permitted with alert " + "prompts!") # User acknowledged an alert self._question.answer = None modeman.maybe_leave(self._win_id, usertypes.KeyMode.prompt, diff --git a/tests/end2end/features/prompts.feature b/tests/end2end/features/prompts.feature index 633f465b6..2f2d3fd98 100644 --- a/tests/end2end/features/prompts.feature +++ b/tests/end2end/features/prompts.feature @@ -218,3 +218,34 @@ Feature: Prompts "authenticated": true, "user": "user" } + + # :prompt-accept with value argument + + Scenario: Javascript alert with value + When I set content -> ignore-javascript-alert to false + And I open data/prompt/jsalert.html + And I run :click-element id button + And I wait for a prompt + And I run :prompt-accept foobar + And I run :prompt-accept + Then the javascript message "Alert done" should be logged + And the error "No value is permitted with alert prompts!" should be shown + + @pyqt>=5.3.1 + Scenario: Javascript prompt with value + When I set content -> ignore-javascript-prompt to false + And I open data/prompt/jsprompt.html + And I run :click-element id button + And I wait for a prompt + And I press the keys "prompt test" + And I run :prompt-accept "overridden value" + Then the javascript message "Prompt reply: overridden value" should be logged + + Scenario: Javascript confirm with invalid value + When I open data/prompt/jsconfirm.html + And I run :click-element id button + And I wait for a prompt + And I run :prompt-accept nope + And I run :prompt-accept yes + Then the javascript message "confirm reply: true" should be logged + And the error "Invalid value nope - expected yes/no!" should be shown