Chrome extension - Error closing tab when typing script

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:

Sample output on console

Sorry for my naive photo editing: P

There are a few more things we can deduce from this image:

  1. 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.
  2. 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.
  3. 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.

Sample output number 2

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

+11
javascript google-chrome google-chrome-extension
source share
1 answer

The error message "The tab was closed" bit misleading, since the tab is clearly not closed. In Chrome sources, a variable with a string is called kRendererDestroyed . So the error is that the corresponding renderer is destroyed for some reason.

I get an error if the page opened in the tab is redirected (thus, one renderer is destroyed, the other is created for the same tab, but this time a different URL), in this case the extension will receive tab updates with statuses, such as:

  • loading url: 'example.com', here the tab has already been returned for callbacks, etc., but it will get an error if you try to inject the script
  • loading url: "example.com/other_url"
  • title: 'some title'
  • complete

I managed to get around by injecting the script only after getting status: 'complete' (but probably injection on title will work too)

I have not tried with pdfs, but chrome will probably replace the renderer for those who also look like a redirect. So look more at page statuses and redirecting / replacing renderers. Hope this helps someone stumble upon this question.

0
source share

All Articles