Chrome扩展程序:获取引用标签

时间:2014-04-30 07:05:55

标签: javascript google-chrome google-chrome-extension

我如何知道是否通过其他标签页上的链接或内容脚本打开了标签?
如果我可以,我也需要该选项卡的信息。

chrome.tabs.onCreated.addListener(function(id, info, tab){
      //  tab doesn't contain any such info
});

1 个答案:

答案 0 :(得分:4)

您的回调格式错误。

根据documentation,回调需要一个Tab参数,而不是三个。

chrome.tabs.onCreated.addListener(function(tab){
  // You now have things like tab.id exposed
  // If you have "tabs" permission, also things like tab.url

  // You also have tab.openerTabId for "referrer" tab

  // If you have host permissions for the tab:
  chrome.tabs.executeScript(
    tab.id,
    { code: "document.referrer;" },
    function(result) {
      // Here, you have the "real" referrer,
      // which would be empty for tabs opened via `chrome.tabs`
    }
  );
});