开发者

jQuery Access Across Chrome Ext. Layers

I'm having a particularly odd issue when developing Chrome extensions using jQuery. I have two "frames" in my extension, the popup page and the background page. Both of these pages have included within them the jQuery library. Essentially what I would like to do is when the user clicks on the browser action icon a loading screen is shown while the background page gathers the data and then sends it back to the popup page. I've tried to do this by having the popup page call a function in the background page and then have the background page use the popup window's dom to access the data and show in the UI. However, I'm noticing that when you get a Window object back from chrome.extension.getView() it does not have access to the jQuery library for some r开发者_JAVA百科eason. Any call to '$' is met with a 'method not found' error. So what would be the best way to handle this?


I would do this using message parsing. Essentially, you add a listener function in your background page, similar to this: (slightly simplified example from the documentation page)

chrome.extension.onRequest.addListener(
    function(request, sender, sendResponse) 
    {
        console.log("Received a request!");
        if (request.message == "hello")
            sendResponse({message: "goodbye"});
    });

The background page fetches the information, and returns it to the popup using the sendResponse function.

Then in your popup, you implement something like this:

chrome.extension.sendRequest({message: "hello"}, function(response) {
    console.log(response.message);
});

Messages (the {message: "hello"} parts) can be any JSON compatible object.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜