I have created an extension that alternates popups based on if the user is logged in a specific website. I used chrome.tabs.onHighlighted and chrome.tabs.onActivated to check in the background page if the user is still logged in, but the change would only happen after I click 2 times on different tabs to change the highlited one (I'm using a promise in there so that might be the reason for the need of an extra click). Is there any other way to force the background page to always be aware? Thank you
chrome.tabs.onHighlighted.addListener(function (evt) {
var token = new Promise(function (resolve, reject) {
try {
// Get the current tab
chrome.tabs.query({ active: false }, function (tabs) {
var t = JSON.stringify(tabs);
var tabs = JSON.parse(t);
let found_tab = [];
for (let i = 0; i < tabs.length; i++) {
if (tabs[i].url.includes("https://example.xyz")) {
found_tab = tabs[i];
}
}
// Execute script in the current tab
chrome.tabs.executeScript(
found_tab.id,
{ code: ` JSON.parse( localStorage.getItem('userCredentials' ))` },
function (result) {
if (
result == null ||
result == undefined ||
result[0] == null ||
result[0] == undefined
)
reject(null);
resolve(result);
}
);
});
} catch (err) {
// Log exceptions
reject(err);
}
});
function getAuthToken() {
token.then(function (value) {
auth_token = value[0].auth_token;
username = value[0].user_username;
});
}
getAuthToken();
if (auth_token === undefined || auth_token === null) {
chrome.browserAction.setPopup({
popup: "login.html",
});
} else {
chrome.browserAction.setPopup({
popup: "popup.html",
});
}
fetch("https://api.example.xyz/token/user/" + username)
.then(handleResponse)
.then((response) => {
if (JSON.parse(JSON.stringify(response.tokens)).includes(auth_token)) {
console.log(auth_token);
} else {
auth_token = null;
}
});
});