我如何使用 chrome 扩展 API 从后台发送请求到后台?

发布于 2024-10-30 13:49:35 字数 295 浏览 0 评论 0原文

现在我通过在后台创建 iframe 来实现该解决方案,因此它处理请求,但它的解决方案非常不受信任+过度杀伤,我有大约 400 个文件,每个文件都使用 sendRequest 方法,我无法将它们合并为一个文件,因为它们是通过依赖加载器(Pub Sub 架构)加载的,现在有另一个解决方案,我已经使用 MessageChannel 和 MessageChannel 对其进行了概念验证。 postMessage 在后台,但问题是对于 contentScript 或我定义的前台,我仍然需要使用 chrome API 在后台和前台之间传递消息。

我该如何解决它?:)

Right now i implemented the solution by creating iframes on the background, so it process the request, but its very untrusted solution + overkill, i have something like 400 files that every one of them use sendRequest method, i can't combine them to one file because they load by dependency loader(Pub Sub architecture), now there is another solution that i already made him Proof-of-concept, using MessageChannel & postMessage in the background, but the problem is that for the contentScript or on my definition frontstage, i still need to use chrome API to pass messages between backstage and frontstage.

How do i solve it?:)

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

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

发布评论

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

评论(1

累赘 2024-11-06 13:49:35

我明白你的意思。你有几个js文件,它们都在后台页面,并希望它们互相sendRequest。您可以通过不实际执行 sendRequest 而是调用函数来实现此目的。举个例子可能会更好:

而不是这个:

chrome.extension.onRequest.addListener(function(request, sender, respond)
{
    // bla bla handle request
});

还有这个:

chrome.extension.sendRequest({ message: "hello" }, function(response)
{
    // do something with response
});

你这样做:

function handle_message(request, sender, respond)
{
    // bla bla handle request
}
chrome.extension.onRequest.addListener(handle_message);

还有这个:

handle_message({ message: "hello" }, function(response)
{
    // do something with response
});

当然,不在后台页面上的脚本仍然会执行sendRequests。

I think I know what you mean. You have several js files that are all on the background page, and want them to sendRequest to eachother. You can do this by not actually doing sendRequests, but calling a function. It's probably better with an example:

Instead of this:

chrome.extension.onRequest.addListener(function(request, sender, respond)
{
    // bla bla handle request
});

And this:

chrome.extension.sendRequest({ message: "hello" }, function(response)
{
    // do something with response
});

You do this:

function handle_message(request, sender, respond)
{
    // bla bla handle request
}
chrome.extension.onRequest.addListener(handle_message);

And this:

handle_message({ message: "hello" }, function(response)
{
    // do something with response
});

Of course scripts that are not on the background page will still do sendRequests.

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