serviceWorkerAppend.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. if (contentLength === 0) {
  29. return fetch(event.request);
  30. }
  31. // console.log("content-length", contentLength);
  32. // self.sessionStorage.setItem("test-sw", "1");
  33. // self.postMessage({
  34. // url: event.request.url,
  35. // progress: 1,
  36. // });
  37. // self.addEventListener('message', function(event){
  38. // console.log("SW Received Message: " + event.data);
  39. // console.log(event);
  40. // event.ports[0].postMessage("SW Says 'Hello back!'");
  41. // });
  42. // Step 3: read the data
  43. let receivedLength = 0; // received that many bytes at the moment
  44. let chunks = []; // array of received binary chunks (comprises the body)
  45. while (true) {
  46. const { done, value } = await reader.read();
  47. if (done) {
  48. break;
  49. }
  50. chunks.push(value);
  51. receivedLength += value.length;
  52. // console.log(`Received ${receivedLength} of ${contentLength}`);
  53. self.clients.matchAll().then(function(clients) {
  54. if (clients && clients.length) {
  55. clients.forEach(function(client) {
  56. client.postMessage([
  57. event.request.url,
  58. receivedLength,
  59. contentLength,
  60. ]);
  61. });
  62. }
  63. });
  64. }
  65. // Step 4: concatenate chunks into single Uint8Array
  66. // let chunksAll = new Uint8Array(receivedLength); // (4.1)
  67. // let position = 0;
  68. // for (let chunk of chunks) {
  69. // chunksAll.set(chunk, position); // (4.2)
  70. // position += chunk.length;
  71. // }
  72. // const blob = await res.clone().blob();
  73. // const blob = new Blob([chunksAll.buffer]);
  74. const blob = new Blob(chunks);
  75. const responseFinal = new Response(blob, {
  76. headers: {
  77. "Content-Type": "audio/mp3",
  78. "Content-Length": contentLength,
  79. },
  80. });
  81. caches.open("audios").then(function(cache) {
  82. cache.put(event.request, responseFinal.clone());
  83. });
  84. return responseFinal.clone();
  85. }
  86. // If we didn't find a match in the cache, use the network.
  87. return fetch(event.request);
  88. })()
  89. );
  90. });