[英]chrome.extension.getBackgroundPage() doesn't work with manifest_version: 3
When I invoke a function of backgroud.js in popup.js, if manifest_version is 2, it works;当我在 popup.js 中调用 backgroud.js 的一个函数时,如果 manifest_version 为 2,则它工作; if manifest_version is 3, it doesn't work.
如果 manifest_version 为 3,则不起作用。 Why?
为什么? How can I do with manifest_version: 3
我该如何处理 manifest_version: 3
manifest.json清单文件
//version 2
{
"manifest_version": 2,
......
"background": {
"scripts": ["background.js"]
},
}
//version 3
{
"manifest_version": 3,
......
"background": {
"service_worker": "background.js"
},
}
popup.js弹出窗口.js
var bg = chrome.extension.getBackgroundPage();
var userName = bg.getUserName();
console.log(bg);
background.js背景.js
var userName = "John";
console.log("background");
function getUserName() {
return userName;
};
snapshot in manifest_version 2 enter image description here manifest_version 2 中的快照在此处输入图像描述
snapshot in manifest_version 3 enter image description here manifest_version 3 中的快照在此处输入图像描述
I search a article mentioned "Things to note about a service worker", the following is the third note:搜了一篇“Service Worker的注意事项”的文章,下面是第三个注意事项:
"It's terminated when not in use, and restarted when it's next needed, so you cannot rely on global state within a service worker's onfetch and onmessage handlers. If there is information that you need to persist and reuse across restarts, service workers do have access to the IndexedDB API." “它在不使用时终止,并在下一次需要时重新启动,因此您不能依赖 Service Worker 的 onfetch 和 onmessage 处理程序中的全局状态。如果有需要在重新启动时保留和重用的信息,Service Workers 确实可以访问到 IndexedDB API。”
Above the question case, I guess that background.js(as Service Worker) has terminated when popup.js invoke the function of background.js in Manifest-V3 , so popup.js can't invoke function of background.js(as Service Worker) in Manifest-V3在问题案例之上,我猜当popup.js调用Manifest-V3中background.js的函数时background.js(as Service Worker)已经终止,所以popup.js不能调用background.js(as Service)的函数工人)在 Manifest-V3
https://developers.google.com/web/fundamentals/primers/service-workers https://developers.google.com/web/fundamentals/primers/service-workers
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.