add sw scope support & auto redirect

This commit is contained in:
OldLiu 2023-07-31 18:23:45 +08:00
parent f9b49e7d72
commit 7bcd4d6f6d
3 changed files with 13 additions and 3 deletions

View file

@ -301,12 +301,14 @@ async function genCode() {
const privacy = optPrivacy.options[optPrivacy.selectedIndex].value const privacy = optPrivacy.options[optPrivacy.selectedIndex].value
const hashStr = hashExp ? bytesToB64(hashExp) : '' const hashStr = hashExp ? bytesToB64(hashExp) : ''
const urlsStr = imgUrls.join("', '") const urlsStr = imgUrls.join("', '")
let pathPrefix = document.getElementById('pathPrefix').value.replaceAll('/', '').replaceAll('\\', '')
let js = `\ let js = `\
var HASH = '${hashStr}' var HASH = '${hashStr}'
var URLS = ['${urlsStr}'] var URLS = ['${urlsStr}']
var PRIVACY = ${privacy} var PRIVACY = ${privacy}
var UPDATE_INTERVAL = 120 var UPDATE_INTERVAL = 120
var IMG_TIMEOUT = 10 var IMG_TIMEOUT = 10
var PATH_PREFIX = '${pathPrefix}'
${tmplCode} ${tmplCode}
` `

View file

@ -22,7 +22,7 @@ function pageEnv() {
var pendings = [] var pendings = []
if (!sw.controller) { 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) fallback(err.message)
}) })
pendings.push(swPending) pendings.push(swPending)
@ -46,7 +46,8 @@ function pageEnv() {
) )
} }
Promise.all(pendings).then(function() { Promise.all(pendings).then(function() {
location.reload() // location.reload()
location.replace(getRootPath(jsUrl))
}) })
} }
@ -295,7 +296,8 @@ function getRootPath(url) {
// e.g. // e.g.
// 'https://mysite.com/' // 'https://mysite.com/'
// 'https://xx.github.io/path/to/' // 'https://xx.github.io/path/to/'
return url.split('?')[0].replace(/[^/]+$/, '') return url.split('?')[0].replace(/[^/]+$/, '') + (PATH_PREFIX ? PATH_PREFIX + '/' : '')
} }
if (self.document) { if (self.document) {

View file

@ -42,6 +42,12 @@
</div> </div>
</div> </div>
<h2>Step2.5</h2>
<p>Enter a sub folder name:</p>
<div>
<textarea id="pathPrefix"></textarea>
</div>
<h2>Step3</h2> <h2>Step3</h2>
<div> <div>
<textarea id="txtUrls" placeholder="Upload Image And Paste URLs"></textarea> <textarea id="txtUrls" placeholder="Upload Image And Paste URLs"></textarea>