我正在开发一个小的chrome扩展程序并尝试将某些URL上的用户(使用onBeforeWebRequest)重定向到我的html页面,以便他们输入密码(保存在存储上),如果密码正常,我想将它们重定向到他们原来的网址。
我在storage.local中保存用户的原始网址,并在输入正确的密码后将其拉出。
我的问题是,当我尝试从我的页面重定向到用户的原始URL时,onBeforeWebRequest侦听器会再次启动并重定向(到密码页面)。
我考虑过重定向是否启用了某个标志,但我认为如果用户打开几个标签并尝试转到列出的网址,它将无效。
代码:
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'];
});
});