The code is meant to output the current tab object for the page the user is viewing to the console but it just outputs undefined. It\'s run from within a browser action page
May be undefined if called from a non-tab context (for example, a background page or popup view).
It looks like you should use this code not in bg.js but rather in cs.js.
Try:
chrome.tabs.getSelected(null, function(tab){
console.log(tab);
});
Since chrome.tabs is only available in background or popup script and background script is not active in any tab, chrome.tabs.getCurrent() always return undefined.
Instead, we can retrieve the active Tab object from the second argument of any message listener callback. For example,
browser.runtime.onMessage.addListener((message, sender) => {
console.log('Active Tab ID: ', sender.tab.id);
});
The method getSelected() has been deprecated since Google Chrome 16 (but many articles in the official documentation had not yet been updated). Official message is here. To get the tab that is selected in the specified window, use chrome.tabs.query() with the argument {'active': true}. So now it should look like this:
chrome.tabs.query({ currentWindow: true, active: true }, function (tabs) {
console.log(tabs[0]);
});