简体   繁体   English

chrome.extension.getBackgroundPage() 不适用于 manifest_version: 3

[英]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.

相关问题 chrome.extension.getBackgroundPage() 函数示例 - chrome.extension.getBackgroundPage() function example chrome.extension.getBackgroundPage在iframe的扩展页面中未定义 - chrome.extension.getBackgroundPage is undefined in an extension page in an iframe 创建实际弹出窗口时无法访问 chrome.extension.getBackgroundPage() - Can't access chrome.extension.getBackgroundPage() when creating actual popup 使用chrome.extension.getBackgroundPage()播放音频文件 - Use chrome.extension.getBackgroundPage() to play an audio file chrome.extension.getBackgroundPage()在一段时间后返回null - chrome.extension.getBackgroundPage() returns null after awhile 地理位置权限不适用于 Manifest 版本 3 Chrome 扩展 - geolocation permission doesn't work with Manifest Version 3 Chrome Extension manifest_version:2 的边缘扩展不起作用 - Edge extension with manifest_version:2 not works 将Chrome扩展程序更改为manifest_version 2需要做些什么? 我的扩展程序只是在弹出窗口中打开一个URL。 - What needs to be done for changing a Chrome Extension to manifest_version 2? My extension simply opens a url in a popup. chrome.extension.getBackgroundPage()。VARIABLE在使用打字稿时未定义,但可与javascript一起正常使用 - chrome.extension.getBackgroundPage().VARIABLE is undefined when using typescript, but works fine with javascript browserify + Chrome扩展程序的getBackgroundPage() - browserify + chrome extension's getBackgroundPage()
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM