6

I have had recent reports of a chrome extension that I develop that stops working after an update or a fresh install. The background script seems to not start at all.

  • There is no response to messages sent to it from the content scripts.
  • There is no process for it in the task manager.
  • Opening background page from chrome://extensions does not show any activity in the console, or show any source files.
  • Profiling, memory snapshot buttons are disabled.

Once this issue appears, it persists for the chrome profile even after reloading or uninstalling/reinstalling the extension.

Restarting chrome resolves the problem.

The issue has been seen on chrome v79. But I cannot say for sure that it is exclusive to this version, as the issue is difficult to reproduce and seemingly random.

Has anyone seen such an issue, or has any ideas what to look for? I am happy to update my question with any new info I have or with any info you need.

Edit:

Here is my webNavigation listener, which is used to inject content scripts. This handler is wired up in the 'root' context of the background script (not asynchronously inside an event handler)

chrome.webNavigation.onCompleted.addListener((details) ⇒ { 
   if(details.frameId === 0) { 
     injectScript( 
       'js/contentScript.js', 
       details.tabId, 
       details.frameId, 
       details.url 
     ).catch((e) ⇒ {}); 
   }
}

The injectScript function is as follows

export const injectScript = ƒ (scriptPath,tab,frame,tabUrl) { 
  return new Promise((res,rej) ⇒ {  
    let options = { 
      file : scriptPath, 
      allFrames : false, 
      frameId : frame, 
      matchAboutBlank: false, 
      runAt : 'document_idle', 
    }; 
    const cb = ƒ () { 
      if (chrome.runtime.lastError) { 
        let err = new Error('Could not inject script'); 
        capture(err,{ 
          ...options, 
          tabUrl, 
          lastError : chrome.runtime.lastError.message, 
        }); 
        rej(err); 
      }else{ 
        res(); 
      } 
    }; 
    if (tabUrl.indexOf('.salesforce.com') !== -1) { 
      window.setTimeout(() => { 
        chrome.tabs.executeScript(tab,options,cb); 
      },500); 
    }else{ 
      chrome.tabs.executeScript(tab,options,cb); 
    } 
  }); 
};

Note above, the capture function reports the error to a backend and I cannot see it being reported there as well. Cannot add a breakpoint in code because no source appears in the background page, as noted above.

Hammad Akhwand
  • 779
  • 1
  • 7
  • 19
  • I guess your extension doesn't inject the content scripts manually which is needed in Chrome on update/install ([example](/a/11598753)). Note that without [MCVE](/help/mcve) we have to guess which is not an effective method of help. – wOxxOm Jan 01 '20 at 10:11
  • @wOxxOm what you say is true. However, that should be resolved once the tab is refreshed so that the content script injects. Not so, in this case. Also, I inject content scripts via code using `executeScript` which happens in the `webNavigation.onCompleted` event. Since the background script doesn't run at all, this doesn't happen. – Hammad Akhwand Jan 01 '20 at 10:16
  • Unfortunately, that's the nature of the beast here. The issue is seemingly random, and I am looking for hints which can lead to an MCVE, which would be helpful even if it is actually a bug in Chrome. – Hammad Akhwand Jan 01 '20 at 10:21
  • If webNavigation listener doesn't fire it's definitely a bug, assuming you registered it correctly. – wOxxOm Jan 01 '20 at 10:23
  • Another possibility is an infinite loop in your background script which should be seen in the OS task manager consuming 100% CPU. In Windows task manager you would need to switch to the advanced view (or Processes tab). As for Chrome's task manager, I don't really trust it. – wOxxOm Jan 01 '20 at 10:31
  • I have added code to show how I've set up the script injection. I don't see high cpu usage in windows task manager (and my cpu fan etc remains normal). It also does not have any impact on the other tabs or other apps I am running on my PC. – Hammad Akhwand Jan 01 '20 at 10:40
  • I really think this is a bug, because people are having not same but similar problems with `webNavigation` , you can track [webRequest Bugs/Chrome](https://bugs.chromium.org/p/chromium/issues/list?q=webrequest&can=2) or file a new issue once you are able to reproduce the error. – keser Jan 09 '20 at 07:41

1 Answers1

0

Doc

A background service worker is loaded when it is needed, and unloaded when it goes idle.

https://developer.chrome.com/docs/extensions/mv3/service_workers/

You can use the following methods:

// Keep heartbeat
let heartTimer;
const keepAlive = () => {
    heartTimer && clearTimeout(heartTimer);
    heartTimer = setTimeout(() => {
        chrome.tabs.query({ active: true, currentWindow: true }, function (tabs) {
            console.info('[heartbeat]')
            tabs.length && chrome.tabs.sendMessage(
                tabs[0].id,
                { action: "heartbeat" }
            );
        });
        keepAlive();
    }, 10000);
};
keepAlive();
BottleLiu
  • 39
  • 8