1

我将阻止任何不包含特定模式的请求URL。在chrome中不包含特定模式的阻止请求URL

在google chrome网络选项卡中有一个请求阻止选项卡(在请求行上单击右键,然后选择阻止请求URL)。

enter image description here

例如,我有7的XMLHttpRequest(XHR)网址请求联锁翼片(使用Ajax发送):

  1. http://www.test.com?userid=5
  2. http://www.test.com?username=username
  3. http://www.test.com?email=email
  4. http://www.test.com?name=x
  5. http://www.test.com?family=q
  6. http://www.test.com?family=y
  7. http://www.test.com?family=z

点击,通过添加一个图案(例如,图案*family*块3请求下文)具有特定的图案加号和块请求:

  1. http://www.test.com?family=q
  2. http://www.test.com?family=y
  3. http://www.test.com?family=z

小心!因为这些模式是区分大小写的

pattern for block requests

如何阻止不包含家庭字请求?(下同)

  1. http://www.test.com?userid=5
  2. http://www.test.com?username=username
  3. http://www.test.com?email=email
  4. http://www.test.com?name=x

我可以使用正则表达式吗?

回答

1

用于阻止客户端浏览器上的请求,使用HTTP请求阻止程序和Google Chrome请求拦截器扩展。

为了处理从CLIEN端请求,使用以下代码: 之前创建在客户端拦截像下面和控制所有请求和发送(与AngularJS)后:

myApp.config(['$httpProvider', '$locationProvider', 
      function ($httpProvider, $locationProvider) { 
      $httpProvider.interceptors.push(function ($q) { 
       return { 
        //This method is called right before $http send a 
        //request 
        'request': function (config) { 

        }, 
        //This method is called right after $http receives the 
        //response 
        'response': function (config) { 

        }, 
        //request can’t be sent 
        'requestError': function (config) { 

        }, 
        //Sometimes our backend call fails 
        'responseError': function (config) { 

        }, 
       } 
      }); 
     }]); 

Request blocker

Interceptors

1

如果你看一下DevTools源代码

https://github.com/ChromeDevTools/devtools-frontend/blob/aaad45c61dfb22ad885b507a0f38f19863c09553/front_end/network/BlockedURLsPane.js#L194

for (var blockedUrl of this._blockedCountForUrl.keys()) { 
     if (this._matches(url, blockedUrl)) 
     result += this._blockedCountForUrl.get(blockedUrl); 
    } 
    return result; 
    } 

    /** 
    * @param {string} pattern 
    * @param {string} url 
    * @return {boolean} 
    */ 
    _matches(pattern, url) { 
    var pos = 0; 
    var parts = pattern.split('*'); 
    for (var index = 0; index < parts.length; index++) { 
     var part = parts[index]; 
     if (!part.length) 
     continue; 
     pos = url.indexOf(part, pos); 
     if (pos === -1) 
     return false; 
     pos += part.length; 
    } 
    return true; 
    } 

它不使用正则表达式。那么简单的答案是否定的,它不支持使用RegEx。但是,您可以轻松添加这样的功能,并提交拉取请求以允许正则表达式模式。

+0

我将阻止来自浏览器(或任何扩展名)的请求,而不是来自代码。我可以在代码端创建一个拦截器(在AngularJS中),并在发送任何请求之前检查所有不包含“家族”模式。 –

+0

在您的代码中,如何阻止不包含'家庭'模式? 请考虑我的代码中的网址。 –

+0

这不是我的代码,我刚刚展示了截至目前DevTools中的代码。告诉你当前的代码是做什么的 –