tools.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /*
  2. helper functions for RemoteID javascript
  3. */
  4. // helper function for cross-origin requests
  5. function createCORSRequest(method, url) {
  6. var xhr = new XMLHttpRequest();
  7. if ("withCredentials" in xhr) {
  8. // XHR for Chrome/Firefox/Opera/Safari.
  9. xhr.open(method, url, true);
  10. } else if (typeof XDomainRequest != "undefined") {
  11. // XDomainRequest for IE.
  12. xhr = new XDomainRequest();
  13. xhr.open(method, url);
  14. } else {
  15. // CORS not supported.
  16. xhr = null;
  17. }
  18. return xhr;
  19. }
  20. /*
  21. fill variables in a page from json
  22. */
  23. function page_fill_json_value(json) {
  24. for (var v in json) {
  25. var element = document.getElementById(v);
  26. if (element) {
  27. element.value = json[v];
  28. }
  29. }
  30. }
  31. /*
  32. fill html in a page from json
  33. */
  34. function page_fill_json_html(json) {
  35. for (var v in json) {
  36. var element = document.getElementById(v);
  37. if (element) {
  38. element.innerHTML = json[v];
  39. }
  40. }
  41. }
  42. /*
  43. fetch a URL, calling a callback
  44. */
  45. function ajax_get_callback(url, callback) {
  46. var xhr = createCORSRequest("GET", url);
  47. xhr.onload = function() {
  48. callback(xhr.responseText);
  49. }
  50. xhr.send();
  51. }
  52. /*
  53. fetch a URL, calling a callback for binary data
  54. */
  55. function ajax_get_callback_binary(url, callback) {
  56. var xhr = createCORSRequest("GET", url);
  57. xhr.onload = function() {
  58. console.log("got response length " + xhr.response.byteLength);
  59. callback(xhr.response);
  60. }
  61. xhr.responseType = "arraybuffer";
  62. xhr.send();
  63. }
  64. /*
  65. poll a URL, calling a callback
  66. */
  67. function ajax_poll(url, callback, refresh_ms=1000) {
  68. function again() {
  69. setTimeout(function() { ajax_poll(url, callback, refresh_ms); }, refresh_ms);
  70. }
  71. var xhr = createCORSRequest("GET", url);
  72. xhr.onload = function() {
  73. if (callback(xhr.responseText)) {
  74. again();
  75. }
  76. }
  77. xhr.onerror = function() {
  78. again();
  79. }
  80. xhr.timeout = 3000;
  81. xhr.ontimeout = function() {
  82. again();
  83. }
  84. xhr.send();
  85. }
  86. /*
  87. poll a json file and fill document IDs at the given rate
  88. */
  89. function ajax_json_poll(url, callback, refresh_ms=1000) {
  90. function do_callback(responseText) {
  91. try {
  92. var json = JSON.parse(responseText);
  93. return callback(json);
  94. } catch(e) {
  95. return true;
  96. }
  97. /* on bad json keep going */
  98. return true;
  99. }
  100. ajax_poll(url, do_callback, refresh_ms);
  101. }
  102. /*
  103. poll a json file and fill document IDs at the given rate
  104. */
  105. function ajax_json_poll_fill(url, refresh_ms=1000) {
  106. function callback(json) {
  107. page_fill_json_html(json);
  108. return true;
  109. }
  110. ajax_json_poll(url, callback, refresh_ms);
  111. }
  112. /*
  113. set a message in a div by id, with given color
  114. */
  115. function set_message_color(id, color, message) {
  116. var element = document.getElementById(id);
  117. if (element) {
  118. element.innerHTML = '<b style="color:' + color + '">' + message + '</b>';
  119. }
  120. }