M
M
Martovitskiy2020-12-21 12:57:17
Browser extensions
Martovitskiy, 2020-12-21 12:57:17

How to pass a variable to the active tab in google extensions?

I'm making a chrome plugin.

in background.js I take authorization cookies from my site test4.test.com

chrome.cookies.get({"url": "http://test4.test.com", "name": "site_session"}, function(cookie) {
        console.log(cookie.value);
    });

How now to send the result to the active tab?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alex, 2020-12-21
@Kozack

https://developer.chrome.com/docs/extensions/mv2/m...

N
Nadim Zakirov, 2020-12-21
@zkrvndm

In the background tab do:

chrome.tabs.query({}, function(tabs) {
  for (i = 0; i < tabs.length; i++) {
    chrome.tabs.sendMessage(tabs[i].id, 'Привет мир!');
  }
});

All tabs will receive the message Hello World!
To receive messages on the target tab inside the content script, set the handler:
chrome.extension.onMessage.addListener(function(message) {
  console.log('Получено сообщение: ' + message);
});

Of course, this is just an example, in fact, all this needs to be finished with a file for yourself.
PS If you need to send a message only and exclusively to the active tab, then set the following filter:
chrome.tabs.query({active: true}, function(tabs) {
  for (i = 0; i < tabs.length; i++) {
    chrome.tabs.sendMessage(tabs[i].id, 'Привет мир!');
  }
});

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question