我正在尝试阻止某些页面并将用户重定向到扩展内的文件,这是我的代码:
function onHeadersReceivedHandler(details) {
if (enabled && device && device.signature) {
var item = map[details.url.toString()];
if (!item) {
item = map[details.url.toString()] = details;
}
console.log("url: ", details.url.toString());
console.log("onBeforeRequest: ", item);
if (item.allowed === false) {
console.log("NOT ALLOWED ", item);
return {
redirectUrl: chrome.extension.getURL(blockUrl)
};
}
else if (item.allowed === true) {
console.log("ALLOWED ", item);
return details;
}
else {
console.log("NEED TO CHECK ", item);
item.allowed = false;
if (item.allowed === false) {
console.log("NOT ALLOWED AFTER CHECK: ", item);
return {
redirectUrl: chrome.extension.getURL(blockUrl)
};
}
}
}
}
chrome.webRequest.onHeadersReceived.addListener(onHeadersReceivedHandler, {
urls: ["http://*/*", "https://*/*"],
types: ["main_frame"]
}, ["responseHeaders", "blocking"]);
但是当此代码检测到被阻止的页面时,chrome会重定向到about:blank页面而不是我的阻止页面。最有趣的是,当我打开阻止页面和刷新页面后激活扩展时一切正常,除非有时候没有加载css,但是当我输入url并点击输入时,它会转到about:blank。这是一些铬问题还是我错过了什么?
答案 0 :(得分:0)
刚补充说:
"web_accessible_resources": [
"block.html"
]
在manifest.json