Browse Source

似乎是已经把所有的内容增加进去了

tags/v24.0
枫谷剑仙 4 years ago
parent
commit
828ffeb842
4 changed files with 96 additions and 35 deletions
  1. +13
    -1
      doc/export-player-data.html
  2. +13
    -1
      doc/index.html
  3. +2
    -0
      run http-server.bat
  4. +68
    -33
      service-worker.js

+ 13
- 1
doc/export-player-data.html View File

@@ -4,7 +4,7 @@
<head>
<meta charset="utf-8">
<title>智龙急速阵型帮助</title>
<link rel="shortcut icon" href="images/icon.png" type="image/x-png" />
<link rel="shortcut icon" href="../images/icon.png" type="image/x-png" />
<meta name="viewport"
content="width=device-width,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<style type="text/css">
@@ -27,6 +27,18 @@
margin-bottom: 5px;
}
</style>
<script type="text/javascript">
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('../service-worker.js', {scope: '../'})
.then(function(registration) {
console.log('service worker 注册成功',registration);
}).catch(function(error) {
console.error('servcie worker 注册失败',error);
});
} else {
console.error('浏览器不支持 servcie worker');
}
</script>
</head>

<body>


+ 13
- 1
doc/index.html View File

@@ -4,7 +4,7 @@
<head>
<meta charset="utf-8">
<title>智龙急速阵型帮助</title>
<link rel="shortcut icon" href="images/icon.png" type="image/x-png" />
<link rel="shortcut icon" href="../images/icon.png" type="image/x-png" />
<meta name="viewport"
content="width=device-width,initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no" />
<style type="text/css">
@@ -20,6 +20,18 @@
margin-bottom: 5px;
}
</style>
<script type="text/javascript">
if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('../service-worker.js', {scope: '../'})
.then(function(registration) {
console.log('service worker 注册成功',registration);
}).catch(function(error) {
console.error('servcie worker 注册失败',error);
});
} else {
console.error('浏览器不支持 servcie worker');
}
</script>
</head>

<body>


+ 2
- 0
run http-server.bat View File

@@ -0,0 +1,2 @@
@echo off
http-server -p 80

+ 68
- 33
service-worker.js View File

@@ -1,4 +1,7 @@
const CACHES = new Map([
['program', 'program-cache-v1'],
['data', 'data-cache-v1'],
['document', 'document-cache-v1'],
['font', 'font-cache-v1'],
['cards_ja', 'card_ja-cache-v1'],
['cards_en', 'card_en-cache-v1'],
@@ -9,17 +12,29 @@
]);
self.addEventListener('install', function(event) {
console.debug("安装中", ENV);
self.skipWaiting();
event.waitUntil(
cache.open(CACHES.get("font")).then(function(cache) {
return cache.addAll([
'/PADDashFormation/fonts/fa-solid-900.woff2',
'/PADDashFormation/fonts/FOT-KurokaneStd-EB.woff2',
'/PADDashFormation/fonts/zpix.woff2',
]);
})
);
console.debug("Service Worker 安装中");
const preCache = () => {
return Promise.all([
cache.open(CACHES.get("font")).then(function(cache) {
return cache.addAll([
'fonts/fa-solid-900.woff2',
'fonts/FOT-KurokaneStd-EB.woff2',
'fonts/zpix.woff2',
]);
}),
cache.open(CACHES.get("library")).then(function(cache) {
return cache.addAll([
'library/html2canvas.min.js',
'library/zxing.umd.min.js',
'library/jy4340132-aaa/adpcm.js',
'library/jy4340132-aaa/adpcm.wasm',
'library/jy4340132-aaa/pcm_player.js',
'library/jy4340132-aaa/std.js',
]);
}),
]);
};
event.waitUntil(preCache());
});
self.addEventListener('activate', function(event) {
@@ -40,28 +55,48 @@ self.addEventListener('activate', function(event) {
self.addEventListener('fetch', function(event) {
event.respondWith(
caches.match(event.request).then(function(resp) {
if (resp) console.debug("找到缓存", event.request.url);
return resp || fetch(event.request).then(async function(response) {
//console.debug("正则测试",/images\/cards\w+\/CARDS_\d+\.PNG/i.test(event.request.url));
if (event.request.url.includes(".woff2")) { //缓存字体
console.debug("缓存字体", event.request.url);
const cache = await caches.open(CACHES.get("font"));
cache.put(event.request, response.clone());
} else if (/images\/cards_\w+\/CARDS_\d+\.PNG/i.test(event.request.url)) { //缓存卡片图
let regRes = /cards_(ja|en|ko)/i.exec(event.request.url);
let langCode = regRes[1];
console.debug("缓存Cards-" + langCode, event.request.url);
const cache = await caches.open(CACHES.get("cards_" + langCode));
cache.put(event.request, response.clone());
} else if (/sound\/voice\/\w+\/padv\d+.wav/i.test(event.request.url)) { //缓存音效
let regRes = /\/(ja|en|ko)\//i.exec(event.request.url);
let langCode = regRes[1];
console.debug("缓存Voice-" + langCode, event.request.url);
const cache = await caches.open(CACHES.get("voice_" + langCode));
cache.put(event.request, response.clone());
}
return response;
});
const url = new URL(event.request.url);
const path = url.pathname;
if (/\.json$/i.test(path)) { //json数据优先通过网络获取
return fetch(event.request).then(async function(response) {
console.debug("缓存数据", url);
const cache = await caches.open(CACHES.get("data"));
cache.put(url.origin + path, response.clone());
return response;
}).catch((err)=>resp);
} else { //其他的优先使用缓存
if (resp) console.debug("找到缓存", event.request.url);
return resp || fetch(event.request).then(async function(response) {
//console.debug("正则测试",/images\/cards\w+\/CARDS_\d+\.PNG/i.test(event.request.url));
if (/\.(html|js|css|wasm)$/i.test(path) ||
/\/images\/[\w\-]+\.(png|svg)/i.test(path)) { //缓存程序
console.debug("缓存程序", url);
const cache = await caches.open(CACHES.get("program"));
cache.put(event.request, response.clone());
} else if (/\/doc\//i.test(path)) { //缓存文档
console.debug("缓存文档", url);
const cache = await caches.open(CACHES.get("document"));
cache.put(event.request, response.clone());
} else if (/\.woff2$/i.test(path)) { //缓存字体
console.debug("缓存字体", url);
const cache = await caches.open(CACHES.get("font"));
cache.put(event.request, response.clone());
} else if (/images\/cards_\w+\/CARDS_\d+\.PNG/i.test(path)) { //缓存卡片图
let regRes = /cards_(\w+)/i.exec(path);
let langCode = regRes[1];
console.debug("缓存Cards-" + langCode, url);
const cache = await caches.open(CACHES.get("cards_" + langCode));
cache.put(event.request, response.clone());
} else if (/sound\/voice\/\w+\/padv\d+.wav/i.test(path)) { //缓存音效
let regRes = /\/(\w+)\//i.exec(path);
let langCode = regRes[1];
console.debug("缓存Voice-" + langCode, url);
const cache = await caches.open(CACHES.get("voice_" + langCode));
cache.put(event.request, response.clone());
}
return response;
});
}
})
);
});

Loading…
Cancel
Save