Skip to content

Instantly share code, notes, and snippets.

@russelldavies
Created August 5, 2023 11:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save russelldavies/67152910c8b4ed923e46f5234ac6f72b to your computer and use it in GitHub Desktop.
Save russelldavies/67152910c8b4ed923e46f5234ac6f72b to your computer and use it in GitHub Desktop.
Intercept unauthorized requests for Elm
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