From f3c561b6d9e60eaa34b80f226a8c5d396120d175 Mon Sep 17 00:00:00 2001 From: evandrocoan Date: Wed, 15 Apr 2020 04:00:32 -0300 Subject: [PATCH 1/3] Fixed javaScriptConsoleMessage showing 'error on line' for all lines. The new output format is: JS info /_anki/reviewer.js:166 autoadd JS error /_anki/reviewer.js:240 Uncaught (in promise) NotAllowedError: play() can only be initiated by a user gesture. --- qt/aqt/webview.py | 24 +++++++++++++++++------- 1 file changed, 17 insertions(+), 7 deletions(-) diff --git a/qt/aqt/webview.py b/qt/aqt/webview.py index 675b5ec52..abb70ddc3 100644 --- a/qt/aqt/webview.py +++ b/qt/aqt/webview.py @@ -67,15 +67,24 @@ class AnkiWebPage(QWebEnginePage): # type: ignore script.setRunsOnSubFrames(False) self.profile().scripts().insert(script) - def javaScriptConsoleMessage(self, lvl, msg, line, srcID): + def javaScriptConsoleMessage(self, level, msg, line, srcID): # not translated because console usually not visible, # and may only accept ascii text - buf = "JS error on line %(a)d: %(b)s" % dict(a=line, b=msg + "\n") - # ensure we don't try to write characters the terminal can't handle - buf = buf.encode(sys.stdout.encoding, "backslashreplace").decode( - sys.stdout.encoding + srcID = srcID.replace(AnkiWebView.webBundlePath("/"), "") + if level == QWebEnginePage.InfoMessageLevel: + level = "info" + elif level == QWebEnginePage.WarningMessageLevel: + level = "warning" + elif level == QWebEnginePage.ErrorMessageLevel: + level = "error" + buf = "JS %(t)s /%(f)s:%(a)d %(b)s" % dict( + t=level, a=line, f=srcID, b=msg + "\n" ) - sys.stdout.write(buf) + # ensure we don't try to write characters the terminal can't handle + buf = buf.encode(sys.stderr.encoding, "backslashreplace").decode( + sys.stderr.encoding + ) + sys.stderr.write(buf) def acceptNavigationRequest(self, url, navType, isMainFrame): if not isMainFrame: @@ -412,7 +421,8 @@ body {{ zoom: {}; background: {}; {} }} # print(html) self.setHtml(html) - def webBundlePath(self, path: str) -> str: + @classmethod + def webBundlePath(cls, path: str) -> str: from aqt import mw if path.startswith("/"): From 7061aac16d9d9da1eab52489553fa0aae1a66535 Mon Sep 17 00:00:00 2001 From: evandrocoan Date: Wed, 15 Apr 2020 16:04:57 -0300 Subject: [PATCH 2/3] Set to javaScriptConsoleMessage output to stdout because it may raise error messages on the anki GUI. --- qt/aqt/webview.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/qt/aqt/webview.py b/qt/aqt/webview.py index abb70ddc3..bb6ebc637 100644 --- a/qt/aqt/webview.py +++ b/qt/aqt/webview.py @@ -81,10 +81,12 @@ class AnkiWebPage(QWebEnginePage): # type: ignore t=level, a=line, f=srcID, b=msg + "\n" ) # ensure we don't try to write characters the terminal can't handle - buf = buf.encode(sys.stderr.encoding, "backslashreplace").decode( - sys.stderr.encoding + buf = buf.encode(sys.stdout.encoding, "backslashreplace").decode( + sys.stdout.encoding ) - sys.stderr.write(buf) + # output to stdout because it may raise error messages on the anki GUI + # https://github.com/ankitects/anki/pull/560 + sys.stdout.write(buf) def acceptNavigationRequest(self, url, navType, isMainFrame): if not isMainFrame: From 2f174b40a6995af7b9096aeb2061a4d1d6ed93a7 Mon Sep 17 00:00:00 2001 From: evandrocoan Date: Wed, 15 Apr 2020 16:21:47 -0300 Subject: [PATCH 3/3] Set to use a regular expression to remove the server name because it is less prone to break in future updates. --- qt/aqt/webview.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/qt/aqt/webview.py b/qt/aqt/webview.py index bb6ebc637..b5958bb36 100644 --- a/qt/aqt/webview.py +++ b/qt/aqt/webview.py @@ -4,6 +4,7 @@ import dataclasses import json import math +import re import sys from typing import Any, Callable, List, Optional, Sequence, Tuple @@ -70,14 +71,14 @@ class AnkiWebPage(QWebEnginePage): # type: ignore def javaScriptConsoleMessage(self, level, msg, line, srcID): # not translated because console usually not visible, # and may only accept ascii text - srcID = srcID.replace(AnkiWebView.webBundlePath("/"), "") + srcID = re.sub(r"(?mi).+://[^/]+", "", srcID) if level == QWebEnginePage.InfoMessageLevel: level = "info" elif level == QWebEnginePage.WarningMessageLevel: level = "warning" elif level == QWebEnginePage.ErrorMessageLevel: level = "error" - buf = "JS %(t)s /%(f)s:%(a)d %(b)s" % dict( + buf = "JS %(t)s %(f)s:%(a)d %(b)s" % dict( t=level, a=line, f=srcID, b=msg + "\n" ) # ensure we don't try to write characters the terminal can't handle