Skip to content

Instantly share code, notes, and snippets.

@grim-reapper
Forked from olegp/background.js
Created August 11, 2021 04:59
Show Gist options
  • Select an option

  • Save grim-reapper/ca68629efb578f5203518e7cfbf97411 to your computer and use it in GitHub Desktop.

Select an option

Save grim-reapper/ca68629efb578f5203518e7cfbf97411 to your computer and use it in GitHub Desktop.
Active tab change detection in Chrome Extension
let activeTabId, lastUrl, lastTitle;
function getTabInfo(tabId) {
chrome.tabs.get(tabId, function(tab) {
if(lastUrl != tab.url || lastTitle != tab.title)
console.log(lastUrl = tab.url, lastTitle = tab.title);
});
}
chrome.tabs.onActivated.addListener(function(activeInfo) {
getTabInfo(activeTabId = activeInfo.tabId);
});
chrome.tabs.onUpdated.addListener(function(tabId, changeInfo, tab) {
if(activeTabId == tabId) {
getTabInfo(tabId);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment