6

i am using the latest chrome: Version 100.0.4896.60 (Official Build) (64-bit) on win 10 pro

when i install an extension all works fine.

when i close chrome and reopen, the extensions no long work. the extensions tab shows for all extensions: "worker service (inactive)"

after click on the reload button of the extension all is ok.

i also tested it with:

https://github.com/GoogleChrome/chrome-extensions-samples/tree/main/examples/hello-world

to make sure that this could be to some settings, i uninstalled chrome, removed all chrome files and reinstalled it.

the issue persists.

friends of mine do not seem to have this issue with the same chrome version.

any suggestions on how to resolve this ?

here the code:

    "use strict";

async function sendRequest(request, sendResponse) {
  try {
    const startTime = Date.now();
    const response = await fetch(request.url, request.options);
    const time = Date.now() - startTime;
    const body = await response.text();
    const headers = [...response.headers].map((el) => ({
      key: el[0],
      value: el[1],
    }));
    sendResponse({
      status: response.status,
      body,
      headers,
      time,
    });
  } catch (err) {
    sendResponse({
      err: err.message
    });
  }
}

chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
  chrome.storage.sync.get("host", ({ host }) => {
    if (host === sender.tab.url) {
      if (request.type === "send-request") {
        sendRequest(request, sendResponse);
      } else if (request.type === "ping") {
        sendResponse();
      } else {
        console.log("bad request type", request.type);
      }
    } else {
      console.log("host not correct", host, sender.tab.url);
    }
  });
  // NOTE: return value required to keep port open for async response
  return true;
});

chrome.webNavigation.onBeforeNavigate.addListener(() => {
  console.info("service is up 2");
});

chrome.webNavigation.onHistoryStateUpdated.addListener((details) => {
  console.log('service is up');
});
ron
  • 418
  • 4
  • 12
  • The background script runs only when it's activated by some event e.g. in this case it's onInstall. – wOxxOm Apr 03 '22 at 10:25
  • 2
    thanks for the fast reply. the problem is that the background script does not react to events as long as it shows service inactive. chrome.webNavigation.onHistoryStateUpdated.addListener((details) => { console.log('wake me up'); }); it seems that this listener is not triggered when a new web site is opened – ron Apr 03 '22 at 10:50
  • i also tried: chrome.runtime.onInstalled.addListener(() => { chrome.webNavigation.onBeforeNavigate.addListener(() => { console.info("serviceis up 1"); }); }); – ron Apr 03 '22 at 10:53
  • The event listeners must be registered outside of other event listeners, not inside. – wOxxOm Apr 03 '22 at 10:54
  • yes, i tried both inside the onInstall and globaly, no success – ron Apr 03 '22 at 10:56
  • in background.js i have registered multiple listeners. none seem to wake up the service: chrome.runtime.onMessage.addListener, chrome.webNavigation.onBeforeNavigate, chrome.webNavigation.onHistoryStateUpdated. – ron Apr 03 '22 at 10:58
  • [How to see background.js console?](/a/10258029). Also make sure to reload the extension after editing. In case of further problems show your real code in the question, not in comments. – wOxxOm Apr 03 '22 at 10:58
  • thanks. when service is inactive, click on service worker does not open the debugger if the worker is inactive. – ron Apr 03 '22 at 11:00
  • It means the extension is broken, probably due to a bug in Chrome. Uninstall the extension and install it again. – wOxxOm Apr 03 '22 at 11:02
  • thanks, tried install/uninstall 100x where can i report a bug ? – ron Apr 03 '22 at 11:09
  • https://crbug.com. – wOxxOm Apr 03 '22 at 11:11

3 Answers3

4

This is a known bug. You can read more about it here: https://bugs.chromium.org/p/chromium/issues/detail?id=1271154#c52

d-_-b
  • 21,536
  • 40
  • 150
  • 256
0

Make sure you add an event handler for chrome.runtime.onStartup()

I've run into this issue where it always shows as inactive when starting Chrome and it went away with this small change, always starts active.

chrome.runtime.onStartup.addListener( () => {
    console.log(`onStartup()`);
});
0

I got same problem here. Service Worker(inactive). yeah it's came to "inactive" in like 20 secs after you click refresh button on your extension. And I seemed find a way to keep it running by somehow you can click the "Service Worker(inactive)" label then chrome will open a DevTools tab and you could see some logs printed from your background script. it worked well for me.The only point is DevTools tab needs to be opened forever.

connar
  • 1