|
@@ -2,108 +2,108 @@
|
|
|
|
|
|
console.log("sw printing before chrome58StopHere");
|
|
console.log("sw printing before chrome58StopHere");
|
|
var stop = true;
|
|
var stop = true;
|
|
-(function() {
|
|
+
|
|
- try {
|
|
+
|
|
- Function("var a = {...{}};");
|
|
+
|
|
- stop = false;
|
|
+
|
|
- } catch (error) {
|
|
+
|
|
- console.log(error);
|
|
+
|
|
- }
|
|
+
|
|
-})();
|
|
+
|
|
console.log("sw printing after chrome58StopHere ", stop);
|
|
console.log("sw printing after chrome58StopHere ", stop);
|
|
|
|
|
|
|
|
|
|
-if (!stop) {
|
|
+
|
|
- self.addEventListener("fetch", event => {
|
|
+
|
|
-
|
|
+
|
|
- if (
|
|
+
|
|
- event.request.url.startsWith(
|
|
+
|
|
- "https://ecs-test-static.qmth.com.cn/comm-ques-bank/dev/audio"
|
|
+
|
|
- ) ||
|
|
+
|
|
- event.request.url.startsWith(
|
|
+
|
|
- "https://ecs-static.qmth.com.cn/comm-ques-bank/prod/audio/"
|
|
+
|
|
- )
|
|
+
|
|
- ) {
|
|
+
|
|
- event.respondWith(
|
|
+
|
|
- (async function() {
|
|
+
|
|
-
|
|
+
|
|
-
|
|
+
|
|
- const cachedResponse = await caches.match(event.request);
|
|
+
|
|
-
|
|
+
|
|
- if (cachedResponse) {
|
|
+
|
|
- console.log("cache res", event.request.url);
|
|
+
|
|
- return cachedResponse;
|
|
+
|
|
- }
|
|
+
|
|
|
|
|
|
- console.log("fetch audio intercept, try to load all");
|
|
+
|
|
- const res = await fetch(event.request.url);
|
|
+
|
|
|
|
|
|
- const reader = res.body.getReader();
|
|
+
|
|
|
|
|
|
-
|
|
+
|
|
- const contentLength = +res.headers.get("Content-Length");
|
|
+
|
|
|
|
|
|
- if (contentLength === 0) {
|
|
+
|
|
- return fetch(event.request);
|
|
+
|
|
- }
|
|
+
|
|
|
|
|
|
-
|
|
+
|
|
- let receivedLength = 0;
|
|
+
|
|
- let chunks = [];
|
|
+
|
|
-
|
|
+
|
|
- while (true) {
|
|
+
|
|
- const { done, value } = await reader.read();
|
|
+
|
|
|
|
|
|
- if (done) {
|
|
+
|
|
- break;
|
|
+
|
|
- }
|
|
+
|
|
|
|
|
|
- chunks.push(value);
|
|
+
|
|
- receivedLength += value.length;
|
|
+
|
|
|
|
|
|
-
|
|
+
|
|
|
|
|
|
- self.clients.matchAll().then(function(clients) {
|
|
+
|
|
- if (clients && clients.length) {
|
|
+
|
|
- clients.forEach(function(client) {
|
|
+
|
|
- client.postMessage([
|
|
+
|
|
- event.request.url,
|
|
+
|
|
- receivedLength,
|
|
+
|
|
- contentLength,
|
|
+
|
|
- ]);
|
|
+
|
|
- });
|
|
+
|
|
- }
|
|
+
|
|
- });
|
|
+
|
|
- }
|
|
+
|
|
|
|
|
|
-
|
|
+
|
|
-
|
|
+
|
|
-
|
|
+
|
|
-
|
|
+
|
|
-
|
|
+
|
|
-
|
|
+
|
|
-
|
|
+
|
|
|
|
|
|
-
|
|
+
|
|
-
|
|
+
|
|
- const blob = new Blob(chunks);
|
|
+
|
|
- const responseFinal = new Response(blob, {
|
|
+
|
|
- headers: {
|
|
+
|
|
- "Content-Type": "audio/mp3",
|
|
+
|
|
- "Content-Length": contentLength,
|
|
+
|
|
- },
|
|
+
|
|
- });
|
|
+
|
|
|
|
|
|
- caches.open("audios").then(function(cache) {
|
|
+
|
|
- cache.put(event.request, responseFinal.clone());
|
|
+
|
|
- });
|
|
+
|
|
|
|
|
|
- return responseFinal.clone();
|
|
+
|
|
-
|
|
+
|
|
-
|
|
+
|
|
- })()
|
|
+
|
|
- );
|
|
+
|
|
- }
|
|
+
|
|
- });
|
|
+
|
|
-}
|
|
+
|
|
console.log("sw printing end");
|
|
console.log("sw printing end");
|