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

  1. function httpGet (url, options, callback) {
  2. let corsRetry = true
  3. var xhr
  4. function readyStateChange () {
  5. if (xhr.readyState === 4) {
  6. if (corsRetry && xhr.status === 0) {
  7. corsRetry = false
  8. return viaServer()
  9. }
  10. if (xhr.status === 200) {
  11. callback(null, { body: xhr.responseText })
  12. } else {
  13. callback(xhr.responseText)
  14. }
  15. }
  16. }
  17. function direct () {
  18. xhr = new XMLHttpRequest()
  19. xhr.open('get', url, true)
  20. xhr.responseType = 'text'
  21. xhr.onreadystatechange = readyStateChange
  22. xhr.send()
  23. }
  24. function viaServer () {
  25. xhr = new XMLHttpRequest()
  26. xhr.open('get', 'httpGet.php?url=' + encodeURIComponent(url), true)
  27. xhr.responseType = 'text'
  28. xhr.onreadystatechange = readyStateChange
  29. xhr.send()
  30. }
  31. if (options.forceServerLoad) {
  32. viaServer()
  33. } else {
  34. direct()
  35. }
  36. }
  37. module.exports = httpGet