• <i id='gRomy'><tr id='gRomy'><dt id='gRomy'><q id='gRomy'><span id='gRomy'><b id='gRomy'><form id='gRomy'><ins id='gRomy'></ins><ul id='gRomy'></ul><sub id='gRomy'></sub></form><legend id='gRomy'></legend><bdo id='gRomy'><pre id='gRomy'><center id='gRomy'></center></pre></bdo></b><th id='gRomy'></th></span></q></dt></tr></i><div id='gRomy'><tfoot id='gRomy'></tfoot><dl id='gRomy'><fieldset id='gRomy'></fieldset></dl></div>
  • <tfoot id='gRomy'></tfoot>

      <small id='gRomy'></small><noframes id='gRomy'>

    1. <legend id='gRomy'><style id='gRomy'><dir id='gRomy'><q id='gRomy'></q></dir></style></legend>

        <bdo id='gRomy'></bdo><ul id='gRomy'></ul>
      1. javascript如何修改服务工作者中的当前响应?

        javascript how to modify the current response in service worker?(javascript如何修改服务工作者中的当前响应?)
      2. <i id='Hkd6d'><tr id='Hkd6d'><dt id='Hkd6d'><q id='Hkd6d'><span id='Hkd6d'><b id='Hkd6d'><form id='Hkd6d'><ins id='Hkd6d'></ins><ul id='Hkd6d'></ul><sub id='Hkd6d'></sub></form><legend id='Hkd6d'></legend><bdo id='Hkd6d'><pre id='Hkd6d'><center id='Hkd6d'></center></pre></bdo></b><th id='Hkd6d'></th></span></q></dt></tr></i><div id='Hkd6d'><tfoot id='Hkd6d'></tfoot><dl id='Hkd6d'><fieldset id='Hkd6d'></fieldset></dl></div>

            <bdo id='Hkd6d'></bdo><ul id='Hkd6d'></ul>
            <tfoot id='Hkd6d'></tfoot>
                <tbody id='Hkd6d'></tbody>
            1. <legend id='Hkd6d'><style id='Hkd6d'><dir id='Hkd6d'><q id='Hkd6d'></q></dir></style></legend>

              <small id='Hkd6d'></small><noframes id='Hkd6d'>

                  本文介绍了javascript如何修改服务工作者中的当前响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我使用 service worker 用下面的代码修改 fetch 响应,

                  self.addEventListener('install', function(event) {console.log('安装');});self.addEventListener('activate', function(event) {console.log('声明控制');返回 self.clients.claim();});self.addEventListener('fetch', function(event) {console.log(获取事件")event.respondWith(fetch(event.request).then(function (response) {if(response.url.match('.mp4')){控制台日志(事件);response.arrayBuffer().then(缓冲区=>{让长度 = 100view = new Uint8Array(buffer,0,length)for(i=0,j=length - 1; i

                  这会导致这个警告和错误,

                  <块引用>

                  "的 FetchEvent导致网络错误响应:承诺被拒绝.

                  <块引用>

                  sw.js:60 Uncaught (in promise) TypeError: Failed to execute 'clone' on'Response':响应正文已被使用在 sw.js:60


                  如果我将 clone() 放在 arrayBuffer() 之前,如下所示,

                  self.addEventListener('install', function(event) {console.log('安装');});self.addEventListener('activate', function(event) {console.log('声明控制');返回 self.clients.claim();});self.addEventListener('fetch', function(event) {console.log(获取事件")event.respondWith(fetch(event.request).then(function (response) {if(response.url.match('.mp4')){控制台日志(事件);responseCloned = response.clone();responseCloned.arrayBuffer().then(缓冲区=>{让长度 = 100view = new Uint8Array(buffer,0,length)for(i=0,j=length - 1; i

                  这会导致以下警告和错误,

                  <块引用>

                  "的 FetchEvent导致网络错误响应:a其正文"的响应被锁定不能用于响应请求.

                  <块引用>

                  GET http://localhost/web/357765_decrypted.mp4 net::ERR_FAILED

                  解决方案

                  尝试实例化并返回一个新的 Response() 与解密的字节:

                  self.addEventListener('fetch', event => {if (event.request.url.endsWith('.mp4') {event.respondWith(getDecryptedResponse(event.request))}})异步函数 getDecryptedResponse(request) {常量响应 = 等待获取(请求)const bytes = new Uint8Array(await response.arrayBuffer())返回新的响应(新 Blob([decryptMp4Bytes(字节)],{类型:'application/mp4'}),{headers: response.headers,//这种情况下可能需要})}

                  I use service worker to modify the fetch response with below code,

                  self.addEventListener('install', function(event) {
                    console.log('install');
                  });
                  
                  self.addEventListener('activate', function(event) {
                    console.log('Claiming control');
                    return self.clients.claim();
                  });
                  
                  self.addEventListener('fetch', function(event) {
                    console.log("fetch event")
                    event.respondWith(
                      fetch(event.request).then(function (response) {
                        if(response.url.match('.mp4')){
                          console.log(event);
                          response.arrayBuffer().then(
                            buffer =>{
                              let length = 100
                              view = new Uint8Array(buffer,0,length)
                              for(i=0,j=length - 1; i<j; i++,j--){
                                  view[i] = view[i] ^ view[j]
                                  view[j] = view[j] ^ view[i]
                                  view[i] = view[i] ^ view[j]
                              }
                            }
                          )
                        }
                        return response.clone();
                      })
                    );
                  });
                  
                  

                  Whichi will lead to this warning and error,

                  The FetchEvent for "" resulted in a network error response: the promise was rejected.

                  sw.js:60 Uncaught (in promise) TypeError: Failed to execute 'clone' on 'Response': Response body is already used at sw.js:60


                  If I put the clone() before arrayBuffer() like below,

                  self.addEventListener('install', function(event) {
                    console.log('install');
                  });
                  
                  self.addEventListener('activate', function(event) {
                    console.log('Claiming control');
                    return self.clients.claim();
                  });
                  
                  self.addEventListener('fetch', function(event) {
                    console.log("fetch event")
                    event.respondWith(
                      fetch(event.request).then(function (response) {
                        if(response.url.match('.mp4')){
                          console.log(event);
                          responseCloned = response.clone();
                          responseCloned.arrayBuffer().then(
                            buffer =>{
                              let length = 100
                              view = new Uint8Array(buffer,0,length)
                              for(i=0,j=length - 1; i<j; i++,j--){
                                  view[i] = view[i] ^ view[j]
                                  view[j] = view[j] ^ view[i]
                                  view[i] = view[i] ^ view[j]
                              }
                            }
                          )
                        }
                        return responseCloned;
                      })
                    );
                  });
                  
                  

                  It will lead to below warning and error,

                  The FetchEvent for "" resulted in a network error response: a Response whose "body" is locked cannot be used to respond to a request.

                  GET http://localhost/web/357765_decrypted.mp4 net::ERR_FAILED

                  解决方案

                  Try instantiating and returning a new Response() with the decrypted bytes:

                  self.addEventListener('fetch', event => {
                    if (event.request.url.endsWith('.mp4') {
                      event.respondWith(getDecryptedResponse(event.request))
                    }
                  })
                  
                  async function getDecryptedResponse(request) {
                    const response = await fetch(request)
                    const bytes = new Uint8Array(await response.arrayBuffer())
                  
                    return new Response(
                      new Blob([ decryptMp4Bytes(bytes) ], { type: 'application/mp4' }),
                      {
                        headers: response.headers,  // possibly required for this case
                      }
                    )
                  }
                  

                  这篇关于javascript如何修改服务工作者中的当前响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Fetch multiple links inside foreach loop(在 foreach 循环中获取多个链接)
                  Backbone Fetch Request is OPTIONS method(Backbone Fetch Request 是 OPTIONS 方法)
                  Fetch API leaks memory in Chrome(Fetch API 在 Chrome 中泄漏内存)
                  How can I download and save a file using the Fetch API? (Node.js)(如何使用 Fetch API 下载和保存文件?(Node.js))
                  Send blob data to node using fetch, multer, express(使用 fetch、multer、express 将 blob 数据发送到节点)
                  Sending a custom User-Agent string along with my headers (fetch)(发送自定义用户代理字符串以及我的标头(获取))

                      • <bdo id='hifJm'></bdo><ul id='hifJm'></ul>

                        <small id='hifJm'></small><noframes id='hifJm'>

                        <i id='hifJm'><tr id='hifJm'><dt id='hifJm'><q id='hifJm'><span id='hifJm'><b id='hifJm'><form id='hifJm'><ins id='hifJm'></ins><ul id='hifJm'></ul><sub id='hifJm'></sub></form><legend id='hifJm'></legend><bdo id='hifJm'><pre id='hifJm'><center id='hifJm'></center></pre></bdo></b><th id='hifJm'></th></span></q></dt></tr></i><div id='hifJm'><tfoot id='hifJm'></tfoot><dl id='hifJm'><fieldset id='hifJm'></fieldset></dl></div>
                          <legend id='hifJm'><style id='hifJm'><dir id='hifJm'><q id='hifJm'></q></dir></style></legend>
                          1. <tfoot id='hifJm'></tfoot>
                              <tbody id='hifJm'></tbody>