title: Chrome Extension - Getting "tab was closed" error on injecting a script
body: <p>I am writing a chrome extension which detects the type of file being opened and based on that injects a script on the page which does many other things. Here is the part of my code for the background.js which is injecting the script:</p>
  
  <pre><code>chrome.webRequest.onHeadersReceived.addListener(function(details){
          console.log("Here: " + details.url + " Tab ID: " + details.tabId);
          if(toInject(details))
          {   
              console.log("PDF Detected: " + details.url);
              if(some-condition) 
              {
                  //some code
              }
              else
              {
                  chrome.tabs.executeScript(details.tabId, { file: "contentscript.js", runAt: "document_start"}, function(result){
                      if(chrome.runtime.lastError)
                      {
                          console.log(chrome.runtime.lastError.message + " Tab ID: " + details.tabId);
                      }
                  });
              }
              return {
              responseHeaders: [{
                name: 'X-Content-Type-Options', 
                value: 'nosniff'
              }, 
                  {
                name: 'X-Frame-Options', 
                  /*
                      Deny rendering of the obtained data.
                      Cant use {cancel:true} as we still need the frame to be accessible.
                  */
                value: 'deny'
              }]
            };
          }
  }, {
      urls: ['*://*/*'],
      types: ['main_frame', 'sub_frame']
  }, ['blocking', 'responseHeaders']);
  </code></pre>
  
  <p>Here is the manifest file:</p>
  
  <pre><code>{
      "manifest_version": 2,
  
      "name": "ABCD",
      "description": "ABCD",
      "version": "1.2",
  
      "icons": {
          "16" :  "images/16.png",
          "32" :  "images/32.png",
          "48" :  "images/48.png",
          "128" :  "images/128.png"
      },
  
      "background": {
          "scripts": ["chrome.tabs.executeScriptInFrame.js", "background.js"],
          "persistent": true
      },
  
      "permissions": [
          "webRequest",
          "&lt;all_urls&gt;",
          "webRequestBlocking",
          "tabs",
          "nativeMessaging"
      ],
      "web_accessible_resources": [ "getFrameId", "aux.html", "chrome-extension:/*", "images/*.png", "images/*.gif", "style.css"]
  }
  </code></pre>
  
  <p>The problem is that when injecting script the last error part runs and it shows the tab was closed and the script is not injected. If I press enter on the omnibox a several times the script is injected and things work fine. Here is a sample run of events:</p>
  
  <p><a href="https://i.stack.imgur.com/hw9xL.png" rel="noreferrer"><img src="https://i.stack.imgur.com/hw9xL.png" alt="Sample output on console"></a></p>
  
  <p>Sorry for my naive photo editing :P</p>
  
  <p>There are a few more things we can deduce from this image:</p>
  
  <ol>
  <li>The first thing being loaded in the tab with tab id 86 is something related to my google account. I have logged out and also turned off the prerender feature of chrome.</li>
  <li>On pressing enter several times the tab was closed error goes but the script which maintains a <code>chrome.runtime</code> connection with the background.js gets disconnected.</li>
  <li>And then finally things work fine.</li>
  </ol>
  
  <p>I have been banging my head around this for days. No other question on SO addresses this problem. Nor anywhere else on the internet as well. </p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>One more thing to note: The sample run shown in the image above is one such. There are many different behaviors. Sometimes 3 enters wouldn't make it work. Sometimes just one will. Is there something wrong because of the custom headers i am sending?</p>
  
  <p><strong>UPDATE #1</strong></p>
  
  <p>One must notice the headers I am returning in <code>OnHeadersReceived</code>. It's being done to stop chrome from rendering the document. But on doing that all the data of the file is dumped on the screen and I don't want that to appear. So i think I need <code>document_start</code> so that I can hide the dumped data before my content script does other things like putting a custom UI on the page.</p>
  
  <p><strong>UPDATE #2</strong></p>
  
  <p>Noticed one more thing. If I open a new tab, and then paste a url there and then press enter the following is the output of the background page on the console. </p>
  
  <p><a href="https://i.stack.imgur.com/f86ex.png" rel="noreferrer"><img src="https://i.stack.imgur.com/f86ex.png" alt="Sample output number 2"></a></p>
  
  <p>So I guess, the location of the window is updated at a later time by chrome. Am I right? Any workarounds? </p>