开发者

How can i sendRequest from background to background with chrome extensions API?

开发者 https://www.devze.com 2023-02-21 06:56 出处:网络
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 the

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,开发者_StackOverflow中文版 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?:)


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消