Implement QWebPage:javaScript*

This commit is contained in:
Florian Bruhin 2014-05-26 20:04:44 +02:00
parent ff1fad073c
commit 4538745ffb
5 changed files with 43 additions and 1 deletions

View File

@ -22,9 +22,12 @@ from PyQt5.QtCore import QCoreApplication
from PyQt5.QtNetwork import QNetworkReply from PyQt5.QtNetwork import QNetworkReply
from PyQt5.QtWebKitWidgets import QWebPage from PyQt5.QtWebKitWidgets import QWebPage
import qutebrowser.utils.message as message
import qutebrowser.utils.url as urlutils import qutebrowser.utils.url as urlutils
import qutebrowser.config.config as config import qutebrowser.config.config as config
import qutebrowser.utils.log as log
from qutebrowser.utils.misc import read_file from qutebrowser.utils.misc import read_file
from qutebrowser.utils.usertypes import PromptMode
class BrowserPage(QWebPage): class BrowserPage(QWebPage):
@ -105,3 +108,24 @@ class BrowserPage(QWebPage):
except KeyError: except KeyError:
return super().extension(ext, opt, out) return super().extension(ext, opt, out)
return handler(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)

View File

@ -45,6 +45,7 @@ init = getLogger('init')
signals = getLogger('signals') signals = getLogger('signals')
hints = getLogger('hints') hints = getLogger('hints')
keyboard = getLogger('keyboard') keyboard = getLogger('keyboard')
js = getLogger('js')
ram_handler = None ram_handler = None

View File

@ -80,6 +80,14 @@ def modular_question(message, mode, default=None):
return q.answer 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): def question(message, mode, handler, default=None):
"""Ask an async question in the statusbar. """Ask an async question in the statusbar.

View File

@ -248,7 +248,7 @@ class FakeDict:
# The mode of a Question. # The mode of a Question.
PromptMode = enum('yesno', 'text', 'user_pwd') PromptMode = enum('yesno', 'text', 'user_pwd', 'alert')
class Question(QObject): class Question(QObject):

View File

@ -111,6 +111,11 @@ class Prompt(QWidget):
self.question.answer = self.question.default self.question.answer = self.question.default
modeman.leave('yesno', 'yesno accept') modeman.leave('yesno', 'yesno accept')
self.question.answered.emit() 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: else:
raise ValueError("Invalid question mode!") raise ValueError("Invalid question mode!")
@ -167,6 +172,10 @@ class Prompt(QWidget):
self._input.setText(q.default) self._input.setText(q.default)
self._input.show() self._input.show()
mode = 'prompt' mode = 'prompt'
elif q.mode == PromptMode.alert:
self._txt.setText(q.text + ' (ok)')
self._input.hide()
mode = 'prompt'
else: else:
raise ValueError("Invalid prompt mode!") raise ValueError("Invalid prompt mode!")
self._input.setFocus() self._input.setFocus()