Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
'use strict';

const XMLHttpRequestSend = XMLHttpRequest.prototype.send;
const XMLHttpRequestOpen = XMLHttpRequest.prototype.open;

var reqCallbacks = [];
var resCallbacks = [];
Expand All @@ -26,6 +27,12 @@ export function removeResponseCallback(callback) {
export function wire() {
if (wired) { throw new Error('Ajax interceptor already wired'); }

XMLHttpRequest.prototype.open = function(method, url) {
this._method = method;
this._url = url;
XMLHttpRequestOpen.apply(this, arguments);
};

XMLHttpRequest.prototype.send = function() {
var reqCallbacksRes = reqCallbacks.map(callback => callback(this, arguments));
var onreadystatechange = this.onreadystatechange;
Expand All @@ -47,6 +54,7 @@ export function wire() {
export function unwire() {
if (!wired) { throw new Error('Ajax interceptor not currently wired'); }

XMLHttpRequest.prototype.open = XMLHttpRequestOpen;
XMLHttpRequest.prototype.send = XMLHttpRequestSend;
wired = false;
};