最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

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

programmeradmin2浏览0评论

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

发布评论

评论列表(0)

  1. 暂无评论