From b862af38444ead7ffd984444275a6332a95f7acc Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 23 Sep 2018 09:01:32 +0530 Subject: [PATCH] More work on the new viewer --- src/calibre/gui2/viewer2/web_view.py | 1 + src/pyj/viewer-main.pyj | 43 ++++++++++++++++++++++++++-- 2 files changed, 41 insertions(+), 3 deletions(-) diff --git a/src/calibre/gui2/viewer2/web_view.py b/src/calibre/gui2/viewer2/web_view.py index 6620886f2a..9c114ed10a 100644 --- a/src/calibre/gui2/viewer2/web_view.py +++ b/src/calibre/gui2/viewer2/web_view.py @@ -47,6 +47,7 @@ def get_data(name): return f.read(), guess_type(name) except EnvironmentError as err: prints('Failed to read from book file: {} with error: {}'.format(name, as_unicode(err))) + return None, None class UrlSchemeHandler(QWebEngineUrlSchemeHandler): diff --git a/src/pyj/viewer-main.pyj b/src/pyj/viewer-main.pyj index 6118daf83c..0a28c0fce3 100644 --- a/src/pyj/viewer-main.pyj +++ b/src/pyj/viewer-main.pyj @@ -2,15 +2,21 @@ # License: GPL v3 Copyright: 2018, Kovid Goyal from __python__ import bound_methods, hash_literals - +import traceback from elementmaker import E -from read_book.globals import runtime, ui_operations +from gettext import gettext as _ + +from ajax import ajax +from modals import error_dialog from qt import from_python +from read_book.globals import runtime, ui_operations + def container_div(id): return E.div(id=id, style='margin: 0; padding: 0; display: none') runtime.is_standalone_viewer = True +book_manifest = None def get_file(book, name, proceed): @@ -29,9 +35,39 @@ def show_error(title, msg, details): pass # TODO: Implement this +def manifest_received(end_type, xhr, ev): + nonlocal book_manifest + if end_type is 'load': + book_manifest = xhr.response + else: + error_dialog(_('Could not open book'), _( + 'Failed to load book manifest, click "Show details" for more info'), + xhr.error_html or None) + + @from_python def start_book_load(): - print(111111111111111) + ajax('book/calibre-book-manifest.json', manifest_received, ok_code=0).send() + + +def onerror(msg, script_url, line_number, column_number, error_object): + if error_object is None: + # This happens for cross-domain errors (probably javascript injected + # into the browser via extensions/ userscripts and the like). It also + # happens all the time when using Chrom on Safari, so ignore this + # type of error + console.log(f'Unhandled error from external javascript, ignoring: {msg} {script_url} {line_number}') + return + console.log(error_object) + try: + fname = script_url.rpartition('/')[-1] or script_url + msg = msg + '
' + 'Error at {}:{}:{}'.format(fname, line_number, column_number or '') + '' + details = '' + details = traceback.format_exception(error_object).join('') + error_dialog(_('Unhandled error'), msg, details) + return True + except: + console.log('There was an error in the unhandled exception handler') if window is window.top: @@ -42,6 +78,7 @@ if window is window.top: ui_operations.show_error = show_error document.body.appendChild(E.div(id='loading')) document.body.appendChild(E.div(id='view')) + window.onerror = onerror else: # iframe pass