Ramon Villain
Ramon Villain

Reputation: 43

Chrome Extension, send data between tabs

I'm developing a simple extension, but I cannot figure out how to move on. I guess it's a 'simple' question.

The scenario:

I did the first three items, and to the fourth I tried chrome.tabs.query/executeScript/messaging, ajax post… without success.

function sendReport() {
    return function(info, tab) {
        var selectedText = info.selectionText;
        var cr_url = 'http://localhost/cr/index.php';
        var tab = chrome.tabs.create({ url: cr_url }, function(tab){

        });
    }
}

var OgameToConverter = chrome.contextMenus.create({
    "title": "Enviar Relatório",
    "contexts": ["selection"],
    "onclick": sendReport()
});

Upvotes: 3

Views: 6892

Answers (1)

0xcaff
0xcaff

Reputation: 13681

You can send messages between tabs by using chrome.runtime.sendMessage or chrome.tabs.sendMessage. To receiving the message, add an listener for the message on the receiving tab by using the chrome.runtime.onMessage.addListener method.

Example:

chrome.tabs.create({ url: cr_url }, function(tab){
  chrome.tabs.sendMessage(tab.id, {greeting: "hello"}, function(){});
});

In the tab:

chrome.runtime.onMessage.addListener(
  function(message, sender, sendResponse) {
    // do what you want to the message
});

Or, you can create the window with window.open, send the message with window.postMessage and recieve the message by catching the Message event with something like window.addEventListener("message", ...);

Upvotes: 4

Related Questions