| 1 |
# vim:fileencoding=utf-8 |
| 2 |
# License: GPL v3 Copyright: 2015, Kovid Goyal <kovid at kovidgoyal.net> |
| 3 |
# globals: ρσ_get_module, self |
| 4 |
from __python__ import hash_literals |
| 5 |
|
| 6 |
import initialize # noqa: unused-import |
| 7 |
from ajax import ajax, console_print, set_default_timeout, absolute_path |
| 8 |
from autoreload import create_auto_reload_watcher |
| 9 |
from book_list.comments_editor import main as prevent_comments_editor_main_from_being_pruned |
| 10 |
from book_list.globals import main_js |
| 11 |
from book_list.main import main |
| 12 |
from read_book.footnotes import main as prevent_footnotes_main_from_being_pruned |
| 13 |
from read_book.globals import runtime |
| 14 |
from read_book.iframe import main as prevent_iframe_main_from_being_pruned |
| 15 |
|
| 16 |
autoreload_enabled = False |
| 17 |
AUTO_UPDATE_THRESHOLD = 1000 # millisecs |
| 18 |
|
| 19 |
|
| 20 |
@no_prune |
| 21 |
def prevent_pruning(): |
| 22 |
prevent_iframe_main_from_being_pruned() |
| 23 |
prevent_comments_editor_main_from_being_pruned() |
| 24 |
prevent_footnotes_main_from_being_pruned() |
| 25 |
|
| 26 |
|
| 27 |
def worker_main(entry_point): |
| 28 |
m = ρσ_get_module(entry_point) |
| 29 |
main = m?.main |
| 30 |
if main: |
| 31 |
main() |
| 32 |
else: |
| 33 |
console.log('worker entry_point ' + entry_point + ' not found') |
| 34 |
|
| 35 |
|
| 36 |
def iframe_main(script): |
| 37 |
script.parentNode.removeChild(script) # free up some memory |
| 38 |
script = undefined |
| 39 |
m = ρσ_get_module(window.iframe_entry_point) |
| 40 |
main = m?.main |
| 41 |
if main: |
| 42 |
main() |
| 43 |
else: |
| 44 |
console.log('iframe entry_point ' + window.iframe_entry_point + ' not found') |
| 45 |
|
| 46 |
|
| 47 |
def toplevel_main(): |
| 48 |
script = document.currentScript or document.scripts[0] |
| 49 |
main_js(script.textContent) |
| 50 |
script.parentNode.removeChild(script) # save some memory |
| 51 |
script = undefined |
| 52 |
if window.navigator.serviceWorker: |
| 53 |
window.navigator.serviceWorker.register(absolute_path('service_worker.js')).catch( |
| 54 |
def (err): |
| 55 |
console.error('Service worker registration failed:', err) |
| 56 |
) |
| 57 |
|
| 58 |
ajax('ajax-setup', def (end_type, xhr, event): |
| 59 |
nonlocal autoreload_enabled, print |
| 60 |
if end_type is 'load': |
| 61 |
try: |
| 62 |
data = JSON.parse(xhr.responseText) |
| 63 |
except: |
| 64 |
return |
| 65 |
tim = data.ajax_timeout |
| 66 |
if not isNaN(tim) and tim > 0: |
| 67 |
set_default_timeout(tim) |
| 68 |
port = data.auto_reload_port |
| 69 |
if not isNaN(port) and port > 0: |
| 70 |
autoreload_enabled = True |
| 71 |
create_auto_reload_watcher(port) |
| 72 |
if data.allow_console_print: |
| 73 |
print = console_print |
| 74 |
if data.in_develop_mode: |
| 75 |
runtime.in_develop_mode = True |
| 76 |
# we dont care about failure here, we assume defaults. We need this to |
| 77 |
# load once so that browser does http auth and caches the credentials |
| 78 |
# for future ajax requests |
| 79 |
main() |
| 80 |
).send() # We must bypass cache as otherwise we could get stale info |
| 81 |
|
| 82 |
if document?: |
| 83 |
iframe_script = document.getElementById('bootstrap') |
| 84 |
if iframe_script: |
| 85 |
iframe_main(iframe_script) |
| 86 |
else: |
| 87 |
toplevel_main() |
| 88 |
elif self?: |
| 89 |
entry_point = self.location.hash[1:] |
| 90 |
worker_main(entry_point) |