M
M
MikUrrey2021-10-25 23:52:52
JavaScript
MikUrrey, 2021-10-25 23:52:52

Does Chrome extension worker either work or not?

Hello!
The extension should respond to certain XHR requests, but for some reason it can suddenly fall off, and resumes work if you start the worker's debug console. While the console is running, it works without failures.
If the console is turned off, the extension may fall off again.
extract from the manifest:

"manifest_version": 3,
  "background": {
    "service_worker": "background.js"
  },
  "permissions": [
    "storage", 
    "scripting", 
    "webRequest",
    "webRequestBlocking",
    "tabs"
  ],
  "host_permissions": ["https://secret"],
  "content_scripts": [
    {
      "matches": ["<all_urls>"],
      "js": ["content-script.js"]
    }
  ],

background.js (monitors requests and kicks content-script.js)
chrome.webRequest.onCompleted.addListener(function (details) {
    console.log({details})
    chrome.tabs.query({active: true, currentWindow: true}, function(tabs) {
      console.log({tabs})
      if (tabs[0]) {
        chrome.tabs.sendMessage(tabs[0].id, {details}, function(response) {
          console.log({response});
        });
      }
    });
}, {urls: ['https://secret/?*']});

content-script.js (if there was a request, it should display the object in the console, but it does not always do this)
chrome.runtime.onMessage.addListener(
    function(request, sender, sendResponse) {
        console.log(request);
        sendResponse({});
    }
);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
dollar, 2021-10-26
@dollar

Make background.js like this:

try {
  importScripts("bg.js");
} catch (e) {
  console.error(e);
}

But bg.js is already a regular js file, transfer all background handlers there, and it will be like on the 2nd version of the manifest.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question