我正在编写一个共享的 webextension 脚本,它应该能够在扩展程序的后台页面、上下文脚本和弹出页面中执行,但它需要知道它在哪里才能在任何地方正常工作。
为此,脚本需要一种方法来找出当前正在运行的上下文(背景、弹出窗口、上下文脚本)以进行相应处理。有没有办法/JS API 来获取上下文?
我查看了标准的 webextension API(例如 chrome.runtime),但找不到任何相关内容。
我的想象是这样的:
const context = chrome.runtime.getCurrentContext();
if (context === "popup") {
// Running inside popup...
} else if (context === "content") {
// Running as a content script on a page
} else if (context === "background") {
// Running in the background page of the extension
}