api.js 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. // @license magnet:?xt=urn:btih:0b31508aeb0634b347b8270c7bee4d411b5d4109&dn=agpl-3.0.txt AGPL-3.0
  2. 'use strict';
  3. const check = function (url, next) {
  4. if (!url || !next) {
  5. return;
  6. }
  7. const req = new XMLHttpRequest();
  8. req.open('GET', url, true);
  9. req.setRequestHeader('Authorization', 'GoogleLogin auth=test/1');
  10. req.onerror = function (e) {
  11. next('FAIL: HTTP ' + e);
  12. };
  13. req.onload = function () {
  14. if (this.status == 200) {
  15. next(this.response);
  16. } else {
  17. next('FAIL: HTTP error ' + this.status + ' ' + this.statusText);
  18. }
  19. };
  20. req.send();
  21. };
  22. const pass = function (output) {
  23. output.innerHTML = output.dataset.i18nPass;
  24. };
  25. const encodingSupport = function (output) {
  26. output.innerHTML = output.dataset.i18nEncodingSupport;
  27. };
  28. const invalidConfiguration = function (output) {
  29. output.innerHTML = output.dataset.i18nInvalidConfiguration;
  30. };
  31. const unknownError = function (output, message) {
  32. output.innerHTML = output.dataset.i18nUnknownError + message;
  33. };
  34. const checkGReaderAPI = function () {
  35. const output = document.getElementById('greaderOutput');
  36. const apiUrl = output.dataset.apiUrl;
  37. check(apiUrl + '/check/compatibility', function next(result1) {
  38. if (result1 === 'PASS') {
  39. pass(output);
  40. } else {
  41. check(apiUrl + '/check%2Fcompatibility', function next(result2) {
  42. if (result2 === 'PASS') {
  43. encodingSupport(output);
  44. } else {
  45. check('./greader.php/check/compatibility', function next(result3) {
  46. if (result3 === 'PASS') {
  47. invalidConfiguration(output);
  48. } else {
  49. unknownError(output, result1);
  50. }
  51. });
  52. }
  53. });
  54. }
  55. });
  56. };
  57. const checkFeverAPI = function () {
  58. const output = document.getElementById('feverOutput');
  59. const apiUrl = output.dataset.apiUrl;
  60. check(apiUrl + '?api', function next(result1) {
  61. try {
  62. JSON.parse(result1);
  63. pass(output);
  64. } catch (ex) {
  65. check('./fever.php?api', function next(result2) {
  66. try {
  67. JSON.parse(result2);
  68. invalidConfiguration(output);
  69. } catch (ex) {
  70. unknownError(output, result1);
  71. }
  72. });
  73. }
  74. });
  75. };
  76. /**
  77. * The API tests are done this way to simulate in a more accurate manner
  78. * outside requests. Since the APIs are used by third-party tools, they
  79. * cannot interact at the server level.
  80. */
  81. checkGReaderAPI();
  82. checkFeverAPI();
  83. // @license-end