0

appAPI.tabs.getActive 在 Firefox 中返回空对象

appAPI.ready(function() 
{ 
    // retrieves the information for the active tab 
    appAPI.tabs.getActive(function(tabInfo) { 
        console.log( 
           'tabId: ' + tabInfo.tabId + 
           ' tabUrl: ' + tabInfo.tabUrl 
        ); 
     }); 
}); 

我在我的扩展中尝试了上面的函数/代码 appAPI.tabs.getActive,它在 Chrome 中正常工作,但在 Firefox 中不工作,它给了我空对象 {}。如果有人知道问题是什么,请尽快回复,提前谢谢

4

1 回答 1

1

根据经验,只有在后台范围以外的范围内使用appAPI.tabs API时才会发生这种情况。请注意,它仅在后台范围内受支持。

要在其他范围内使用appAPI.tabs.getActive,从范围向后台范围发送消息以获取 tabInfo 对象,然后将数据发送回原始范围,类似于以下弹出范围中的示例:

popup.html

function crossriderMain($) {
  var tabInfo = null;
  appAPI.message.addListener(function(msg) {
    if (msg.type==='set-tabInfo') {
      tabInfo = msg.tabInfo;
    }
  });
  appAPI.message.toBackground({type:'get-tabInfo'});
}

背景.js

appAPI.ready(function() {
  appAPI.message.addListener(function(msg) {
    if (msg.type==='get-tabInfo') {
      appAPI.tabs.getActive(function(tabInfo) {
        appAPI.message.toPopup({type:'set-tabInfo', tabInfo:tabInfo});
      });
    }
  });
});
于 2013-10-31T09:50:12.633 回答