From 4538745ffbe40a8e111753a77bf0bacc40fd1551 Mon Sep 17 00:00:00 2001 From: Florian Bruhin Date: Mon, 26 May 2014 20:04:44 +0200 Subject: [PATCH] Implement QWebPage:javaScript* --- qutebrowser/browser/webpage.py | 24 ++++++++++++++++++++++++ qutebrowser/utils/log.py | 1 + qutebrowser/utils/message.py | 8 ++++++++ qutebrowser/utils/usertypes.py | 2 +- qutebrowser/widgets/statusbar/_prompt.py | 9 +++++++++ 5 files changed, 43 insertions(+), 1 deletion(-) diff --git a/qutebrowser/browser/webpage.py b/qutebrowser/browser/webpage.py index a2fe43bb4..91e468b7d 100644 --- a/qutebrowser/browser/webpage.py +++ b/qutebrowser/browser/webpage.py @@ -22,9 +22,12 @@ from PyQt5.QtCore import QCoreApplication from PyQt5.QtNetwork import QNetworkReply from PyQt5.QtWebKitWidgets import QWebPage +import qutebrowser.utils.message as message import qutebrowser.utils.url as urlutils import qutebrowser.config.config as config +import qutebrowser.utils.log as log from qutebrowser.utils.misc import read_file +from qutebrowser.utils.usertypes import PromptMode class BrowserPage(QWebPage): @@ -105,3 +108,24 @@ class BrowserPage(QWebPage): except KeyError: return super().extension(ext, opt, out) return handler(opt, out) + + def javaScriptAlert(self, _frame, msg): + """Override javaScriptAlert to use the statusbar.""" + message.modular_question("js: {}".format(msg), PromptMode.alert) + + def javaScriptConfirm(self, _frame, msg): + """Override javaScriptConfirm to use the statusbar.""" + return message.modular_question("js: {}".format(msg), PromptMode.yesno) + + def javaScriptConsoleMessage(self, msg, line, source): + """Override javaScriptConsoleMessage to use debug log.""" + log.js.info("[{}:{}] {}".format(source, line, msg)) + + def javaScriptPrompt(self, _frame, msg, default): + """Override javaScriptConfirm to use the statusbar.""" + answer = message.modular_question( + "js: {}".format(msg), PromptMode.text, default) + if answer is None: + return (False, "") + else: + return (True, answer) diff --git a/qutebrowser/utils/log.py b/qutebrowser/utils/log.py index d7f9909a7..bd12cf2bb 100644 --- a/qutebrowser/utils/log.py +++ b/qutebrowser/utils/log.py @@ -45,6 +45,7 @@ init = getLogger('init') signals = getLogger('signals') hints = getLogger('hints') keyboard = getLogger('keyboard') +js = getLogger('js') ram_handler = None diff --git a/qutebrowser/utils/message.py b/qutebrowser/utils/message.py index 0bce5fa3f..f3d3ee786 100644 --- a/qutebrowser/utils/message.py +++ b/qutebrowser/utils/message.py @@ -80,6 +80,14 @@ def modular_question(message, mode, default=None): return q.answer +def alert(message): + """Display an alert which needs to be confirmed.""" + q = Question() + q.text = message + q.mode = PromptMode.alert + instance().question.emit(q, True) + + def question(message, mode, handler, default=None): """Ask an async question in the statusbar. diff --git a/qutebrowser/utils/usertypes.py b/qutebrowser/utils/usertypes.py index 1f2deff61..0ce154b54 100644 --- a/qutebrowser/utils/usertypes.py +++ b/qutebrowser/utils/usertypes.py @@ -248,7 +248,7 @@ class FakeDict: # The mode of a Question. -PromptMode = enum('yesno', 'text', 'user_pwd') +PromptMode = enum('yesno', 'text', 'user_pwd', 'alert') class Question(QObject): diff --git a/qutebrowser/widgets/statusbar/_prompt.py b/qutebrowser/widgets/statusbar/_prompt.py index 8c6d30a13..13ab7b94b 100644 --- a/qutebrowser/widgets/statusbar/_prompt.py +++ b/qutebrowser/widgets/statusbar/_prompt.py @@ -111,6 +111,11 @@ class Prompt(QWidget): self.question.answer = self.question.default modeman.leave('yesno', 'yesno accept') self.question.answered.emit() + elif self.question.mode == PromptMode.alert: + # User acknowledged an alert + self.question.answer = None + modeman.leave('prompt', 'alert accept') + self.question.answered.emit() else: raise ValueError("Invalid question mode!") @@ -167,6 +172,10 @@ class Prompt(QWidget): self._input.setText(q.default) self._input.show() mode = 'prompt' + elif q.mode == PromptMode.alert: + self._txt.setText(q.text + ' (ok)') + self._input.hide() + mode = 'prompt' else: raise ValueError("Invalid prompt mode!") self._input.setFocus()