Skip to content

Instantly share code, notes, and snippets.

@olegp
Created September 20, 2018 20:31
Show Gist options
  • Save olegp/a32dcf2c07603b2aafcb6e462c044f10 to your computer and use it in GitHub Desktop.
Save olegp/a32dcf2c07603b2aafcb6e462c044f10 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