Created
June 28, 2019 06:55
-
-
Save ccapndave/2153051e0f8bc13b5e3d80884ca55548 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function monkeyPatchXMLHttpRequest() { | |
const originalSetRequestHeader = XMLHttpRequest.prototype.setRequestHeader; | |
const originalAddEventListener = XMLHttpRequest.prototype.addEventListener; | |
XMLHttpRequest.prototype.setRequestHeader = function (header: string, value: string): void { | |
if (header.toLowerCase() === "x-passphrase") { | |
this.passphrase = value; | |
} else { | |
originalSetRequestHeader.apply(this, [ header, value ]); | |
} | |
}; | |
XMLHttpRequest.prototype.addEventListener = function (type, listener, options?) { | |
if (type === "load") { | |
originalAddEventListener.apply(this, [ "load", async function (event: ProgressEvent) { | |
if (this.passphrase) { | |
Object.defineProperty(this, "response", { writable: true }); | |
Object.defineProperty(this, "responseText", { writable: true }); | |
// this.response is `undefined` here. Have I got mixed up with my `this`s? | |
listener(event); | |
} else { | |
listener(event); | |
} | |
} ]); | |
} else { | |
originalAddEventListener.apply(this, [ type, listener, options ]); | |
} | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment