javascript - Chrome Extension, send data between tabs - Stack Overflow

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

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

The scenario:

  • The user select an text
  • Opens the contextual menu and hit the extension
  • A new tab will open
  • Fill a textarea w/ the selected/highlighted text

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()
});

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

The scenario:

  • The user select an text
  • Opens the contextual menu and hit the extension
  • A new tab will open
  • Fill a textarea w/ the selected/highlighted text

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()
});
Share Improve this question edited Nov 23, 2014 at 2:04 Ramon Villain asked Nov 23, 2014 at 1:59 Ramon VillainRamon Villain 431 silver badge4 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 4

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", ...);

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745320186a4622408.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信