From d54da64fa52a1d42fa5f2b202f81b64e83bcc846 Mon Sep 17 00:00:00 2001 From: Taka Date: Thu, 16 Aug 2018 10:10:49 +0000 Subject: [PATCH] Javascript console messages are logged by the PyQt wrapper app. --- vasl_templates/main_window.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/vasl_templates/main_window.py b/vasl_templates/main_window.py index 93e838d..fa56077 100644 --- a/vasl_templates/main_window.py +++ b/vasl_templates/main_window.py @@ -1,6 +1,7 @@ """ Main application window. """ import os +import re from PyQt5.QtWidgets import QWidget, QVBoxLayout, QLabel, QFileDialog, QMessageBox from PyQt5.QtWebEngineWidgets import QWebEngineView, QWebEngineProfile, QWebEnginePage @@ -11,6 +12,8 @@ from PyQt5.QtCore import QObject, QUrl, pyqtSlot from vasl_templates.webapp.config.constants import APP_NAME from vasl_templates.webapp import app as webapp +_CONSOLE_SOURCE_REGEX = re.compile( r"^http://.+?/static/(.*)$" ) + # --------------------------------------------------------------------- class WebChannelHandler( QObject ): @@ -30,6 +33,17 @@ class WebChannelHandler( QObject ): # --------------------------------------------------------------------- +class AppWebPage( QWebEnginePage ): + """Main webapp page.""" + + def javaScriptConsoleMessage( self, level, msg, line_no, source_id ): #pylint: disable=unused-argument,no-self-use + """Log a Javascript console message.""" + mo = _CONSOLE_SOURCE_REGEX.search( source_id ) + source = mo.group(1) if mo else source_id + print( "{}:{} - {}".format( source, line_no, msg ) ) + +# --------------------------------------------------------------------- + class MainWindow( QWidget ): """Main application window.""" @@ -65,7 +79,7 @@ class MainWindow( QWidget ): # nb: we create an off-the-record profile to stop the view from using cached JS files :-/ profile = QWebEngineProfile( None, self.view ) profile.downloadRequested.connect( self.onDownloadRequested ) - page = QWebEnginePage( profile, self.view ) + page = AppWebPage( profile, self.view ) self.view.setPage( page ) # create a web channel to communicate with the front-end