I am writing a Chrome extension that determines the type of file to open and based on this, inserts a script into a page that does many other things. Here is the part of my code for background.js that implements the script:
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']);
Here is the manifest file:
{ "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", "<all_urls>", "webRequestBlocking", "tabs", "nativeMessaging" ], "web_accessible_resources": [ "getFrameId", "aux.html", "chrome-extension:/*", "images/*.png", "images/*.gif", "style.css"] }
The problem is that when you inject the script, the last part of the error is executed, which indicates that the tab has been closed and the script has not been implemented. If I press the ENTER key in the omnibox several times, the script is entered and everything works fine. Here is a sample series of events:

Sorry for my naive photo editing: P
There are a few more things we can deduce from this image:
- The first thing that loads into the tab with id 86 is something related to my Google account. I logged out and also disabled the prerender feature in Chrome.
- When you press the enter key several times, the tab closes, the error disappears, but the script that supports connecting
chrome.runtime to background.js was turned off. - And finally, everything works fine.
I banged my head about this for several days. No other SO question solves this problem. And nowhere on the Internet.
EDIT:
One more thing to pay attention to: the run example shown in the figure above is one such example. There are many different behaviors. Sometimes 3 inputs will not make it work. Sometimes only one will be. Is there something wrong with the custom headers I'm posting?
UPDATE No. 1
You need to notice the headers that I return to OnHeadersReceived . This is to ensure that Chrome does not display the document. But after that, all the file data is reset to the screen, and I do not want this to appear. Therefore, I think I need document_start so that I can hide the downloaded data before my content script does other things, such as placing the user interface on the page.
UPDATE No. 2
I noticed one more thing. If I open a new tab, and then paste the URL there and then press the enter key, the background page on the console is displayed below.

So, I think the window location is updated later than Chrome. I'm right? Are there any workarounds?