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

Block request URLs that don't contain a specific pattern in chrome

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

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

例如,我在请求阻止选项卡中有 7 个 XMLHttpRequest(XHR) URLs(与 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

小心!因为模式区分大小写

如何屏蔽不带family字的请求?(下同)

  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

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

如果您查看 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。但是您可以轻松地添加这样的功能并向他们提交拉取请求以允许正则表达式模式

为了阻止客户端浏览器上的请求,使用 google chrome 的 HTTP 请求阻止程序和请求阻止程序扩展。

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