Created
April 29, 2020 00:23
-
-
Save KonradLinkowski/7035bb109617fdc037cf2ee316bafccf to your computer and use it in GitHub Desktop.
CORS Anywhere XMLHttpRequest example
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.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