简体   繁体   English

Chrome webRequest仅收听用户输入的网址

[英]Chrome webRequest listening to only user entered URLs

I'm making a Chrome extension that only allows users to access websites that are on a given whitelist. 我正在制作Chrome扩展程序,仅允许用户访问指定白名单中的网站。 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. chrome.webRequest.onBeforeRequest非常适合拦截和检查URL,但我遇到的问题是它会检查所有传入的URL,包括网页何时尝试加载资源。 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. 我希望它只检查用户输入的URL,如果该URL在白名单上,我希望它允许该网页加载它需要的任何资源,无论它们是否在白名单中。

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) will return true or false depending on if the URL is or isn't on the whitelist. monitor.ExamineWhitelist(pageURL, whitelist)将返回true或false,具体取决于URL是否在白名单中。

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 尝试仅过滤“用户输入的”网址很棘手,但可能对您有所帮助的是webRequest资源类型: https//developer.chrome.com/extensions/webRequest#type-ResourceType

Resource types allow you to only filter certain types of requests. 资源类型允许您仅筛选某些类型的请求。 'Main_frame' for example, is the document loaded at the top level frame. 例如,'Main_frame'是在顶层框架加载的文档。 This way your onBeforeRequest listener won't fire every time an image or style sheet is requested. 这样,每次请求图像或样式表时,onBeforeRequest侦听器都不会触发。

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

info has an attribute called type that returns the resourceType of the webRequest. info有一个名为type的属性,它返回webRequest的resourceType。

The resource types are listed here: https://developer.chrome.com/extensions/webRequest#type-ResourceType , with "main_frame" being the type you are looking for. 此处列出了资源类型: https//developer.chrome.com/extensions/webRequest#type-ResourceType ,其中“main_frame”是您要查找的类型。

chrome.webRequest.onBeforeRequest.addListener(
  function(info) {
    if(info.type == "main_frame"){
        doMyStuff();
        return {cancel: denyRequest};
    }
},
{
    urls: [
    "<all_urls>"
    ],
},
["blocking"]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM