From 7bcd4d6f6d121d98af9bb27dc046a4a0cdf9bd2f Mon Sep 17 00:00:00 2001 From: OldLiu <632171029@qq.com> Date: Mon, 31 Jul 2023 18:23:45 +0800 Subject: [PATCH] add sw scope support & auto redirect --- assets/js/main.js | 2 ++ assets/js/tmpl.js | 8 +++++--- index.html | 6 ++++++ 3 files changed, 13 insertions(+), 3 deletions(-) diff --git a/assets/js/main.js b/assets/js/main.js index 77791ed..fe13355 100644 --- a/assets/js/main.js +++ b/assets/js/main.js @@ -301,12 +301,14 @@ async function genCode() { const privacy = optPrivacy.options[optPrivacy.selectedIndex].value const hashStr = hashExp ? bytesToB64(hashExp) : '' const urlsStr = imgUrls.join("', '") + let pathPrefix = document.getElementById('pathPrefix').value.replaceAll('/', '').replaceAll('\\', '') let js = `\ var HASH = '${hashStr}' var URLS = ['${urlsStr}'] var PRIVACY = ${privacy} var UPDATE_INTERVAL = 120 var IMG_TIMEOUT = 10 +var PATH_PREFIX = '${pathPrefix}' ${tmplCode} ` diff --git a/assets/js/tmpl.js b/assets/js/tmpl.js index 5940397..12ac606 100644 --- a/assets/js/tmpl.js +++ b/assets/js/tmpl.js @@ -22,7 +22,7 @@ function pageEnv() { var pendings = [] if (!sw.controller) { - var swPending = sw.register(jsUrl).catch(function(err) { + var swPending = sw.register(jsUrl, {scope: getRootPath(jsUrl)}).catch(function(err) { fallback(err.message) }) pendings.push(swPending) @@ -46,7 +46,8 @@ function pageEnv() { ) } Promise.all(pendings).then(function() { - location.reload() + // location.reload() + location.replace(getRootPath(jsUrl)) }) } @@ -295,7 +296,8 @@ function getRootPath(url) { // e.g. // 'https://mysite.com/' // 'https://xx.github.io/path/to/' - return url.split('?')[0].replace(/[^/]+$/, '') + return url.split('?')[0].replace(/[^/]+$/, '') + (PATH_PREFIX ? PATH_PREFIX + '/' : '') + } if (self.document) { diff --git a/index.html b/index.html index f025216..6c2edef 100644 --- a/index.html +++ b/index.html @@ -42,6 +42,12 @@ +

Step2.5

+

Enter a sub folder name:

+
+ +
+

Step3