serviceWorkerAppend.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. event.respondWith(
  19. (async function() {
  20. // console.log("fetch intercepted");
  21. // Try to get the response from a cache.
  22. const cachedResponse = await caches.match(event.request);
  23. // Return it if we found one.
  24. if (cachedResponse) {
  25. console.log("cache res", event.request.url);
  26. return cachedResponse;
  27. }
  28. if (
  29. event.request.url.startsWith(
  30. "https://ecs-test-static.qmth.com.cn/comm-ques-bank/dev/audio"
  31. ) ||
  32. event.request.url.startsWith(
  33. "https://ecs-static.qmth.com.cn/comm-ques-bank/prod/audio/"
  34. )
  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. // console.log("content-length", contentLength);
  45. // self.sessionStorage.setItem("test-sw", "1");
  46. // self.postMessage({
  47. // url: event.request.url,
  48. // progress: 1,
  49. // });
  50. // self.addEventListener('message', function(event){
  51. // console.log("SW Received Message: " + event.data);
  52. // console.log(event);
  53. // event.ports[0].postMessage("SW Says 'Hello back!'");
  54. // });
  55. // Step 3: read the data
  56. let receivedLength = 0; // received that many bytes at the moment
  57. let chunks = []; // array of received binary chunks (comprises the body)
  58. while (true) {
  59. const { done, value } = await reader.read();
  60. if (done) {
  61. break;
  62. }
  63. chunks.push(value);
  64. receivedLength += value.length;
  65. // console.log(`Received ${receivedLength} of ${contentLength}`);
  66. self.clients.matchAll().then(function(clients) {
  67. if (clients && clients.length) {
  68. clients.forEach(function(client) {
  69. client.postMessage([
  70. event.request.url,
  71. receivedLength,
  72. contentLength,
  73. ]);
  74. });
  75. }
  76. });
  77. }
  78. // Step 4: concatenate chunks into single Uint8Array
  79. // let chunksAll = new Uint8Array(receivedLength); // (4.1)
  80. // let position = 0;
  81. // for (let chunk of chunks) {
  82. // chunksAll.set(chunk, position); // (4.2)
  83. // position += chunk.length;
  84. // }
  85. // const blob = await res.clone().blob();
  86. // const blob = new Blob([chunksAll.buffer]);
  87. const blob = new Blob(chunks);
  88. const responseFinal = new Response(blob, {
  89. headers: {
  90. "Content-Type": "audio/mp3",
  91. "Content-Length": contentLength,
  92. },
  93. });
  94. caches.open("audios").then(function(cache) {
  95. cache.put(event.request, responseFinal.clone());
  96. });
  97. return responseFinal.clone();
  98. }
  99. // If we didn't find a match in the cache, use the network.
  100. return fetch(event.request);
  101. })()
  102. );
  103. });
  104. }
  105. console.log("sw printing end");