From d0c34d21318cd36c5cf43c250f97055afa58b0e5 Mon Sep 17 00:00:00 2001 From: Kovid Goyal Date: Sun, 16 May 2021 08:52:50 +0530 Subject: [PATCH] Avoid unnecessary replace on JS Also makes the JS available for use in Web Workers --- src/pyj/iframe_comm.pyj | 9 +-------- src/pyj/srv.pyj | 15 +++++++++++---- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/pyj/iframe_comm.pyj b/src/pyj/iframe_comm.pyj index 22b80b09cd..efef736e66 100644 --- a/src/pyj/iframe_comm.pyj +++ b/src/pyj/iframe_comm.pyj @@ -32,13 +32,6 @@ __BS__ '''.replace('end_script', '<' + '/script>') # cannot have a closing script tag as this is embedded inside a script tag in index.html -def iframe_js(): - if not iframe_js.ans: - iframe_js.ans = main_js().replace(/is_running_in_iframe\s*=\s*false/, 'is_running_in_iframe = true') - main_js(None) - return iframe_js.ans - - class Messenger: def __init__(self): @@ -91,7 +84,7 @@ class IframeWrapper: if self.entry_point: data = { 'BS': self.bootstrap_text, - 'SCRIPT': iframe_js(), + 'SCRIPT': main_js(), 'FONT': get_font_family(), 'ENTRY_POINT': self.entry_point, } diff --git a/src/pyj/srv.pyj b/src/pyj/srv.pyj index 582d1ceb95..646965b1fa 100644 --- a/src/pyj/srv.pyj +++ b/src/pyj/srv.pyj @@ -9,11 +9,11 @@ from autoreload import create_auto_reload_watcher from book_list.globals import main_js from book_list.main import main -is_running_in_iframe = False # Changed before script is loaded in the iframe autoreload_enabled = False AUTO_UPDATE_THRESHOLD = 1000 # millisecs -if is_running_in_iframe: + +def iframe_main(): script = document.getElementById('bootstrap') if script: script.parentNode.removeChild(script) # free up some memory @@ -23,9 +23,9 @@ if is_running_in_iframe: main() else: console.log('iframe entry_point ' + window.iframe_entry_point + ' not found') -else: - start_time = window.performance.now() + +def toplevel_main(): script = document.currentScript or document.scripts[0] main_js(script.textContent) script.parentNode.removeChild(script) # save some memory @@ -52,3 +52,10 @@ else: if data.allow_console_print: print = console_print ).send() # We must bypass cache as otherwise we could get stale info + +if document?: + iframe_script = document.getElementById('bootstrap') + if iframe_script: + iframe_main() + else: + toplevel_main()