Chrome extension: Get current tab from popup

chrome.tabs.getSelected is deprecated. You should use chrome.tabs.query instead.

chrome.tabs.query requires two parameters: a query object and a callback function that takes the array of resulting tabs as a parameter.

You can get the “current tab” by querying for all tabs which are currently active and are in the current window.

var query = { active: true, currentWindow: true };

Since the query will return a Tab array containing the current tab alone, be sure to take the first element in the callback

function callback(tabs) {
  var currentTab = tabs[0]; // there will be only one in this array
  console.log(currentTab); // also has properties like

Et voilĂ :

chrome.tabs.query(query, callback);

Leave a Comment