【问题标题】:Block request URLs that don't contain a specific pattern in chrome阻止在 chrome 中不包含特定模式的请求 URL
【发布时间】:2018-01-28 14:08:53
【问题描述】:

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

谷歌浏览器网络标签中有一个请求阻止标签(右键单击请求行然后选择阻止请求URL)。

例如,我在请求阻止选项卡中有 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

点击加号并通过添加模式来阻止具有特定模式的请求(例如模式*family*在请求下方阻止3):

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

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

如何拦截不包含同族词的请求?(下同)

  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

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

【问题讨论】:

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


    【解决方案1】:

    为了阻止客户端浏览器上的请求,使用 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

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 我将阻止来自浏览器(或任何扩展程序)的请求,而不是来自代码的请求。我可以在代码端创建一个拦截器(在 AngularJS 中),并在发送任何请求之前检查所有不包含“家庭”模式。
      • 在您的代码中,如何阻止不包含“家庭”模式?请在您的代码中考虑我的网址。
      • 这不是我的代码,我只是展示了目前 DevTools 中的代码。告诉你当前代码的作用
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-13
      • 2017-12-15
      • 2011-10-15
      • 2013-11-19
      • 2023-03-16
      相关资源
      最近更新 更多