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

发布于 2025-01-10 12:06:34 字数 147 浏览 0 评论 0原文

我正在编写一个扩展,它具有在新选项卡/窗口中打开 URL 的功能。据我所知,它需要用户的许可才能明确允许“弹出窗口”。

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

有没有办法避免这种情况,让用户只允许弹出一次?

I'm writing an extension that has a feature to open a URL in a new tab/window. I understand that it will require the user's permission to explicitly allow a "popup".

The content script of that extension is injected into every website, hence the user is required to give the popup permission on every domain they trigger this action on.

Is there a way to avoid it, so the user has to only allow the popup once?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

婴鹅 2025-01-17 12:06:34

遵循 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]

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

Following the excellent lead of wOxxOm, here's a quick example of how you could manage this. The basic idea is to send a message from the content script (which is injected into every website) to the background script, and from there call the chrome.tabs.create method.

contentScript.js

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

In the background Script, then, you would have to receive the message and open the desired tab.

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) {});
}

That's it! Here are a few links to the official docs:
[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]

I hope I was able to answer your question; if you have any other requests feel free to ask me! This is my first SO answer, so I just hope I didn't break any guidelines ;)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文