Skip to content

Commit

Permalink
feat: refactor service work and Reduce invalid requests (#21)
Browse files Browse the repository at this point in the history
  • Loading branch information
SamYSF authored Mar 19, 2024
1 parent f067aa2 commit 3f7453b
Show file tree
Hide file tree
Showing 2 changed files with 99 additions and 29 deletions.
96 changes: 74 additions & 22 deletions src/background.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
setInterval(clearAutoLoginTabs, 10000);
32 changes: 25 additions & 7 deletions src/content-script.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 3f7453b

Please sign in to comment.