serviceWorkerAppend.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. // eslint-disable-next-line
  59. while (true) {
  60. const { done, value } = await reader.read();
  61. if (done) {
  62. break;
  63. }
  64. chunks.push(value);
  65. receivedLength += value.length;
  66. // console.log(`Received ${receivedLength} of ${contentLength}`);
  67. self.clients.matchAll().then(function(clients) {
  68. if (clients && clients.length) {
  69. clients.forEach(function(client) {
  70. client.postMessage([
  71. event.request.url,
  72. receivedLength,
  73. contentLength,
  74. ]);
  75. });
  76. }
  77. });
  78. }
  79. // Step 4: concatenate chunks into single Uint8Array
  80. // let chunksAll = new Uint8Array(receivedLength); // (4.1)
  81. // let position = 0;
  82. // for (let chunk of chunks) {
  83. // chunksAll.set(chunk, position); // (4.2)
  84. // position += chunk.length;
  85. // }
  86. // const blob = await res.clone().blob();
  87. // const blob = new Blob([chunksAll.buffer]);
  88. const blob = new Blob(chunks);
  89. const responseFinal = new Response(blob, {
  90. headers: {
  91. "Content-Type": "audio/mp3",
  92. "Content-Length": contentLength,
  93. },
  94. });
  95. caches.open("audios").then(function(cache) {
  96. cache.put(event.request, responseFinal.clone());
  97. });
  98. return responseFinal.clone();
  99. }
  100. // If we didn't find a match in the cache, use the network.
  101. return fetch(event.request);
  102. })()
  103. );
  104. });
  105. }
  106. console.log("sw printing end");