开发者

Chrome extension development: Call a method in popup from background_html

开发者 https://www.devze.com 2023-03-03 23:50 出处:网络
I have a开发者_开发百科 popup named mf_popup.html and my background page is named mf_background.html.

I have a开发者_开发百科 popup named mf_popup.html and my background page is named mf_background.html.

As I start the browser my background page fires (as is my understanding, I am new to Chrome development), and I would like to call a function in my mf_popup.html page from my background page.

For example:

Start chrome

background page fires

background page calls some function in popup page (which initiates some stuff)

How do I do the above?


if you place the needed code in both html-files in mf_javascript.js and includes the script in both with this line:

<script type="text/javascript" src="mf_javascript.js">


mf_popup.html

//sendRequest([any type] request, [function] responseCallback)
chrome.extension.sendRequest({
    function: "foo", 
    params: [myParam1, myParam2],
    function(response) {
      alert("foo returns:"+response.result+");
    }
});

mf_background.html

chrome.extension.onRequest.addListener(
    function(request, sender, sendResponse) {
        if(request.function == "foo") 
            var bar = foo(request.params[0], request.params[1]);
        sendResponse({result: bar});
    }
);

You also could just use sendRequest("foo") if you don't want send any params and/or use an callback function.

0

精彩评论

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