126 lines
3.9 KiB
JavaScript
126 lines
3.9 KiB
JavaScript
|
var fetchTextFromURL;
|
||
|
if (typeof XMLHttpRequest != 'undefined') {
|
||
|
fetchTextFromURL = function(url, authorization, fulfill, reject) {
|
||
|
var xhr = new XMLHttpRequest();
|
||
|
var sameDomain = true;
|
||
|
var doTimeout = false;
|
||
|
if (!('withCredentials' in xhr)) {
|
||
|
// check if same domain
|
||
|
var domainCheck = /^(\w+:)?\/\/([^\/]+)/.exec(url);
|
||
|
if (domainCheck) {
|
||
|
sameDomain = domainCheck[2] === window.location.host;
|
||
|
if (domainCheck[1])
|
||
|
sameDomain &= domainCheck[1] === window.location.protocol;
|
||
|
}
|
||
|
}
|
||
|
if (!sameDomain && typeof XDomainRequest != 'undefined') {
|
||
|
xhr = new XDomainRequest();
|
||
|
xhr.onload = load;
|
||
|
xhr.onerror = error;
|
||
|
xhr.ontimeout = error;
|
||
|
xhr.onprogress = function() {};
|
||
|
xhr.timeout = 0;
|
||
|
doTimeout = true;
|
||
|
}
|
||
|
function load() {
|
||
|
fulfill(xhr.responseText);
|
||
|
}
|
||
|
function error() {
|
||
|
reject(new Error('XHR error' + (xhr.status ? ' (' + xhr.status + (xhr.statusText ? ' ' + xhr.statusText : '') + ')' : '') + ' loading ' + url));
|
||
|
}
|
||
|
|
||
|
xhr.onreadystatechange = function () {
|
||
|
if (xhr.readyState === 4) {
|
||
|
// in Chrome on file:/// URLs, status is 0
|
||
|
if (xhr.status == 0) {
|
||
|
if (xhr.responseText) {
|
||
|
load();
|
||
|
}
|
||
|
else {
|
||
|
// when responseText is empty, wait for load or error event
|
||
|
// to inform if it is a 404 or empty file
|
||
|
xhr.addEventListener('error', error);
|
||
|
xhr.addEventListener('load', load);
|
||
|
}
|
||
|
}
|
||
|
else if (xhr.status === 200) {
|
||
|
load();
|
||
|
}
|
||
|
else {
|
||
|
error();
|
||
|
}
|
||
|
}
|
||
|
};
|
||
|
xhr.open("GET", url, true);
|
||
|
|
||
|
if (xhr.setRequestHeader) {
|
||
|
xhr.setRequestHeader('Accept', 'application/x-es-module, */*');
|
||
|
// can set "authorization: true" to enable withCredentials only
|
||
|
if (authorization) {
|
||
|
if (typeof authorization == 'string')
|
||
|
xhr.setRequestHeader('Authorization', authorization);
|
||
|
xhr.withCredentials = true;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
if (doTimeout) {
|
||
|
setTimeout(function() {
|
||
|
xhr.send();
|
||
|
}, 0);
|
||
|
} else {
|
||
|
xhr.send(null);
|
||
|
}
|
||
|
};
|
||
|
}
|
||
|
else if (typeof require != 'undefined' && typeof process != 'undefined') {
|
||
|
var fs;
|
||
|
fetchTextFromURL = function(url, authorization, fulfill, reject) {
|
||
|
if (url.substr(0, 8) != 'file:///')
|
||
|
throw new Error('Unable to fetch "' + url + '". Only file URLs of the form file:/// allowed running in Node.');
|
||
|
fs = fs || require('fs');
|
||
|
if (isWindows)
|
||
|
url = url.replace(/\//g, '\\').substr(8);
|
||
|
else
|
||
|
url = url.substr(7);
|
||
|
return fs.readFile(url, function(err, data) {
|
||
|
if (err) {
|
||
|
return reject(err);
|
||
|
}
|
||
|
else {
|
||
|
// Strip Byte Order Mark out if it's the leading char
|
||
|
var dataString = data + '';
|
||
|
if (dataString[0] === '\ufeff')
|
||
|
dataString = dataString.substr(1);
|
||
|
|
||
|
fulfill(dataString);
|
||
|
}
|
||
|
});
|
||
|
};
|
||
|
}
|
||
|
else if (typeof self != 'undefined' && typeof self.fetch != 'undefined') {
|
||
|
fetchTextFromURL = function(url, authorization, fulfill, reject) {
|
||
|
var opts = {
|
||
|
headers: {'Accept': 'application/x-es-module, */*'}
|
||
|
};
|
||
|
|
||
|
if (authorization) {
|
||
|
if (typeof authorization == 'string')
|
||
|
opts.headers['Authorization'] = authorization;
|
||
|
opts.credentials = 'include';
|
||
|
}
|
||
|
|
||
|
fetch(url, opts)
|
||
|
.then(function (r) {
|
||
|
if (r.ok) {
|
||
|
return r.text();
|
||
|
} else {
|
||
|
throw new Error('Fetch error: ' + r.status + ' ' + r.statusText);
|
||
|
}
|
||
|
})
|
||
|
.then(fulfill, reject);
|
||
|
}
|
||
|
}
|
||
|
else {
|
||
|
throw new TypeError('No environment fetch API available.');
|
||
|
}
|