service worker 不在 chrome-extension 的导航器中

service worker is not in navigator for chrome-extension

我正在使用 manifest v3 开发 chrome 扩展。我尝试使用以下代码在许多资源中注册服务工作者,我发现

[Service Worker Registration] Registered extension scheme to allow service workers
Service Workers require a secure origin, such as HTTPS. 
chrome-extension:// pages are not HTTP/HTTPS, but are secure so this change 
becomes a necessary step to allow extensions to register a Service Worker.

"chrome-extension" is added as a scheme allowing service workers.

但是在使用下面的代码之后,service worker 不工作,background.js 文件因此不工作。

范围URL:

chrome-extension://ifcfmjiflkcjbbddpbeccmkjhhimbphj/trigger/trigger.html

serviceWorker.js

self.addEventListener('load', function () {
    navigator.serviceWorker.register('./background.js').then(function (registration) {
        // Registration was successful
        console.log('ServiceWorker registration successful with scope: ', registration.scope);
    }, function (err) {
        // registration failed :(
        console.log('ServiceWorker registration failed: ', err);
    });
});

self.addEventListener('install', (event) => {
    event.waitUntil(
        caches.open('v1').then((cache) => {
            return cache.addAll([
                
            ]);
        })
    );
});

self.addEventListener('fetch', (event) => {
    event.respondWith(
        caches.match(event.request).then((resp) => {
            return resp || fetch(event.request).then((response) => {
                return caches.open('v1').then((cache) => {
                    // cache.put(event.request, response.clone());
                    return response;
                });
            });
        })
    );
});

self.addEventListener('activate', (event) => {
    var cacheKeeplist = ['v2'];

    event.waitUntil(
        caches.keys().then((keyList) => {
            return Promise.all(keyList.map((key) => {
                if (cacheKeeplist.indexOf(key) === -1) {
                    return caches.delete(key);
                }
            }));
        })
    );
});

manifest.json

{
  "manifest_version": 3,

  "name": "TestExtension",
  "description": "This extension is for testing purpose only",
  "version": "0.0.1",
  "background":{
    "service_worker":"./serviceWorker.js"
  },
  "declarative_net_request": {
    "rule_resources": [{
      "id": "ruleset_1",
      "enabled": true,
      "path": "rules.json"
    }]
  },  
  "permissions":["storage", "tabs", "activeTab", "scripting", "declarativeNetRequest", "declarativeNetRequestFeedback","browsingData","contextMenus"],  
  "host_permissions":["<all_urls>"],
  "web_accessible_resources": [
    {
      "resources": [ "css/notification.css" ],
      "matches": [ "<all_urls>" ]
    }
  ],
  "content_scripts":[
    {
      "js":["content-scripts/content.js"],
      "matches":["<all_urls>"],
      "all_frames":true,
      "exclude_matches":["*://extensions/*"],
      "run_at":"document_end"
    }
  ],
  "icons":{
    "16":"assets/baby-logo-black-icon.png",
    "48":"assets/baby-logo-black-icon.png",
    "128":"assets/baby-logo-black-icon.png"
  },
  "action":{
    "default_title":"TestExtension",
    "default_popup":"trigger/trigger.html"
  }
}

任何人都请帮助我并指导我是否遗漏了 serviceWorker.js 或 manifest.json 中的任何内容?或者在这方面犯了什么错误?请帮助我使 serviceWorker.js 和 background.js 正常工作。

注意:使用上面的代码,我在 serviceWorker.js 上没有收到任何错误,它也不起作用。

不需要注册任何东西。
当您安装扩展程序时,浏览器会自动执行此操作。

  1. 删除注册码和serviceWorker.js。
  2. 在manifest.json
  3. 中使用"service_worker": "background.js"