2016-10-05 14:36:59 +00:00
|
|
|
/**
|
2016-12-07 22:00:54 +00:00
|
|
|
* Loads a script from a specific URL. React Native cannot load a JS
|
2017-08-23 13:03:55 +00:00
|
|
|
* file/resource/URL via a <script> HTML element, so the implementation
|
|
|
|
* fetches the specified src as plain text using fetch() and then
|
2016-10-05 14:36:59 +00:00
|
|
|
* 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.
|
2016-10-05 14:36:59 +00:00
|
|
|
* @returns {void}
|
|
|
|
*/
|
2016-12-07 22:00:54 +00:00
|
|
|
export function loadScript(url) {
|
|
|
|
return (
|
2017-08-23 13:03:55 +00:00
|
|
|
fetch(url, { method: 'GET' })
|
2016-12-07 22:00:54 +00:00
|
|
|
.then(response => {
|
|
|
|
switch (response.status) {
|
|
|
|
case 200:
|
|
|
|
return response.responseText || response.text();
|
|
|
|
|
|
|
|
default:
|
|
|
|
throw response.statusText;
|
|
|
|
}
|
2016-10-05 14:36:59 +00:00
|
|
|
|
2016-12-07 22:00:54 +00:00
|
|
|
})
|
|
|
|
.then(responseText => {
|
|
|
|
eval.call(window, responseText); // eslint-disable-line no-eval
|
|
|
|
}));
|
2016-10-05 14:36:59 +00:00
|
|
|
}
|