阻止不包含chrome中特定模式的请求URL

时间:2017-08-20 10:37:46

标签: javascript ajax google-chrome xmlhttprequest developer-tools

我将阻止任何不包含特定模式的请求网址。

Google Chrome网络标签中有一个请求阻止标签(右键单击请求行后选择阻止请求网址。)

enter image description here

例如,我在请求阻止选项卡中有7个XMLHttpRequest(XHR)URL(与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
  8. 单击加号并通过添加模式来阻止具有特定模式的请求(例如,请求下方的模式*family*块3):

    1. http://www.test.com?family=q
    2. http://www.test.com?family=y
    3. http://www.test.com?family=z
    4. 小心!因为模式区分大小写

      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
      5. 我可以使用正则表达式吗?

2 个答案:

答案 0 :(得分: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。如此简单的答案是否,它不支持使用RegEx。但您可以轻松添加此类功能并向其提交拉取请求以允许正则表达式模式

答案 1 :(得分:1)

用于阻止客户端浏览器上的请求,使用Google请求阻止程序和谷歌浏览器的请求阻止程序扩展。

使用以下代码处理来自客户的请求: 在客户端创建一个拦截器,如下所示,并在发送之前和之后控制所有请求(使用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