zoukankan      html  css  js  c++  java
  • 全局拦截各种http请求

    http请求无非就是ajax、src、href、表单

     function hookAJAX() {
        XMLHttpRequest.prototype.nativeOpen = XMLHttpRequest.prototype.open;
        var customizeOpen = function (method, url, async, user, password) {
          // do something
          this.nativeOpen(method, url, async, user, password);
        };
    
        XMLHttpRequest.prototype.open = customizeOpen;
      }
    
      /**
       *全局拦截Image的图片请求添加token
       *
       */
      function hookImg() {
        const property = Object.getOwnPropertyDescriptor(Image.prototype, 'src');
        const nativeSet = property.set;
    
        function customiseSrcSet(url) {
          // do something
          nativeSet.call(this, url);
        }
        Object.defineProperty(Image.prototype, 'src', {
          set: customiseSrcSet,
        });
      }
    
      /**
       * 拦截全局open的url添加token
       *
       */
      function hookOpen() {
        const nativeOpen = window.open;
        window.open = function (url) {
          // do something
          nativeOpen.call(this, url);
        };
      }
    
      function hookFetch() {
        var fet = Object.getOwnPropertyDescriptor(window, 'fetch')
        Object.defineProperty(window, 'fetch', {
          value: function (a, b, c) {
            // do something
            return fet.value.apply(this, args)
          }
        })
      }
  • 相关阅读:
    iOS 组件化方案
    iOS 核心动画概览
    iOS @字面量
    iOS id 和 instancetype 的区别
    C++ 中的 const
    iOS 开发资料
    iOS 架构-App组件化开发
    iOS 知名大牛的一些博客
    iOS 键盘 隐藏系统的 toolBar
    iOS UIView 单独设置一个角为圆角,两个 三个角也行
  • 原文地址:https://www.cnblogs.com/amiezhang/p/9984690.html
Copyright © 2011-2022 走看看