From b0cf1e8c05ec0cceb49ddcd64195cab84156ae17 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 12 Jun 2016 17:28:40 +0530 Subject: [PATCH] Prevent un-lloadable resources in link tags (non-CSS stylesheets) from causing the resource loading check to timeout --- src/calibre/srv/render_book.py | 9 +++++++++ src/pyj/read_book/resources.pyj | 4 +++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/src/calibre/srv/render_book.py b/src/calibre/srv/render_book.py index 913fa590e6..ac0b815daa 100644 --- a/src/calibre/srv/render_book.py +++ b/src/calibre/srv/render_book.py @@ -215,6 +215,7 @@ class Container(ContainerBase): resource_template = link_uid + '|{}|' xlink_xpath = XPath('//*[@xl:href]') link_xpath = XPath('//h:a[@href]') + res_link_xpath = XPath('//h:link[@href]') def link_replacer(base, url): if url.startswith('#'): @@ -249,6 +250,14 @@ class Container(ContainerBase): elif mt in OEB_DOCS: self.virtualized_names.add(name) root = self.parsed(name) + for link in res_link_xpath(root): + ltype = (link.get('type') or 'text/css').lower() + rel = (link.get('rel') or 'stylesheet').lower() + if ltype != 'text/css' or rel != 'stylesheet': + # This link will not be loaded by the browser anyway + # and will causes the resource load check to hang + link.attrib.clear() + changed.add(name) rewrite_links(root, partial(link_replacer, name)) for a in link_xpath(root): href = a.get('href') diff --git a/src/pyj/read_book/resources.pyj b/src/pyj/read_book/resources.pyj index d1266615a6..8743ed9af3 100644 --- a/src/pyj/read_book/resources.pyj +++ b/src/pyj/read_book/resources.pyj @@ -229,11 +229,13 @@ def unserialize_html(serialized_data, proceed): clear(document.head, document.body) load_required = set() proceeded = False + hang_timeout = 5 def hangcheck(): nonlocal proceeded if not proceeded: proceeded = True + print('WARNING: All resources did not load in {} seconds, proceeding anyway ({} resources left)'.format(hang_timeout, load_required.length)) proceed() def onload(): @@ -258,7 +260,7 @@ def unserialize_html(serialized_data, proceed): ev.initEvent('DOMContentLoaded', True, True) document.dispatchEvent(ev) if load_required.length: - setTimeout(hangcheck, 5000) + setTimeout(hangcheck, hang_timeout * 1000) else: proceeded = True proceed()