Created
January 29, 2024 10:04
-
-
Save lamrongol/4af3afea1ad5803ba1d6a32c3fd7cbff to your computer and use it in GitHub Desktop.
Detect main window(which has most tabs) and sub(second most tabs).
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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