Merge pull request #560 from evandroforks/fix_javascript_console_output

Fixed javaScriptConsoleMessage showing 'error on line'
This commit is contained in:
Damien Elmes 2020-04-16 10:15:56 +10:00 committed by GitHub
commit 394f7c630c
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -4,6 +4,7 @@
import dataclasses import dataclasses
import json import json
import math import math
import re
import sys import sys
from typing import Any, Callable, List, Optional, Sequence, Tuple from typing import Any, Callable, List, Optional, Sequence, Tuple
@ -67,14 +68,25 @@ class AnkiWebPage(QWebEnginePage): # type: ignore
script.setRunsOnSubFrames(False) script.setRunsOnSubFrames(False)
self.profile().scripts().insert(script) 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, # not translated because console usually not visible,
# and may only accept ascii text # and may only accept ascii text
buf = "JS error on line %(a)d: %(b)s" % dict(a=line, b=msg + "\n") 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(
t=level, a=line, f=srcID, b=msg + "\n"
)
# ensure we don't try to write characters the terminal can't handle # ensure we don't try to write characters the terminal can't handle
buf = buf.encode(sys.stdout.encoding, "backslashreplace").decode( buf = buf.encode(sys.stdout.encoding, "backslashreplace").decode(
sys.stdout.encoding sys.stdout.encoding
) )
# output to stdout because it may raise error messages on the anki GUI
# https://github.com/ankitects/anki/pull/560
sys.stdout.write(buf) sys.stdout.write(buf)
def acceptNavigationRequest(self, url, navType, isMainFrame): def acceptNavigationRequest(self, url, navType, isMainFrame):
@ -412,7 +424,8 @@ body {{ zoom: {}; background: {}; {} }}
# print(html) # print(html)
self.setHtml(html) self.setHtml(html)
def webBundlePath(self, path: str) -> str: @classmethod
def webBundlePath(cls, path: str) -> str:
from aqt import mw from aqt import mw
if path.startswith("/"): if path.startswith("/"):