![](/img/trans.png)
[英]chrome.webRequest.onBeforeRequest Only Redirect the last URL
[英]Chrome webRequest onBeforeRequest won't redirect after receiving function return
所以基本上我正在制作某种网络钓鱼网站检测器。
我的程序所做的是:
blocked.html
问题在于重定向。 无论结果如何,它都会重定向回 URL。
代码:
清单文件
{
"manifest_version": 2,
"name": "Phishing Site Detector",
"version": "1.0",
"browser_action": {
"default_icon": "icon.png"
},
"background": {
"scripts": ["background.js"],
"persistent": true
},
"web_accessible_resources": [
"blocked.html"
],
"permissions": [
"webRequest",
"webRequestBlocking",
"<all_urls>"
]
}
背景.js
function checkurl(url){
console.log("checking URL..");
console.log(url);
var xhr = new XMLHttpRequest();
xhr.onreadystatechange = function (){
if (xhr.readyState == 4) {
console.log("response received!");
console.log(xhr.responseText);
return xhr.responseText;
}
};
xhr.open("GET", "http://localhost:5000/"+url, false);
xhr.send();
};
chrome.webRequest.onBeforeRequest.addListener(
function(details){
var res = checkurl(details.url);
if(res == "0"){
console.log("redirecting back..");
return { redirectUrl: details.url };
}else if(res == "1"){
console.log("blocked!");
return { redirectUrl: "chrome-extension://njfcgcmoahjhhbggmapgphfapmedmjhj/blocked.html" };
}
},
{
urls: ["http://*/*", "https://*/*"],
types: ["main_frame"]
},
["blocking"]
);
提前致谢。
onBeforeRequest 在 chrome 扩展中不能是异步的,所以你的代码几乎就在那里,但是当使用不推荐使用的同步 XHR 时,它的send
等待响应,所以你只需立即访问响应而不是 xhr.onreadystatechange 事件:
function checkurl(url) {
var xhr = new XMLHttpRequest();
xhr.open('GET', 'http://localhost:5000/' + url, false); // synchronous
xhr.send();
return xhr.responseText;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.