Files
calibre/src/pyj/srv.pyj
T

70 lines
2.8 KiB
Plaintext

# vim:fileencoding=utf-8
# License: GPL v3 Copyright: 2015, Kovid Goyal <kovid at kovidgoyal.net>
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 read_book.iframe import init
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:
init()
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)
window.applicationCache.addEventListener('updateready', on_appcache_ready)
if window.applicationCache.status is window.applicationCache.UPDATEREADY:
on_appcache_ready()
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