serviceWorkerAppend.js 3.3 KB

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