Chrome Extensions - onRequest/sendRequest vs onMessage/sendMessage [duplicate]

自古美人都是妖i 提交于 2019-12-20 09:46:18

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!