0

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'];
    });
});
Snirco
  • 1
  • 2
  • possible duplicate of https://stackoverflow.com/questions/12065029/redirecting-url-in-a-chrome-extension – ReyAnthonyRenacia Dec 07 '17 at 09:52
  • @noogui I read through this post, and my onBeforeRequest function is working as expected (too well unfortunately), but my problem is redirecting back to where the user ORIGINALLY tried to navigate to. any help? – Snirco Dec 07 '17 at 16:09

0 Answers0