重定向到chrome扩展中的本地文件

时间:2022-10-24 10:40:14

I'm trying to block some pages and redirect users to file inside extension and here is my code:

我试图阻止一些页面并将用户重定向到扩展内的文件,这是我的代码:

 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"]);

But when this code detect blocked page, chrome redirect to about:blank page instead my block page. The most interesting thing is that everything works fine when I activate extension after opening page for blocking and than refresh page, except sometimes css is not loaded, but when I type url and hit enter it goes to about:blank. Is this some chrome issue or I'm missing something?

但是当此代码检测到被阻止的页面时,chrome会重定向到about:blank页面而不是我的阻止页面。最有趣的是,当我打开阻止页面和刷新页面后激活扩展时一切正常,除非有时css没有加载,但是当我输入url并点击输入时,它会转到about:blank。这是一些铬问题还是我错过了什么?

1 个解决方案

#1


0  

Just added:

"web_accessible_resources": [
   "block.html"
]

in my manifest.json

在我的manifest.json中

#1


0  

Just added:

"web_accessible_resources": [
   "block.html"
]

in my manifest.json

在我的manifest.json中