Skip to content

Instantly share code, notes, and snippets.

@KonradLinkowski
Created April 29, 2020 00:23
Show Gist options
  • Save KonradLinkowski/7035bb109617fdc037cf2ee316bafccf to your computer and use it in GitHub Desktop.
Save KonradLinkowski/7035bb109617fdc037cf2ee316bafccf to your computer and use it in GitHub Desktop.
CORS Anywhere XMLHttpRequest example
// XMLHttpRequest.open override
var cors_api_host = 'cors-anywhere.herokuapp.com';
var cors_api_url = 'https://' + cors_api_host + '/';
var slice = [].slice;
var origin = window.location.protocol + '//' + window.location.host;
var open = XMLHttpRequest.prototype.open;
XMLHttpRequest.prototype.open = function() {
var args = slice.call(arguments);
var targetOrigin = /^https?:\/\/([^\/]+)/i.exec(args[1]);
if (targetOrigin && targetOrigin[0].toLowerCase() !== origin &&
targetOrigin[1] !== cors_api_host) {
args[1] = cors_api_url + args[1];
}
return open.apply(this, args);
};
// use of the overriden method
var req = new XMLHttpRequest();
req.open('GET', 'https://genius.com/Quebonafide-bubbletea-lyrics', false);
req.send(null);
console.log(req.response)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment