Chrome 扩展 – 来自同一来源的弹出窗口?

Chrome extension – popup from the same source?

我正在编写一个扩展,它具有在新的 tab/window 中打开 URL 的功能。我知道它需要用户的许可才能明确允许“弹出”。

该扩展的内容脚本被注入每个网站,因此用户需要在他们触发此操作的每个域上授予弹出权限。

有没有办法避免它,让用户只允许弹出一次?

在 wOxxOm 的出色领导下,这里有一个简单的示例来说明如何管理它。基本思想是从内容脚本(注入每个网站)向后台脚本发送一条消息,然后从那里调用 chrome.tabs.create 方法。

contentScript.js

chrome.runtime.sendMessage({message: "openNewTab", urlToOpen: "https://www.example.com/"}, function (response) {
    
});

然后,在后台脚本中,您必须接收消息并打开所需的选项卡。

chrome.runtime.onMessage.addListener( // this is the message listener
    function(request, sender, sendResponse) {
        if (request.message === "openNewTab")
            openTabFromBackground(request.urlToOpen);
    }
);

async function openTabFromBackground(url){
    chrome.tabs.create({url: url}, function (tab) {});
}

就是这样!以下是一些官方文档的链接: [https://developer.chrome.com/docs/extensions/mv3/messaging/][1] [https://developer.chrome.com/docs/extensions/reference/tabs/#opening-an-extension-page-in-a-new-tab][2]

希望我能够回答你的问题;如果您有任何其他要求,请随时问我!这是我的第一个 SO 答案,所以我只希望我没有违反任何准则 ;)