重新整理service-worker,让离线也可以用

This commit is contained in:
枫谷剑仙 2024-04-26 05:00:43 +08:00
parent 94d79da035
commit 426f990326
3 changed files with 128 additions and 100 deletions

View File

@ -9,7 +9,7 @@ const GM_xmlhttpRequest = function(GM_param) {
const _xhr = e.target;
if (_xhr.readyState === _xhr.DONE) { //请求完成时
console.debug("http状态码", _xhr.status);
if ((_xhr.status === 200 || _xhr.status === 0) && GM_param.onload) //正确加载时
if ((_xhr.status === 200) && GM_param.onload) //正确加载时
{
GM_param.onload(_xhr);
} else if (_xhr.status !== 200 && GM_param.onerror) //发生错误时

View File

@ -2045,6 +2045,7 @@ function loadData(force = false)
//开始读取解析怪物数据
const sourceDataFolder = "monsters-info";
dealCkeyData();
statusLine?.writeText(localTranslating.status_message.loading_check_version);
GM_xmlhttpRequest({
@ -2061,22 +2062,34 @@ function loadData(force = false)
//处理返回的数据
function dealCkeyData(responseText)
{ //处理数据版本
const lastKeyString = localStorage.getItem(cfgPrefix + "ckey");
let newCkeys; //当前的Ckey们
let lastCkeys; //以前Ckey们
let currentCkey; //获取当前语言的ckey
let lastCurrentCkey; //以前的当前语言的ckey
try {
newCkeys = JSON.parse(responseText);
} catch (e) {
console.error("新的 Ckey 数据 JSON 解码出错。", e);
return;
if (responseText === undefined) {
try {
newCkeys = JSON.parse(lastKeyString);
console.info("提前预载原来已经储存的数据");
} catch (e) {
console.error("以前还没有下载过数据。", e);
return;
}
} else {
try {
newCkeys = JSON.parse(responseText);
} catch (e) {
console.error("新的 Ckey 数据 JSON 解码出错。", e);
return;
}
}
console.debug("目前使用的数据区服是 %s。", currentDataSource.code);
currentCkey = newCkeys.find(ckey => ckey.code == currentDataSource.code); //获取当前语言的ckey
try {
lastCkeys = JSON.parse(localStorage.getItem(cfgPrefix + "ckey")); //读取本地储存的原来的ckey
lastCkeys = JSON.parse(lastKeyString); //读取本地储存的原来的ckey
if (!Boolean(lastCkeys) || !Array.isArray(lastCkeys))
lastCkeys = [];
} catch (e) {

View File

@ -25519,11 +25519,11 @@ const cachesMap = new Map([
],
[
"script-universal_function.js",
"7d4cc7769b061422c6f55b392f3fd445"
"4c6512cc2876102856ecf24775673805"
],
[
"script.js",
"bf5e60b56a64419c5592a5c641258d3f"
"3e1b60d425c9148f70ea81d518c217fd"
],
[
"solo.html",
@ -25753,6 +25753,10 @@ const cachesMap = new Map([
"library/html2canvas.min.js",
"d7530aa0b7587e627484c49fdf8f13f2"
],
[
"library/minified.js",
"4d02ac7bb781ce3f52a8008f2047e77e"
],
[
"library/zxing.umd.min.js",
"46934f662940b020aee6466c1e334c5f"
@ -26241,37 +26245,48 @@ self.addEventListener('fetch', async function(event) {
//console.debug("请求网络", event.request.url, relativePath, md5);
if (md5) {
url.searchParams.set("md5", md5);
const responseWithMd5 = await caches.match(url, {ignoreSearch: false});
if (responseWithMd5) {
console.debug("%c已有相同 md5 缓存,直接使用缓存:%s%s", "color: green;", relativePath, md5);
event.respondWith(responseWithMd5);
} else {
//console.debug("%c无相同 md5 缓存,重新在线获取", "color: blue;", url);
let newResponse;
const responseSync = (async () => {
// Try to get the response from a cache.
const responseWithMd5 = await caches.match(url, {ignoreSearch: false});
// Return it if we found one.
if (responseWithMd5) {
console.debug("%c已有相同 md5 缓存,直接使用缓存:%s%s", "color: green;", relativePath, md5);
return responseWithMd5;
}
// If we didn't find a match in the cache, use the network.
try {
newResponse = await fetch(event.request);
console.debug("%c无相同 md5 缓存,重新在线获取结果:%s%s", "color: blue;", relativePath, md5);
const newResponse = fetch(event.request);
console.debug("%c无相同 md5 缓存,重新在线获取结果:%s%s", "color: lightblue;", relativePath, md5);
const response = await newResponse;
const cache = await caches.open(cacheName);
await cache.put(url, newResponse.clone());
await cache.put(url, response.clone());
return response;
//console.debug("%c储存新的Cache", "color: blue;", url, cache);
} catch (error) {
console.error("%c数据在线获取失败尝试使用忽略 search 的离线数据:%s%s", "color: red;", relativePath, md5, error);
newResponse = await caches.match(url, {ignoreSearch: true});
console.error("%c数据在线获取失败-有hash尝试使用忽略 search 的离线数据:%s%s", "color: red;", relativePath, md5, error);
const response = await caches.match(url, {ignoreSearch: true});
return response || new Response(new Blob(), { status: 418, statusText: "数据在线获取失败" });
}
event.respondWith(newResponse);
}
})();
event.respondWith(responseSync);
} else {
console.debug("无 md5 值,重新在线获取:%s", relativePath);
let newResponse;
try {
newResponse = await fetch(event.request);
const cache = await caches.open(cacheName);
await cache.put(url, newResponse.clone());
event.respondWith(newResponse);
} catch (error) {
console.error("%c数据在线获取失败尝试使用忽略 search 的离线数据:%s", "color: red;", relativePath, error);
newResponse = await caches.match(url, {ignoreSearch: true});
}
event.respondWith(newResponse);
console.debug("%c无 md5 值,重新在线获取:%s", "color: lightblue;", relativePath);
const responseSync = (async () => {
try {
const newResponse = fetch(event.request);
const response = await newResponse;
const cache = await caches.open(cacheName);
await cache.put(url, response.clone());
return response;
//console.debug("%c储存新的Cache", "color: blue;", url, cache);
} catch (error) {
console.error("%c数据在线获取失败-无hash尝试使用忽略 search 的离线数据:%s", "color: red;", relativePath, error);
const response = await caches.match(url, {ignoreSearch: true});
return response || new Response(new Blob(), { status: 418, statusText: "数据在线获取失败" });
}
})();
event.respondWith(responseSync);
}
});