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
});
精彩评论