mirror of
https://github.com/kovidgoyal/calibre.git
synced 2026-03-17 15:09:17 -04:00
84 lines
3.4 KiB
Plaintext
84 lines
3.4 KiB
Plaintext
# vim:fileencoding=utf-8
|
||
# License: GPL v3 Copyright: 2015, Kovid Goyal <kovid at kovidgoyal.net>
|
||
# globals: ρσ_get_module
|
||
from __python__ import hash_literals
|
||
|
||
from gettext import gettext as _
|
||
|
||
import initialize # noqa: unused-import
|
||
from ajax import ajax, console_print, set_default_timeout
|
||
from autoreload import create_auto_reload_watcher
|
||
from book_list.globals import main_js
|
||
from book_list.main import main
|
||
from book_list.constants import has_offline_support
|
||
|
||
is_running_in_iframe = False # Changed before script is loaded in the iframe
|
||
autoreload_enabled = False
|
||
asked_appcache_update = False
|
||
AUTO_UPDATE_THRESHOLD = 1000 # millisecs
|
||
|
||
if is_running_in_iframe:
|
||
script = document.getElementById('bootstrap')
|
||
if script:
|
||
script.parentNode.removeChild(script) # free up some memory
|
||
m = ρσ_get_module(window.iframe_entry_point)
|
||
main = m?.main
|
||
if main:
|
||
main()
|
||
else:
|
||
console.log('iframe entry_point ' + window.iframe_entry_point + ' not found')
|
||
else:
|
||
start_time = window.performance.now()
|
||
|
||
on_appcache_ready = def ():
|
||
nonlocal asked_appcache_update
|
||
if asked_appcache_update:
|
||
return
|
||
if window.applicationCache.status is window.applicationCache.UPDATEREADY:
|
||
asked_appcache_update = True
|
||
try:
|
||
window.applicationCache.swapCache()
|
||
time_since_start = window.performance.now() - start_time
|
||
if time_since_start <= AUTO_UPDATE_THRESHOLD or autoreload_enabled or window.confirm(_(
|
||
'The calibre web application has been updated. Do you want reload the site?')):
|
||
window.location.reload()
|
||
except Exception as e:
|
||
# For some reason swapCache occasionally fails even though status is UPDATEREADY
|
||
print('WARNING: failed to swap applicationCache')
|
||
console.log(e)
|
||
|
||
if window.applicationCache:
|
||
window.applicationCache.addEventListener('updateready', on_appcache_ready)
|
||
if window.applicationCache.status is window.applicationCache.UPDATEREADY:
|
||
on_appcache_ready()
|
||
has_offline_support(True)
|
||
else:
|
||
has_offline_support(False)
|
||
console.warn("Your browser does not have a working Application Cache, offline access will not work")
|
||
script = document.currentScript or document.scripts[0]
|
||
main_js(script.textContent)
|
||
script.parentNode.removeChild(script) # save some memory
|
||
script = undefined
|
||
# We wait for all page elements to load, since this is a single page app
|
||
# with a largely empty starting document, we can use this to preload any resources
|
||
# we know are going to be needed immediately.
|
||
window.addEventListener('load', main)
|
||
|
||
ajax('ajax-setup', def(end_type, xhr, event):
|
||
nonlocal autoreload_enabled, print
|
||
if end_type is 'load':
|
||
try:
|
||
data = JSON.parse(xhr.responseText)
|
||
except:
|
||
return
|
||
tim = data.ajax_timeout
|
||
if not isNaN(tim) and tim > 0:
|
||
set_default_timeout(tim)
|
||
port = data.auto_reload_port
|
||
if not isNaN(port) and port > 0:
|
||
autoreload_enabled = True
|
||
create_auto_reload_watcher(port)
|
||
if data.allow_console_print:
|
||
print = console_print
|
||
).send() # We must bypass cache as otherwise we could get stale info
|