diff --git a/src/background.js b/src/background.js index 21901ef..bf992bf 100644 --- a/src/background.js +++ b/src/background.js @@ -14,32 +14,84 @@ let autoLoginTabs = new Set(); -chrome.tabs.onUpdated.addListener((tabId, changeInfo, tab) => { - if (tab.url && tab.status === "complete" && !autoLoginTabs.has(tabId) ) { - const url = tab.url; - - chrome.tabs.sendMessage(tabId, {action: "setManagedAccounts"}, +chrome.tabs.onUpdated.addListener(async (tabId, changeInfo, tab) => { + if (tab.url && tab.status === "complete") { + const [UserLoggedIn, DisableAutoLogin] = await Promise.all([ + isUserLoggedIn(tabId), + isDisableAutoLogin(tabId), + ]); + if (UserLoggedIn && !DisableAutoLogin && !autoLoginTabs.has(tabId)) { + autoLogin(tabId, tab.url); + } + } +}); + +async function isUserLoggedIn(tabId) { + return new Promise((resolve, reject) => { + chrome.tabs.sendMessage( + tabId, + { action: "isUserLoggedIn" }, + function (UserLoggedIn) { + if (chrome.runtime.lastError) { + reject(chrome.runtime.lastError.message); + } else { + resolve(UserLoggedIn); + } + } + ); + }); +} + +function getManagedAccounts(tabId) { + return new Promise((resolve, reject) => { + chrome.tabs.sendMessage( + tabId, + { action: "getManagedAccounts" }, function (managedAccounts) { if (chrome.runtime.lastError) { - console.error("Error sending first message:", chrome.runtime.lastError); - } else if (managedAccounts) { - for (const managedAccount of managedAccounts) { - if (managedAccount && url.includes(managedAccount.signinUrl)) { - autoLoginTabs.add(tabId); - chrome.tabs.sendMessage(tabId, { - action: "autoLogin", - managedAccount: managedAccount, - }); - } - } - } - } - ); - } -}); + reject(chrome.runtime.lastError.message); + } else { + resolve(managedAccounts); + } + } + ); + }); +} + +async function autoLogin(tabId, url) { + const managedAccounts = await getManagedAccounts(tabId); + if (managedAccounts) { + for (const managedAccount of managedAccounts) { + if (managedAccount && url.includes(managedAccount.signinUrl)) { + autoLoginTabs.add(tabId); + chrome.tabs.sendMessage(tabId, { + action: "autoLogin", + managedAccount: managedAccount, + }); + break; + } + } + } +} + +function isDisableAutoLogin(tabId) { + return new Promise((resolve, reject) => { + chrome.tabs.sendMessage( + tabId, + { action: "isDisableAutoLogin" }, + function (DisableAutoLogin) { + if (chrome.runtime.lastError) { + reject(chrome.runtime.lastError); + } else { + resolve(DisableAutoLogin); + } + } + ); + }); +} function clearAutoLoginTabs() { autoLoginTabs.clear(); } -setInterval(clearAutoLoginTabs, 10000); \ No newline at end of file +setInterval(clearAutoLoginTabs, 10000); diff --git a/src/content-script.js b/src/content-script.js index fe89556..7006ac7 100644 --- a/src/content-script.js +++ b/src/content-script.js @@ -98,17 +98,35 @@ window.onload = () => { chrome.runtime.onMessage.addListener((message) => { if (message.action === "autoLogin") { - chrome.storage.sync.get("disableAutoLogin", ({ disableAutoLogin }) => { - if (!disableAutoLogin) { - const managedAccount = message.managedAccount; - autoLogin(managedAccount.username, managedAccount.password); - } - }); + const managedAccount = message.managedAccount; + autoLogin(managedAccount.username, managedAccount.password); + } +}); + +chrome.runtime.onMessage.addListener((message, sender, sendResponse) => { + if (message.action === "isUserLoggedIn") { + chrome.storage.sync.get("accessToken", ({accessToken}) => { + if(accessToken){ + sendResponse(true); + } else{ + sendResponse(false); + } + }); + return true; + } +}); + +chrome.runtime.onMessage.addListener((message, sender, sendResponse) => { + if (message.action === "isDisableAutoLogin") { + chrome.storage.sync.get("disableAutoLogin", ({ disableAutoLogin }) => { + sendResponse(disableAutoLogin); + }); + return true; } }); chrome.runtime.onMessage.addListener(function(message, sender,sendResponse) { - if (message.action === "setManagedAccounts") { + if (message.action === "getManagedAccounts") { chrome.storage.sync.get("accessToken", ({accessToken}) => { sdk.getAccount(accessToken).then((account) => { const managedAccounts = account.data.managedAccounts;