UNPKG

nightwatch-xhr

Version:
60 lines (54 loc) 2.01 kB
'use strict'; Object.defineProperty(exports, "__esModule", { value: true }); var clientListen = exports.clientListen = function clientListen() { var getXhr = function getXhr(id) { return window.xhrListen.find(function (xhr) { return xhr.id === id; }); }; var rand = function rand() { return Math.random() * 16 | 0; }; var uuidV4 = function uuidV4() { return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) { return (c === 'x' ? rand() : rand() & 0x3 | 0x8).toString(16); }); }; window.xhrListen = []; if (!XMLHttpRequest.customized) { XMLHttpRequest.realSend = XMLHttpRequest.prototype.send; XMLHttpRequest.realOpen = XMLHttpRequest.prototype.open; XMLHttpRequest.prototype.open = function (method, url) { this.id = uuidV4(); window.xhrListen.push({ id: this.id, method: method, url: url, openedTime: Date.now() }); this.onloadend = function () { if (this.readyState === XMLHttpRequest.DONE) { var xhr = getXhr(this.id); if (xhr) { xhr.httpResponseCode = this.status; xhr.responseData = this.response; xhr.status = this.status === 200 ? 'success' : 'error'; xhr.responseHeaders = this.getAllResponseHeaders(); } } }; XMLHttpRequest.realOpen.apply(this, arguments); }; XMLHttpRequest.prototype.send = function (data) { var xhr = getXhr(this.id); if (xhr) xhr.requestData = data; XMLHttpRequest.realSend.apply(this, arguments); }; XMLHttpRequest.customized = true; } }; var clientPoll = exports.clientPoll = function clientPoll() { return window.xhrListen || []; };