1

I have a Chrome extension that is using the following code:

chrome.tabs.onUpdated.addListener(
  function(tabId, changeInfo, tab) {
    if (changeInfo.status === 'complete') {
      chrome.tabs.executeScript(null, {file: "js/content.js"});   
    }
  }
);

If I close my browser whilst a webpage is loading, the extension will crash. I believe this is because of the following error:

Unchecked runtime.lastError while running tabs.executeScript: The tab was closed.

Does anyone know how I can avoid this error?

sangerous
  • 45
  • 1
  • 5
  • Well, the easy thing to do is to check `chrome.runtime.lastError` in the callback for `chrome.tabs.executeScript()`. You should be doing this anyway. Doing so will at least move any issues into something else (e.g. it might be that the context is destroyed prior to the callback being called). – Makyen Jan 01 '17 at 23:51
  • @sangerous, Exactly what do you mean by "the extension will crash"? – Pacerier Aug 10 '17 at 04:18
  • Duplicate of https://stackoverflow.com/a/45603880/632951 – Pacerier Aug 10 '17 at 04:21
  • Hi @Pacerier, I tried numerous different routes of suppressing the error and the code would never execute as it was like pulling the plug on a system mid-calculation. I then tracked the bug down to only occurring on Mac OS, a Chrome update fixed this a while ago and I can no longer reproduce. By 'extension will crash' I mean Chrome will completely give up on it and force-disable the extension. – sangerous Aug 11 '17 at 08:21

1 Answers1

2

add a callback to chrome.tabs.executeScript. something like:

chrome.tabs.onUpdated.addListener(
  function(tabId, changeInfo, tab) {
    if (changeInfo.status === 'complete') {
      chrome.tabs.executeScript(null, {file: "js/content.js"},
        function(result) {
          // Process |result| here (or maybe do nothing at all).
        }
      );   
    }
  }
);

you can find documentation on this function in the official Chrome API site: https://developer.chrome.com/extensions/tabs#method-executeScript

Mike Frysinger
  • 2,827
  • 1
  • 21
  • 26
  • The callback itself wouldn't work. You must read the lastError as shown in https://stackoverflow.com/a/45603880/632951 – Pacerier Aug 10 '17 at 04:20