diff --git a/assets/js/main.js b/assets/js/main.js index 3706bfb..73ffbc9 100644 --- a/assets/js/main.js +++ b/assets/js/main.js @@ -305,6 +305,7 @@ async function genCode() { var HASH = '${hashStr}' var URLS = ['${urlsStr}'] var PRIVACY = ${privacy} +var UPDATE_TIMER = 120 ${tmplCode} ` diff --git a/assets/js/tmpl.js b/assets/js/tmpl.js index 1eecb92..ae81e38 100644 --- a/assets/js/tmpl.js +++ b/assets/js/tmpl.js @@ -2,17 +2,13 @@ function pageEnv() { var container = document.documentElement function fallback(html) { - var noscript = document.getElementsByTagName('noscript') - if (noscript.length > 0) { - html = noscript[0].innerHTML + var noscripts = document.getElementsByTagName('noscript') + if (noscripts.length > 0) { + html = noscripts[0].innerHTML } container.innerHTML = html } - function reload() { - location.reload() - } - var currentScript = document.currentScript var jsUrl = currentScript.src var rootPath @@ -31,6 +27,29 @@ function pageEnv() { rootPath = currentScript.dataset.root } + function unpackToCache(bytes, cache) { + var info = JSON.stringify({ + hash: HASH, + time: Date.now() + }) + var res = new Response(info) + var pendings = [ + cache.put(rootPath + '.cache-info', res), + swPending, + ] + var pathResMap = unpack(bytes) + + for (var path in pathResMap) { + res = pathResMap[path] + pendings.push( + cache.put(rootPath + path, res) + ) + } + Promise.all(pendings).then(function() { + location.reload() + }) + } + function parseImgBuf(buf) { if (!buf) { loadNextUrl() @@ -44,16 +63,9 @@ function pageEnv() { return } var bytes = decode1Px3Bytes(buf) - caches.delete('.web2img').then(function() { caches.open('.web2img').then(function(cache) { - unpack(bytes, cache).then(function() { - if (swPending) { - swPending.then(reload) - } else { - reload() - } - }) + unpackToCache(bytes, cache) }) }) }) @@ -97,13 +109,13 @@ function pageEnv() { } if (PRIVACY === 2) { - // hide origin header + // hide `origin` header var iframe = document.createElement('iframe') if (typeof RELEASE !== 'undefined') { - iframe.src = 'data:text/html,' + iframe.src = 'data:text/html,') + iframe.src = 'data:text/html;base64,' + btoa('' + if (newJs && req.mode === 'navigate') { + var html = '