N
N
Nadim Zakirov2020-01-27 15:24:45
JavaScript
Nadim Zakirov, 2020-01-27 15:24:45

How to pass a message from the extension's popup window to all running content scripts?

Hello, there is no way to send a message from the extension popup window to content scripts on open sites.

To receive messages, I use the chrome.extension.onMessage.addListener()
handler. To send messages, I use chrome.extension.sendMessage()

At the same time, the operation of these interfaces makes me misunderstand:
1. The message is correctly transmitted from the content script to background.js
2 From background.js to the popup-window of the extension, the message also normally leaves
3. From the popup-window of the extension to background.js, there are no problems with passing either

However, when I try to pass the message exactly to the content script, I get nothing! How so?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Nadim Zakirov, 2020-01-27
@zkrvndm

Everything, I figured it out. It turned out that to send a message to the content script, you need to use another method:

chrome.tabs.query({}, function(tabs) {
    for (var i=0; i<tabs.length; i++) {
        chrome.tabs.sendMessage(tabs[i].id, "наше сообщение");
    }
});

Receiving a message via chrome.extension.onMessage.addListener() works correctly.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question