AJAX.js 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. (function(){
  2. function module_init() {
  3. console.log("[lora-prompt-tool] load AJAX module");
  4. lorahelper.lorahelp_js_ajax_txtbox_textarea = null;
  5. lorahelper.lorahelp_js_ajax_txtbox_callback = function() {
  6. lorahelper.debug('lorahelp_js_ajax_txtbox_callback callback');
  7. }
  8. lorahelper.call_lorahelp_js_ajax_txtbox_callback = function(){
  9. lorahelper.lorahelp_js_ajax_txtbox_callback();
  10. }
  11. function build_cors_request(url){
  12. return new Promise((resolve, reject) => {
  13. new myAJAX({
  14. "action": "cors_request",
  15. "url": url
  16. }, lorahelper.js_cors_request_btn).then(response_message => {
  17. const response_json = JSON.parse(response_message);
  18. if(response_json.status === "error"){
  19. reject(response_json.message);
  20. }
  21. resolve(response_json.message);
  22. }).error(error => {
  23. throw new Error("Request failed");
  24. }).sent();
  25. });
  26. }
  27. function readFile(filePath) {
  28. let request = new XMLHttpRequest();
  29. request.open("GET", `file=${filePath}`, false);
  30. request.send(null);
  31. return request.responseText;
  32. }
  33. // Options for the observer (which mutations to observe)
  34. var lorahelp_js_ajax_txtbox_observer_config = { attributes: true, childList: true, subtree: true };
  35. var lorahelp_js_ajax_txtbox_observer_value_check = "";
  36. // Create an observer instance linked to the callback function
  37. var lorahelp_js_ajax_txtbox_observer = new MutationObserver(lorahelper.call_lorahelp_js_ajax_txtbox_callback);
  38. //ajax queue
  39. let ajax_list = []
  40. let ajax_state = "ready";
  41. function update_myAJAX(){
  42. if(ajax_state == "running"){
  43. return;
  44. }
  45. if (ajax_list.length > 0){
  46. let req = ajax_list[0];
  47. req.sentAJAX();
  48. }
  49. }
  50. function remove_ajax_queue_item(self){
  51. for(let i=0; i<ajax_list.length; ++i){
  52. if(ajax_list[i] === self){
  53. ajax_list.splice(i, 1);
  54. break;
  55. }
  56. }
  57. }
  58. function myAJAX(msg, trigger){
  59. this.message = msg;
  60. this.trigger = trigger;
  61. this.then_not_set = true;
  62. this.then_func = lorahelper.noop_func;
  63. this.error_func = lorahelper.noop_func;
  64. this.timeout = 6000;
  65. }
  66. myAJAX.prototype.then = function(then_func) {
  67. this.then_func = then_func;
  68. this.then_not_set = false;
  69. return this;
  70. }
  71. myAJAX.prototype.error = function(error_func) {
  72. this.error_func = error_func;
  73. return this;
  74. }
  75. myAJAX.prototype.sentAJAX = function(){
  76. if(ajax_state == "running"){
  77. return;
  78. }
  79. ajax_state = "running";
  80. // fill to msg box
  81. send_lorahelp_py_ajax(this.message);
  82. if(!this.then_not_set){
  83. lorahelp_js_ajax_txtbox_observer_value_check = lorahelper.lorahelp_js_ajax_txtbox_textarea.value;
  84. this.timeout_id = window.setTimeout((function(self) {return function() {
  85. lorahelp_js_ajax_txtbox_observer.disconnect();
  86. lorahelper.lorahelp_js_ajax_txtbox_callback = function() {
  87. lorahelper.debug('lorahelp_js_ajax_txtbox_callback callback');
  88. }
  89. self.error_func("timeout");
  90. remove_ajax_queue_item(self);
  91. ajax_state = "ready";
  92. update_myAJAX();
  93. }; })(this), this.timeout);
  94. lorahelper.lorahelp_js_ajax_txtbox_callback = (function(self) {return function() {
  95. if(lorahelper.lorahelp_js_ajax_txtbox_textarea.value == lorahelp_js_ajax_txtbox_observer_value_check){
  96. return;
  97. }
  98. window.clearTimeout(self.timeout_id);
  99. lorahelp_js_ajax_txtbox_observer.disconnect();
  100. lorahelper.lorahelp_js_ajax_txtbox_callback = function() {
  101. lorahelper.debug('lorahelp_js_ajax_txtbox_callback callback');
  102. }
  103. self.then_func(lorahelper.lorahelp_js_ajax_txtbox_textarea.value);
  104. remove_ajax_queue_item(self);
  105. ajax_state = "ready";
  106. update_myAJAX();
  107. }; })(this);
  108. lorahelp_js_ajax_txtbox_observer.observe(lorahelper.lorahelp_js_ajax_txtbox_textarea, lorahelp_js_ajax_txtbox_observer_config);
  109. }
  110. this.trigger.click();
  111. if(this.then_not_set){
  112. remove_ajax_queue_item(this);
  113. ajax_state = "ready";
  114. update_myAJAX();
  115. }
  116. }
  117. myAJAX.prototype.sent = function(){
  118. ajax_list.push(this);
  119. update_myAJAX();
  120. }
  121. function my_dispatchEvent(ele,eve){
  122. try {
  123. ele.dispatchEvent(eve);
  124. } catch (error) {
  125. }
  126. }
  127. function send_lorahelp_py_ajax(msg){
  128. lorahelper.debug("run send_lorahelp_py_ajax");
  129. let js_ajax_txtbox = lorahelper.gradioApp().querySelector("#lorahelp_js_ajax_txtbox textarea");
  130. if (js_ajax_txtbox && msg) {
  131. // fill to msg box
  132. update_inputbox(js_ajax_txtbox, JSON.stringify(msg));
  133. //js_ajax_txtbox.dispatchEvent(new Event("input"));
  134. }
  135. }
  136. function update_inputbox(textbox, text){
  137. textbox.value = text;
  138. my_dispatchEvent(textbox, new Event("input", {
  139. bubbles: true,
  140. cancelable: true,
  141. }));
  142. }
  143. function get_lorahelp_py_ajax(){
  144. lorahelper.debug("run get_lorahelp_py_ajax");
  145. const py_ajax_txtbox = lorahelper.gradioApp().querySelector("#lorahelp_py_ajax_txtbox textarea");
  146. if (py_ajax_txtbox && py_ajax_txtbox.value) {
  147. lorahelper.debug("find py_ajax_txtbox");
  148. lorahelper.debug("py_ajax_txtbox value: ");
  149. lorahelper.debug(py_ajax_txtbox.value);
  150. return py_ajax_txtbox.value
  151. } else {
  152. return ""
  153. }
  154. }
  155. const get_new_lorahelp_py_ajax = (max_count=3) => new Promise((resolve, reject) => {
  156. lorahelper.debug("run get_new_lorahelp_py_ajax");
  157. let count = 0;
  158. let new_ajax = "";
  159. let find_ajax = false;
  160. const interval = setInterval(() => {
  161. const py_ajax_txtbox = lorahelper.gradioApp().querySelector("#lorahelp_py_ajax_txtbox textarea");
  162. count++;
  163. if (py_ajax_txtbox && py_ajax_txtbox.value) {
  164. lorahelper.debug("find py_ajax_txtbox");
  165. lorahelper.debug("py_ajax_txtbox value: ");
  166. lorahelper.debug(py_ajax_txtbox.value);
  167. new_ajax = py_ajax_txtbox.value
  168. if (new_ajax != "") {
  169. find_ajax=true
  170. }
  171. }
  172. if (find_ajax) {
  173. //clear msg in both sides
  174. update_inputbox(py_ajax_txtbox, "");
  175. //py_ajax_txtbox.dispatchEvent(new Event("input"));
  176. resolve(new_ajax);
  177. clearInterval(interval);
  178. } else if (count > max_count) {
  179. //clear msg in both sides
  180. update_inputbox(py_ajax_txtbox, "");
  181. //py_ajax_txtbox.dispatchEvent(new Event("input"));
  182. reject('');
  183. clearInterval(interval);
  184. }
  185. }, 1000);
  186. })
  187. lorahelper.build_cors_request = build_cors_request;
  188. lorahelper.readFile = readFile;
  189. lorahelper.myAJAX = myAJAX;
  190. lorahelper.my_dispatchEvent = my_dispatchEvent;
  191. lorahelper.update_inputbox = update_inputbox;
  192. }
  193. let module_loadded = false;
  194. document.addEventListener("DOMContentLoaded", () => {
  195. if (module_loadded) return;
  196. module_loadded = true;
  197. module_init();
  198. });
  199. document.addEventListener("load", () => {
  200. if (module_loadded) return;
  201. module_loadded = true;
  202. module_init();
  203. });
  204. })();