From e61e3c0c28e8ff268350a7cb92532b0bbc15d969 Mon Sep 17 00:00:00 2001 From: huanghongkai Date: Mon, 26 Jun 2023 20:02:29 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20429=E6=8A=A5=E9=94=99=E8=87=AA=E5=8A=A8?= =?UTF-8?q?=E9=87=8D=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- KeepChatGPT.user.js | 59 +++++++++++++++++++++++++++------------------ 1 file changed, 35 insertions(+), 24 deletions(-) diff --git a/KeepChatGPT.user.js b/KeepChatGPT.user.js index 0d58ae89..7c5215e3 100644 --- a/KeepChatGPT.user.js +++ b/KeepChatGPT.user.js @@ -924,31 +924,42 @@ nav.flex div.overflow-y-auto { return fetchRsp; } } catch (e) {} - fetchRsp = target.apply(thisArg, argumentsList); - fetchRsp.then(response => { - let clonedResponse = response.clone(); - clonedResponse.text().then(async fetchRspBody => { - const fetchRspHeaders = clonedResponse.headers; - if (fetchReqUrl.match('/api/auth/session(\\?|$)') && !global.st_ec) { - const email = JSON.parse(fetchRspBody).user.email; - global.st_ec = new IndexedDB(`KeepChatGPT_${email}`, 'conversations'); - cacheEC(); - } else if (gv("k_everchanging", false) === true && fetchReqUrl.match('/backend-api/conversations\\?.*offset=')) { - const b = JSON.parse(fetchRspBody).items; - b.forEach(async el => { - const update_time = new Date(el.update_time); - const ec_tmp = await global.st_ec.get(el.id) || {}; - await global.st_ec.put({id: el.id, title: el.title, update_time: update_time, last: ec_tmp.last, model: ec_tmp.model}); - }); - setTimeout(function() { + let retryCount = 0; + const retry = async () => { + fetchRsp = target.apply(thisArg, argumentsList); + console.log('fetch', thisArg) + const response = await fetchRsp; + if (fetchReqUrl.match('/backend-api/conversation$') && response.status === 429 && retryCount < 20) { // solve 429 status_code, retry 20 times + retryCount++; + await new Promise(resolve => setTimeout(resolve, 1000)); // wait 1 second + return retry() + } + fetchRsp.then(response => { + let clonedResponse = response.clone(); + clonedResponse.text().then(async fetchRspBody => { + const fetchRspHeaders = clonedResponse.headers; + if (fetchReqUrl.match('/api/auth/session(\\?|$)') && !global.st_ec) { + const email = JSON.parse(fetchRspBody).user.email; + global.st_ec = new IndexedDB(`KeepChatGPT_${email}`, 'conversations'); cacheEC(); - attachDate(); - }, 300); - } - }); - return clonedResponse; - }).catch(error => {}); - return fetchRsp; + } else if (gv("k_everchanging", false) === true && fetchReqUrl.match('/backend-api/conversations\\?.*offset=')) { + const b = JSON.parse(fetchRspBody).items; + b.forEach(async el => { + const update_time = new Date(el.update_time); + const ec_tmp = await global.st_ec.get(el.id) || {}; + await global.st_ec.put({id: el.id, title: el.title, update_time: update_time, last: ec_tmp.last, model: ec_tmp.model}); + }); + setTimeout(function() { + cacheEC(); + attachDate(); + }, 300); + } + }); + return clonedResponse; + }).catch(error => {}); + return fetchRsp; + } + return retry() } }); navigator.sendBeacon = function(url, data) {};