Messaging between background and content scripts

Пытаюсь связь между слоями расширения сделать. Но отчаянно не хочет у меня работать. Сделал простой сетап:

по нажатию на иконку расширения в браузере, юзаю chrome.runtime.sendMessage, но контент скрипт не реагирует на него( Есть у кого-то идеи почему? Что я упустил?)

Типа если бросать сообщения из content скрипта в background, то все ок)

сhrome.runtime.sendMessage('Hello from content');`

и так его слушать:

chrome.runtime.onMessage.addListener(function (request, sender, sendResponse) {
    console.log(request);
});

так заводится, а наоборот ни в какую

There are 2 sendMessage functions in Chrome API.

So, to send a message TO a content script, you need to use chrome.tabs . To send a message FROM a content script (or within the extension pages), you need to use chrome.runtime .

The event is chrome.runtime.onMessage in both cases.

See Messaging docs for more details.

1 лайк