jiti-meet/react/features/base/util/loadScript.native.js

56 lines
1.7 KiB
JavaScript
Raw Normal View History

/**
2016-12-07 22:00:54 +00:00
* Loads a script from a specific URL. React Native cannot load a JS
* file/resource/URL via a <script> HTML element, so the implementation
* fetches the specified src as plain text (e.g. via XMLHttpRequest) and then
* evaluates the fetched string as JavaScript code (i.e. via the {@link eval}
* function).
*
2016-12-07 22:00:54 +00:00
* @param {string} url - The absolute URL from the which the script is to be
* (down)loaded.
* @returns {void}
*/
2016-12-07 22:00:54 +00:00
export function loadScript(url) {
let fetch;
const method = 'GET';
2016-12-07 22:00:54 +00:00
// Prefer the Fetch API. Apart from the fact that we're fetching the
// specified script as a static resource, the Fetch API provides more
// detailed errors.
if (typeof (fetch = window.fetch) === 'function') {
fetch = fetch(url, { method });
} else {
// Otherwise, fall back to the XMLHttpRequest API.
fetch
= new Promise(resolve => {
const xhr = new XMLHttpRequest();
2016-12-07 22:00:54 +00:00
xhr.responseType = 'text';
xhr.onreadystatechange = () => {
if (xhr.readyState === 4) {
resolve(xhr);
}
2016-12-07 22:00:54 +00:00
};
xhr.open(method, url, /* async */ true);
xhr.send();
});
}
2016-12-07 22:00:54 +00:00
return (
fetch
.then(response => {
switch (response.status) {
case 200:
return response.responseText || response.text();
default:
throw response.statusText;
}
2016-12-07 22:00:54 +00:00
})
.then(responseText => {
eval.call(window, responseText); // eslint-disable-line no-eval
}));
}