From 80a3ae2890b6432cc98d73fff543e8e51fbd24ad Mon Sep 17 00:00:00 2001 From: Joseph Cheung Date: Sun, 26 Feb 2023 13:58:38 +0800 Subject: [PATCH] Update webapp1.py --- searx/webapp1.py | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/searx/webapp1.py b/searx/webapp1.py index c9da8a078..27170fd18 100644 --- a/searx/webapp1.py +++ b/searx/webapp1.py @@ -1016,6 +1016,20 @@ function b64EncodeUnicode(t) } var word_last=""; var lock_chat=1; +function wait(delay){ + return new Promise((resolve) => setTimeout(resolve, delay)); +} + +function fetchRetry(url, tries, fetchOptions = {}) { + function onError(err){ + triesLeft = tries - 1; + if(!triesLeft){ + throw err; + } + return wait(500).then(() => fetchRetry(url, triesLeft, fetchOptions)); + } + return fetch(url,fetchOptions).catch(onError); +} function send_webchat(elem) { if(lock_chat!=0) return; @@ -1026,7 +1040,7 @@ function send_webchat(elem) let word = document.querySelector("#chat_input").value; if(elem){word = elem.textContent;elem.remove(); chatmore();} if(word.length==0 || word.length > 140) return; - fetch('https://search.kg/search?q='+encodeURIComponent(word)+'&language=zh-CN&time_range=&safesearch=0&categories=general&format=json') + fetchRetry('https://search.kg/search?q='+encodeURIComponent(word)+'&language=zh-CN&time_range=&safesearch=0&categories=general&format=json',3) .then(response => response.json()) .then(data => { prompt = JSON.parse(atob( (/