/**获取ajax请求信息 */ // 首先拦截ajax请求,然后将这些数据依次抛出 // XMLHttpRequest 文档参考:https://developer.mozilla.org/zh-CN/docs/Web/API/XMLHttpRequest import { innertEvent, windowCustomEventTrigger } from './utils' import { InterceptAjaxData, AjaxEvent } from './types' class Ajax { private data: InterceptAjaxData // 请求集合 private requestArray: any[] = [] constructor() { this.data = {}; this.listenerAjax(); //this.listenerFetch(); } send(ajaxRoot: any, responseText: any) { const currentTime = new Date().getTime(); const loadTime = currentTime - ajaxRoot.request.timeStamp; const responseURL = ajaxRoot.responseURL; const status = ajaxRoot.status; const statusText = ajaxRoot.statusText; if (ajaxRoot.request.ajaxUrl.indexOf('https://hubble.netease.com/track/w') > -1) { return; } innertEvent.trigger('ajax', { currentTime: currentTime, loadTime: loadTime, responseURL: responseURL, status: status, statusText: statusText, responseText: responseText && responseText.substring(0, 200), request: ajaxRoot.request }) } /**监听ajax, 重写 XMLHttpRequest*/ listenerAjax() { const root = this; const oldXHR = window.XMLHttpRequest; const oldOpen = oldXHR.prototype.open; const oldSend = oldXHR.prototype.send; oldXHR.prototype.open = function(method: string, ajaxUrl: string) { this.request = { method: method, ajaxUrl: ajaxUrl, timeStamp: new Date().getTime() }; oldOpen && oldOpen.apply(this, arguments); }; oldXHR.prototype.send = function(body?: any) { this.request.body = body; oldSend && oldSend.apply(this, arguments); const oldOnloadend = this.onloadend; const ajaxRoot = this; this.onloadend = function() { let responseText = ''; const responseType = (ajaxRoot.responseType+'').toLowerCase(); if (responseType === 'blob') { (function(ajaxRoot) { const reader = new FileReader(); reader.onload = function() { const responseText = reader.result; root.send(ajaxRoot, responseText); } try { reader.readAsText(ajaxRoot.response, 'utf-8'); } catch (e) { root.send(ajaxRoot, ajaxRoot.response + ""); } })(ajaxRoot); } else { responseText = ajaxRoot.responseText; } root.send(ajaxRoot, responseText); oldOnloadend && oldOnloadend.apply(this, arguments); } }; window.XMLHttpRequest = oldXHR; // const newXHR: any = function() { // const realXHR = new oldXHR(); // realXHR.addEventListener('loadstart', function () { windowCustomEventTrigger.call(this, 'ajaxLoadStart'); }, false); // realXHR.addEventListener('loadend', function () { windowCustomEventTrigger.call(this, 'ajaxLoadEnd'); }, false); // return realXHR; // } // window.XMLHttpRequest = newXHR; // window.addEventListener('ajaxLoadStart', (e: Event) => { // const tempObj = { // timeStamp: window.performance.now(), // event: e, // url: window.location.href, // uploadFlag: false // }; // // 将请求保存到临时队列中 // this.requestArray.push(tempObj); // this.requestArray = this.requestArray.filter((request) => { // return !request.uploadFlag; // }) // }); // window.addEventListener('ajaxLoadEnd', () => { // this.requestArray.forEach((request: any) => { // if (request && !request.uploadFlag) { // // https://developer.mozilla.org/zh-CN/docs/Web/API/XMLHttpRequest/status // if (request.event.detail.status > 0) { // // https://developer.mozilla.org/zh-CN/docs/Web/API/XMLHttpRequest/responseType // const responseType = (request.event.detail.responseType+'').toLowerCase(); // let responseText = ''; // if (responseType === 'blob') { // (function(request, root) { // const reader = new FileReader(); // reader.onload = function() { // const responseText = reader.result; // root.send(request, responseText); // } // try { // reader.readAsText(request.event.detail.response, 'utf-8'); // } catch (e) { // root.send(request, request.event.detail.response + ""); // } // })(request, this); // } else { // responseText = request.event.detail.responseText // } // this.send(request, responseText) // } // } // }) //}); } /**监听fetch 请求, 重写 fetch */ listenerFetch() { const oldFetch = window.fetch; const root = this; } } export default Ajax