问题
Checking out this sample extension linked by a page in the Chrome Extension center, I see they used
chrome.extension.onRequest.addListener(onRequest);
in the background.js page in order to listen to the contentscript.js and
chrome.extension.sendRequest({}, function(response) {});
in the contentscript.js in order to talk to the background.js page.
But I can't find the documentation for these functions anywhere in the web and Google's Message Passing guide only mentions
chrome.extension.sendMessage(...)
to send, and
chrome.extension.onMessage.addListener(...)
to listen.
Which functions should I use? Is sendRequest/onRequest obsolete? Is the Google's dev guide still up-to-date?
回答1:
It seems sendMessage
is favored over sendRequest
, which is to be deprecated: http://codereview.chromium.org/9965005/
回答2:
Also note the change in API path from
- chrome.extension.onRequest
- chrome.extension.sendRequest
to
- chrome.runtime.onMessage
- chrome.runtime.sendMessage
will save you getting frustrated over why e.g. chrome.extension.onMessage is not working!
来源:https://stackoverflow.com/questions/11335815/chrome-extensions-onrequest-sendrequest-vs-onmessage-sendmessage