From 8988cbcf56fede9c5ccf9b4a7df9685cc41a67df Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 16 May 2021 05:43:02 +0530 Subject: [PATCH] Remove top-level references to window and document --- src/pyj/book_list/book_details.pyj | 3 ++- src/pyj/read_book/flow_mode.pyj | 19 ++++++++++++------- src/pyj/read_book/prefs/head_foot.pyj | 2 +- src/pyj/read_book/tts.pyj | 4 ++-- src/pyj/select.pyj | 2 +- src/pyj/utils.pyj | 2 +- 6 files changed, 19 insertions(+), 13 deletions(-) diff --git a/src/pyj/book_list/book_details.pyj b/src/pyj/book_list/book_details.pyj index 51c6cce818..ce5a4c1fdc 100644 --- a/src/pyj/book_list/book_details.pyj +++ b/src/pyj/book_list/book_details.pyj @@ -143,7 +143,8 @@ def add_stars_to(stars, val, allow_half_stars): stars.appendChild(svgicon('star-half')) -window.addEventListener('resize', debounce(adjust_all_iframes, 250)) +if window?: + window.addEventListener('resize', debounce(adjust_all_iframes, 250)) def adjusting_sandboxed_html(html, extra_css): color = get_color_as_rgba('window-foreground') diff --git a/src/pyj/read_book/flow_mode.pyj b/src/pyj/read_book/flow_mode.pyj index 3a74f02eca..d01c367df7 100644 --- a/src/pyj/read_book/flow_mode.pyj +++ b/src/pyj/read_book/flow_mode.pyj @@ -244,6 +244,7 @@ def handle_shortcut(sc_name, evt): def layout(is_single_page): + add_visibility_listener() line_height(True) rem_size(True) set_css(document.body, margin='0', border_width='0', padding='0') @@ -258,13 +259,17 @@ def auto_scroll_resume(): scroll_animator.sync() -# Pause auto-scroll while minimized -document.addEventListener("visibilitychange", def(): - if (document.visibilityState is 'visible'): - scroll_animator.sync() - else: - scroll_animator.pause() -) +def add_visibility_listener(): + if add_visibility_listener.done: + return + add_visibility_listener.done = True + # Pause auto-scroll while minimized + document.addEventListener("visibilitychange", def(): + if (document.visibilityState is 'visible'): + scroll_animator.sync() + else: + scroll_animator.pause() + ) def cancel_scroll(): diff --git a/src/pyj/read_book/prefs/head_foot.pyj b/src/pyj/read_book/prefs/head_foot.pyj index 93cfd67d19..58e242725a 100644 --- a/src/pyj/read_book/prefs/head_foot.pyj +++ b/src/pyj/read_book/prefs/head_foot.pyj @@ -146,7 +146,7 @@ def commit_head_foot(onchange, container): onchange() -if window.Intl?.DateTimeFormat: +if window?.Intl?.DateTimeFormat: time_formatter = window.Intl.DateTimeFormat(undefined, {'hour':'numeric', 'minute':'numeric'}) else: time_formatter = {'format': def(date): diff --git a/src/pyj/read_book/tts.pyj b/src/pyj/read_book/tts.pyj index 0ff26f1d74..07f7636e36 100644 --- a/src/pyj/read_book/tts.pyj +++ b/src/pyj/read_book/tts.pyj @@ -20,7 +20,6 @@ def escaper(): return def(text): el.nodeValue = text return ser.serializeToString(el) -escape_for_xml = escaper() class Client: @@ -29,6 +28,7 @@ class Client: max_rate = 2 def __init__(self): + self.escape_for_xml = escaper() self.stop_requested_at = None self.status = {'synthesizing': False, 'paused': False} self.queue = v'[]' @@ -134,7 +134,7 @@ class Client: # buf.push('') buf.push('') else: - buf.push(escape_for_xml(x)) + buf.push(self.escape_for_xml(x)) size += buf[-1].length if size > 24000: buf = v'[]' diff --git a/src/pyj/select.pyj b/src/pyj/select.pyj index 1997b137ab..42d9d2741f 100644 --- a/src/pyj/select.pyj +++ b/src/pyj/select.pyj @@ -3,7 +3,7 @@ from __python__ import bound_methods, hash_literals -if document.caretPositionFromPoint: +if document?.caretPositionFromPoint: caret_position_from_point = document.caretPositionFromPoint.bind(document) else: caret_position_from_point = def(x, y): diff --git a/src/pyj/utils.pyj b/src/pyj/utils.pyj index d212127fa1..362c9aa3ab 100644 --- a/src/pyj/utils.pyj +++ b/src/pyj/utils.pyj @@ -8,7 +8,7 @@ from book_list.theme import get_font_family is_ios = v'!!navigator.platform && /iPad|iPhone|iPod/.test(navigator.platform)' -if !is_ios and window.navigator.platform is 'MacIntel' and window.navigator.maxTouchPoints > 1: +if !is_ios and v'!!navigator.platform' and window? and window.navigator.platform is 'MacIntel' and window.navigator.maxTouchPoints > 1: # iPad Safari in desktop mode https://stackoverflow.com/questions/57765958/how-to-detect-ipad-and-ipad-os-version-in-ios-13-and-up is_ios = True