title: Chrome extension - message port closed before a response was received
body: <p>I get this error in my simple chrome extension. I'm trying to pass some messages from background.js to my content script. The first message will be sent without problems, but the second one give me this error <code>_generated_background_page.html:1 Unchecked runtime.lastError: The message port closed before a response was received.</code></p>
  <p>I need to pass a response from the content script after the first message is recived, this because I need to remove event listener from <code>webRequest.onCompleted</code> otherwise I will get a loop problem with message passing. I'm not sure if I'm doing the things well also with <code>removeListener</code> function.</p>
  <p>Here is my code, any help will be appreciated</p>
  <p>contentScript.js</p>
  <pre><code>const m3u8 = new M3U8(); 
  console.log(m3u8);
  chrome.runtime.onMessage.addListener( message =&gt; {    
      console.log(message);
      chrome.runtime.sendMessage({status: 'ok'})
      const download = m3u8.start(message.url);
      download.on(&quot;progress&quot;, progress =&gt; {
          console.log(progress);
      }).on(&quot;finished&quot;, finished =&gt; {
          console.log(finished);
      }).on(&quot;error&quot;, error =&gt; {
          console.log(error);
      });
  });
  </code></pre>
  <p>background.js</p>
  <pre><code>const sendURL = (request) =&gt; {
      chrome.tabs.sendMessage(request.tabId, {url: request.url}, response =&gt; {
          if( response.status === 'ok' ){
              chrome.webRequest.onCompleted.removeListener( sendURL )
          }
          return true;
      })
  }
  
  chrome.webRequest.onCompleted.addListener( request =&gt; {
      console.log(request);
      sendURL(request)
  },{
      urls: [&quot;https://*.myurl.net/*/*prog_index.m3u8*&quot;],
      types: [&quot;xmlhttprequest&quot;]
  },[&quot;responseHeaders&quot;]);
  </code></pre>