I am working on a small chrome extension and trying to redirect a user on certain URLs (using onBeforeWebRequest) to my html page for them to enter a password (saved on storage) and if password is OK I want to redirect them to their original URL.
I am saving the user's original URL in storage.local and pull it after they enter the correct password.
My problem is that when I try to redirect from my page to the user's original URL, the onBeforeWebRequest listener kicks in and redirects again (to the password page).
I thought about redirecting if a certain flag is on, but I think it won't work if the user opens a few tabs and tries to go to the listed URLs.
Code:
background.js:
chrome.webRequest.onBeforeRequest.addListener(details => {
var firstLogin;
chrome.storage.local.set({'redirect-to': details.url});
chrome.storage.sync.get('firstLogin', (value) => {
firstLogin = value['firstLogin'];
});
if (firstLogin == false) {
return {
redirectUrl: chrome.runtime.getURL('html/redirect.html')
}
}
else {
return {
redirectUrl: chrome.runtime.getURL('html/options.html')
}
}
}, {
urls: ["http://www.gmail.com/*"]
}, ['blocking']
);
redirect.js:
redirectBtn.addEventListener('click', () => {
chrome.storage.local.get('redirect-to', (value) => {
location.href = value['redirect-to'];
});
});