Why doesn't chrome.tabs.query() return the tab's URL when called using RequireJS in a Chrome extension?

后端 未结 2 1305
暗喜
暗喜 2020-12-24 14:24

I have a simple Chrome extension that adds a browser action. When the extension\'s popup is opened, it needs to access the current tab\'s URL. Since it doesn\'t need acces

2条回答
  •  梦毁少年i
    2020-12-24 15:10

    To overcome the devTools bug that Rob W. reported in his post, the following getActiveTab workaround seems to consistently work for me (even when there are multiple devTools windows open). It works by always saving a reference to the activeTabId in the background page, whenever the tabs.onActivated event fires.

    var activeTabId;
    
    chrome.tabs.onActivated.addListener(function(activeInfo) {
      activeTabId = activeInfo.tabId;
    });
    
    function getActiveTab(callback) {
      chrome.tabs.query({ currentWindow: true, active: true }, function (tabs) {
        var tab = tabs[0];
    
        if (tab) {
          callback(tab);
        } else {
          chrome.tabs.get(activeTabId, function (tab) {
            if (tab) {
              callback(tab);
            } else {
              console.log('No active tab identified.');
            }
          });
    
        }
      });
    }
    

提交回复
热议问题