开发者

Communication from Popup Page to Content Script Page

I am developing an extension and I do not wish to use the options page. I use a browser action (the icon appears on the top-right), and some preference开发者_Go百科s are made trough that page and I store them in localStorage.

However, my content script needs to read that localStorage but I know that it cannot access it. I looked the message passing but could not accomplish what I would wanted.

Here what I have tried:

popup.js

$(document).ready(function(){
    chrome.extension.onRequest.addListener(function(request, sender, sendResponse) {
        if (request.method == "getList")
            sendResponse({status: localStorage['list']});
        else
            sendResponse({}); // snub them.
        });
});

content.js

$(document).ready(function(){
    var p;
    chrome.extension.sendRequest({method: "getList"}, function(response) {
        p = response.status;
        alert(response.status);
    });
});


The popup is ephemeral meaning that the code only lives for the time that the popup is open. This means that your listener will be lost.

Move the code for your listener from your popup.html in to a background page, then this should work fine.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜