Chrome Extension: What is the active tab in 2 opened windows?

前端 未结 2 672
广开言路
广开言路 2021-01-03 08:56

I have an extension that needs to know what URL is on the active tab, but the problem is that when I open a second chrome window there are 2 active tabs, in the webmaster to

相关标签:
2条回答
  • 2021-01-03 09:20

    Make your query to select the last focused Window:

    chrome.tabs.query({ active: true, lastFocusedWindow: true }, function (tabs) {
      //...
    });
    

    Note: Better don't take currentWindow: true because:

    The current window is the window that contains the code that is currently executing. It's important to realize that this can be different from the topmost or focused window.

    Source: http://developer.chrome.com/extensions/windows.html#current-window

    0 讨论(0)
  • 2021-01-03 09:24

    Use chrome.windows.getCurrent() (or .getLastFocused(), right below it) to get the current window, then look for the active tab in the tabs property of the returned window.

    0 讨论(0)
提交回复
热议问题