serviceWorkerAppend.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /** append by ecs */
  2. // 以下为学生端添加内容
  3. self.addEventListener("fetch", event => {
  4. // Prevent the default, and handle the request ourselves.
  5. event.respondWith(
  6. (async function() {
  7. // console.log("fetch intercepted");
  8. // Try to get the response from a cache.
  9. const cachedResponse = await caches.match(event.request);
  10. // Return it if we found one.
  11. if (cachedResponse) {
  12. console.log("cache res", event.request.url);
  13. return cachedResponse;
  14. }
  15. if (
  16. event.request.url.startsWith(
  17. "https://ecs-test-static.qmth.com.cn/comm-ques-bank/dev/audio"
  18. ) ||
  19. event.request.url.startsWith(
  20. "https://ecs-static.qmth.com.cn/comm-ques-bank/prod/audio/"
  21. )
  22. ) {
  23. console.log("fetch audio intercept, try to load all");
  24. const res = await fetch(event.request.url);
  25. const reader = res.body.getReader();
  26. // Step 2: get total length
  27. const contentLength = +res.headers.get("Content-Length");
  28. // console.log("content-length", contentLength);
  29. // self.sessionStorage.setItem("test-sw", "1");
  30. // self.postMessage({
  31. // url: event.request.url,
  32. // progress: 1,
  33. // });
  34. // self.addEventListener('message', function(event){
  35. // console.log("SW Received Message: " + event.data);
  36. // console.log(event);
  37. // event.ports[0].postMessage("SW Says 'Hello back!'");
  38. // });
  39. // Step 3: read the data
  40. let receivedLength = 0; // received that many bytes at the moment
  41. let chunks = []; // array of received binary chunks (comprises the body)
  42. while (true) {
  43. const { done, value } = await reader.read();
  44. if (done) {
  45. break;
  46. }
  47. chunks.push(value);
  48. receivedLength += value.length;
  49. // console.log(`Received ${receivedLength} of ${contentLength}`);
  50. self.clients.matchAll().then(function(clients) {
  51. if (clients && clients.length) {
  52. clients.forEach(function(client) {
  53. client.postMessage([
  54. event.request.url,
  55. receivedLength,
  56. contentLength,
  57. ]);
  58. });
  59. }
  60. });
  61. }
  62. // Step 4: concatenate chunks into single Uint8Array
  63. // let chunksAll = new Uint8Array(receivedLength); // (4.1)
  64. // let position = 0;
  65. // for (let chunk of chunks) {
  66. // chunksAll.set(chunk, position); // (4.2)
  67. // position += chunk.length;
  68. // }
  69. // const blob = await res.clone().blob();
  70. // const blob = new Blob([chunksAll.buffer]);
  71. const blob = new Blob(chunks);
  72. const responseFinal = new Response(blob, {
  73. headers: {
  74. "Content-Type": "audio/mp3",
  75. "Content-Length": contentLength,
  76. },
  77. });
  78. caches.open("audios").then(function(cache) {
  79. cache.put(event.request, responseFinal.clone());
  80. });
  81. return responseFinal.clone();
  82. }
  83. // If we didn't find a match in the cache, use the network.
  84. return fetch(event.request);
  85. })()
  86. );
  87. });