Avoid unnecessary replace on JS

Also makes the JS available for use in Web Workers
This commit is contained in:
Kovid Goyal 2021-05-16 08:52:50 +05:30
parent 8988cbcf56
commit d0c34d2131
No known key found for this signature in database
GPG Key ID: 06BC317B515ACE7C
2 changed files with 12 additions and 12 deletions

View File

@ -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 '''.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: class Messenger:
def __init__(self): def __init__(self):
@ -91,7 +84,7 @@ class IframeWrapper:
if self.entry_point: if self.entry_point:
data = { data = {
'BS': self.bootstrap_text, 'BS': self.bootstrap_text,
'SCRIPT': iframe_js(), 'SCRIPT': main_js(),
'FONT': get_font_family(), 'FONT': get_font_family(),
'ENTRY_POINT': self.entry_point, 'ENTRY_POINT': self.entry_point,
} }

View File

@ -9,11 +9,11 @@ from autoreload import create_auto_reload_watcher
from book_list.globals import main_js from book_list.globals import main_js
from book_list.main import main from book_list.main import main
is_running_in_iframe = False # Changed before script is loaded in the iframe
autoreload_enabled = False autoreload_enabled = False
AUTO_UPDATE_THRESHOLD = 1000 # millisecs AUTO_UPDATE_THRESHOLD = 1000 # millisecs
if is_running_in_iframe:
def iframe_main():
script = document.getElementById('bootstrap') script = document.getElementById('bootstrap')
if script: if script:
script.parentNode.removeChild(script) # free up some memory script.parentNode.removeChild(script) # free up some memory
@ -23,9 +23,9 @@ if is_running_in_iframe:
main() main()
else: else:
console.log('iframe entry_point ' + window.iframe_entry_point + ' not found') 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] script = document.currentScript or document.scripts[0]
main_js(script.textContent) main_js(script.textContent)
script.parentNode.removeChild(script) # save some memory script.parentNode.removeChild(script) # save some memory
@ -52,3 +52,10 @@ else:
if data.allow_console_print: if data.allow_console_print:
print = console_print print = console_print
).send() # We must bypass cache as otherwise we could get stale info ).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()