iMath
iMath

Reputation: 2478

how to get reference to current tab of current window?

I am a newbie for Google Chrome Extensions development ,I wonder how to get a reference to the current tab of current window .

I used chrome.tabs.query({'active': true} but it doesn't work when multiple windows opened .

Upvotes: 4

Views: 3404

Answers (1)

Chris McFarland
Chris McFarland

Reputation: 6169

Every window that has a tab has one active tab, so if there are multiple windows open, you need to specify which window you want.

To get the window that the current script is calling from, use:

chrome.tabs.query({ active: true, windowId: chrome.windows.WINDOW_ID_CURRENT }, function (tabs) {
    // Do something with tabs
});

If, however, by "current window", you mean the front-most focused window shown to the user, use:

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

For further info, see chrome.tabs.query and Chrome's definition of current window.

Upvotes: 8

Related Questions