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.

loadScript.native.js 915B

1234567891011121314151617181920212223242526
  1. /**
  2. * Loads a script from a specific URL. React Native cannot load a JS
  3. * file/resource/URL via a <script> HTML element, so the implementation
  4. * fetches the specified <tt>url</tt> as plain text using {@link fetch()} and
  5. * then evaluates the fetched string as JavaScript code (using {@link eval()}).
  6. *
  7. * @param {string} url - The absolute URL from which the script is to be
  8. * (down)loaded.
  9. * @returns {void}
  10. */
  11. export function loadScript(url) {
  12. return (
  13. fetch(url, { method: 'GET' })
  14. .then(response => {
  15. switch (response.status) {
  16. case 200:
  17. return response.responseText || response.text();
  18. default:
  19. throw response.statusText;
  20. }
  21. })
  22. .then(responseText => {
  23. eval.call(window, responseText); // eslint-disable-line no-eval
  24. }));
  25. }