Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 .

like image 887
iMath Avatar asked Feb 16 '23 15:02

iMath


1 Answers

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.

like image 107
Chris McFarland Avatar answered Feb 18 '23 12:02

Chris McFarland