Created
August 5, 2023 11:26
-
-
Save russelldavies/67152910c8b4ed923e46f5234ac6f72b to your computer and use it in GitHub Desktop.
Intercept unauthorized requests for Elm
This file contains 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
XMLHttpRequest = class extends XMLHttpRequest { | |
send() { | |
const onreadystatechange = this.onreadystatechange; | |
this.onreadystatechange = () => { | |
console.log(this.readyState); | |
if (typeof onreadystatechange === 'function') { | |
this._onreadystatechange(); | |
} | |
}; | |
super.send(); | |
} | |
}; | |
const open = XMLHttpRequest.prototype.open; | |
const send = XMLHttpRequest.prototype.send; | |
XMLHttpRequest.prototype.open = function(method, url) { | |
return open.apply(this, arguments); | |
}; | |
XMLHttpRequest.prototype.send = function(data) { | |
function handleResponse() { | |
if (this.status === 401) { | |
app.ports.notAuthorizedIncoming.send(window.location.pathname); | |
} | |
} | |
this.addEventListener("load", handleResponse); | |
this.addEventListener("error", handleResponse); | |
this.addEventListener("timeout", handleResponse); | |
send.apply(this, arguments); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment