开发者

Returning a value from a content script

When executing a content script from a popup is there a way for that content script to return a value to the popup where the s开发者_运维百科cript was executed.


Referring to Google's Docs, use the following code:

contentscript.js

chrome.runtime.sendMessage({value: "hello"}, null);

popup.html

chrome.runtime.onMessage.addListener(
    function myFunc(request, sender, sendResponse) {
        doStuffWithValue(request.value);
        chrome.runtime.onMessage.removeListener(myFunc); //if you want to stop listening after receiving the message
    });
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜