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 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}
`

View file

@ -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) {

View file

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