chrome.extension.onMessage is undefined

I have a simple plugin that just does something like this:

chrome.extension.onMessage.addListener(function(msg, _, sendResponse) {
    log("Got message from background page: " + msg);
});

unfortunately when my panel is loaded the following error is shown:

TypeError: Cannot call method 'addListener' of undefined

and according to my tests chrome.extension.onMessage is undefined

According to this page http://code.google.com/chrome/extensions/messaging.html I should be able to access this chrome API from my page so it has to be something small that I am missing here...


Please note methods chrome.extension.onRequest and chrome.extension.sendRequest , as originally suggested in this answer, are deprecated as of Chrome 33.


You should use

chrome.extension.onRequest

instead of

chrome.extension.onMessage

And in background page or any other extension scripts:

chrome.tabs.sendRequest

instead of

chrome.tabs.sendMessage

( the documentation is outdated... alert to google team ;) )


Just a side note: the Yandex browser (mostly oriented for Russians) which is also based on Chromium still (as of 11/10/2012, ver. 1.0) has the .*Request methods instead of .*Message. Many thanks to Ciprian Amariei for the tip, it saved me a lot of time!

PS: This should actually be a comment to Ciprian Amariei's answer but unfortunately I can't leave comments yet and I though this information could be very helpful to those who develop extensions for Yandex browser.


Make sure you're using the latest Google Chrome version. Older versions don't have the chrome.extension.onMessage API.

链接地址: http://www.djcxy.com/p/10932.html

上一篇: 如何将UserControl添加到WPF窗口上的Panel

下一篇: chrome.extension.onMessage未定义