Chrome WebRequest仅侦听用户输入的URL [英] Chrome webRequest listening to only user entered URLs

查看:95
本文介绍了Chrome WebRequest仅侦听用户输入的URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在制作一个Chrome扩展程序,该扩展程序仅允许用户访问给定白名单上的网站. chrome.webRequest.onBeforeRequest非常适合拦截和检查URL,但是我遇到的问题是,它会检查所有传入的URL,包括在网页尝试加载资源时.我希望它仅检查用户输入的URL,并且该URL是否在白名单上,所以我希望它允许该网页加载所需的任何资源,而不管它们是否在白名单上.

I'm making a Chrome extension that only allows users to access websites that are on a given whitelist. chrome.webRequest.onBeforeRequest is perfect for intercepting and examining the URLs but the problem I am having is that it examines all incoming URLs including when a webpage is trying to load resources. I want it to only examine user entered URLs and if that URL is on the whitelist I want it to allow that webpage to load any resources it needs, regardless of if they are on the whitelist or not.

这是我给侦听器的代码.

Here is my code for the listener.

    chrome.webRequest.onBeforeRequest.addListener(
      function(info) {
        console.log("URL: " + info.url);
        var pageURL = info['url'];
        let mngwlst = new ManageWhitelist();
        var whitelist = mngwlst.getWhitelist();
        if(whitelist == null) mngwlst.setWhitelist([]);
        var denyRequest = false;
        var denyRequest = monitor.ExamineWhitelist(pageURL, whitelist);
        console.log(denyRequest);
        return {cancel: denyRequest}
    },

    {
        urls: [
        "<all_urls>"
        ],
    },

    ["blocking"]);

monitor.ExamineWhitelist(pageURL, whitelist)将返回true或false,具体取决于URL是否在白名单上.

monitor.ExamineWhitelist(pageURL, whitelist) will return true or false depending on if the URL is or isn't on the whitelist.

推荐答案

尝试仅过滤用户输入"的URL很棘手,但这里可能对您有所帮助的是webRequest资源类型:

Trying to filter only 'user entered' URLs is tricky, but what might help you here is webRequest resource types: https://developer.chrome.com/extensions/webRequest#type-ResourceType

资源类型仅允许您过滤某些类型的请求.例如,"Main_frame"是在顶层框架上加载的文档.这样,您的onBeforeRequest侦听器不会在每次请求图像或样式表时触发.

Resource types allow you to only filter certain types of requests. 'Main_frame' for example, is the document loaded at the top level frame. This way your onBeforeRequest listener won't fire every time an image or style sheet is requested.

您可以按照与通过URL进行过滤相同的方式来按类型进行过滤:

You can filter by types in the same way you can filter by URLs:

chrome.webRequest.onBeforeRequest.addListener(
      function(info) {
        console.log("URL: " + info.url);
        var pageURL = info['url'];
        let mngwlst = new ManageWhitelist();
        var whitelist = mngwlst.getWhitelist();
        if(whitelist == null) mngwlst.setWhitelist([]);
        var denyRequest = false;
        var denyRequest = monitor.ExamineWhitelist(pageURL, whitelist);
        console.log(denyRequest);
        return {cancel: denyRequest}
    },

{ 
    urls: ["<all_urls>"],
    types: ["main_frame"],
},
["blocking"]);

这篇关于Chrome WebRequest仅侦听用户输入的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆