Skip to content

Instantly share code, notes, and snippets.

@lamrongol
Created January 29, 2024 10:04
Show Gist options
  • Save lamrongol/4af3afea1ad5803ba1d6a32c3fd7cbff to your computer and use it in GitHub Desktop.
Save lamrongol/4af3afea1ad5803ba1d6a32c3fd7cbff to your computer and use it in GitHub Desktop.
Detect main window(which has most tabs) and sub(second most tabs).
let mainId, subId;
const detectMainWinodows = () => {
chrome.windows.getAll({populate: true, windowTypes: ['normal']}, (windows) => {
if (windows.length == 1) return;
const sortedWindowIdArray = windows.sort((a, b) => b.tabs.length - a.tabs.length).map((win) => win.id);
mainId = sortedWindowIdArray[0];
subId = sortedWindowIdArray[1];
}
)
}
chrome.runtime.onStartup.addListener(detectMainWinodows);
chrome.runtime.onInstalled.addListener((details) => {
if(details.reason == "install") detectMainWinodows();
});
chrome.windows.onCreated.addListener(detectMainWinodows);
chrome.windows.onRemoved.addListener(detectMainWinodows);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment