0

I am trying to create an extension where: from clicking a button on chrome it will launch a popup window and from that popup window I would like to get information from the initial tab which was focused on the time I've pressed the button.

function onClick(tab) {
    var originalTab = tab,
        popupTab;
    chrome.windows.create(
        {
            url: chrome.extension.getURL('popup.html'),
            type: 'popup'
        },
        function (w) {
            w.getCurrent({populate: true}, function (win) {
                var popupTab = win.tabs[0];
                popupTab.insertCSS(null, {code: 'body {background:red;}'});
                //look now I have access to the original and popup tabs!!!
            });
        }
    );
}

chrome.browserAction.onClicked.addListener(onClick);

By using the following script, nothing really happens.

Where I am creating another window after the button is clicked and when the window is created I am getting the first tab available in it. In this situation I would have access to both originalTab and popupTab instance, but unfortunately this is not what is happening.

Is there particular reason for this script not work as expected?

Thanks in advance

4

1 回答 1

0

我不确定你的代码有什么特别的问题,但我是这样做的。

弹出窗口在清单中指定。这通常可能并不重要,但如果事件处理程序唯一应该做的事情就是打开弹出窗口,这似乎是一种更可取的方法。情况就是这样。

对于弹出 onload 事件,有一个类似的处理程序:

function onLoad()
{
  chrome.windows.getCurrent(
  function(win)
  { 
    chrome.tabs.query({'windowId': win.id, 'active': true},
    function(tabArray)
    {
      // tabArray[0] - is the initially selected tab, so you may do...
      chrome.tabs.insertCSS(tabArray[0].id, {code: 'body {background:red;}'});
    }
  }
}
于 2012-11-20T20:23:35.037 回答