You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
43 lines
911 B
43 lines
911 B
function httpGet (url, options, callback) {
|
|
let corsRetry = true
|
|
var xhr
|
|
|
|
function readyStateChange () {
|
|
if (xhr.readyState === 4) {
|
|
if (corsRetry && xhr.status === 0) {
|
|
corsRetry = false
|
|
return viaServer()
|
|
}
|
|
|
|
if (xhr.status === 200) {
|
|
callback(null, { body: xhr.responseText })
|
|
} else {
|
|
callback(xhr.responseText)
|
|
}
|
|
}
|
|
}
|
|
|
|
function direct () {
|
|
xhr = new XMLHttpRequest()
|
|
xhr.open('get', url, true)
|
|
xhr.responseType = 'text'
|
|
xhr.onreadystatechange = readyStateChange
|
|
xhr.send()
|
|
}
|
|
|
|
function viaServer () {
|
|
xhr = new XMLHttpRequest()
|
|
xhr.open('get', 'httpGet.php?url=' + encodeURIComponent(url), true)
|
|
xhr.responseType = 'text'
|
|
xhr.onreadystatechange = readyStateChange
|
|
xhr.send()
|
|
}
|
|
|
|
if (options.forceServerLoad) {
|
|
viaServer()
|
|
} else {
|
|
direct()
|
|
}
|
|
}
|
|
|
|
module.exports = httpGet
|