From 0353a3807c85071b19478c9df4ee68d6e3910f69 Mon Sep 17 00:00:00 2001 From: "adamlui@protonmail.com" Date: Thu, 12 Sep 2024 19:34:49 +0000 Subject: [PATCH] =?UTF-8?q?Updated=20`chatgpt.isIdle()`=20+=20`chatgpt.cod?= =?UTF-8?q?e.isIdle()`=20to=20work=20w/=20new=20UI=20=E2=86=9E=20[auto-syn?= =?UTF-8?q?c=20from=20`KudoAI/chatgpt.js`]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- starters/chrome/extension/lib/chatgpt.js | 38 ++++++++++++++++-------- 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/starters/chrome/extension/lib/chatgpt.js b/starters/chrome/extension/lib/chatgpt.js index 3eb882581..ab2909692 100644 --- a/starters/chrome/extension/lib/chatgpt.js +++ b/starters/chrome/extension/lib/chatgpt.js @@ -423,25 +423,26 @@ const chatgpt = { // eslint-disable-line no-redeclare }); await new Promise(resolve => { // when reply starts generating new MutationObserver((_, obs) => { - if (!chatgpt.getRegenerateBtn()) { obs.disconnect(); resolve(); } - }).observe(document.body, obsConfig); + if (chatgpt.getStopBtn()) { obs.disconnect(); resolve(); } + }).observe(document.body, { childList: true, subtree: true }); }); - const lastReplyDiv = await new Promise(resolve => { // when code starts generating + const replyDivs = document.querySelectorAll(selectors.replyDiv), + lastReplyDiv = replyDivs[replyDivs.length - 1]; + await new Promise(resolve => { // when code starts generating new MutationObserver((_, obs) => { - const replyDivs = document.querySelectorAll(selectors.replyDiv), - lastReplyDiv = replyDivs[replyDivs.length - 1]; - if (lastReplyDiv?.querySelector('pre')) { obs.disconnect(); resolve(lastReplyDiv); } + if (lastReplyDiv?.querySelector('pre')) { obs.disconnect(); resolve(); } }).observe(document.body, obsConfig); }); await new Promise(resolve => { // when code stops generating new MutationObserver((_, obs) => { if (lastReplyDiv?.querySelector('pre').nextElementSibling // code block not last child of reply div - || chatgpt.getRegenerateBtn() // ...or reply outright stopped generating + || !chatgpt.getStopBtn() // ...or reply outright stopped generating ) { obs.disconnect(); resolve(); } }).observe(document.body, obsConfig); }); }, + async minify(code) { if (!code) return console.error('Code argument not supplied. Pass some code!'); if (typeof code !== 'string') return console.error('Code argument must be a string!'); @@ -1081,12 +1082,25 @@ const chatgpt = { // eslint-disable-line no-redeclare isDarkMode() { return document.documentElement.classList.toString().includes('dark'); }, isFullScreen() { return chatgpt.browser.isFullScreen(); }, - isIdle() { - return new Promise(resolve => { - if (chatgpt.getRegenerateBtn()) resolve(true); + async isIdle() { + const obsConfig = { childList: true, subtree: true }, + msgDivSelector = 'div[data-message-author-role]'; + + await new Promise(resolve => { // when in conversation page + if (document.querySelector(msgDivSelector)) resolve(); else new MutationObserver((_, obs) => { - if (chatgpt.getRegenerateBtn()) { obs.disconnect(); resolve(true); } - }).observe(document.body, { childList: true, subtree: true }); + if (document.querySelector(msgDivSelector)) { obs.disconnect(); resolve(); } + }).observe(document.body, obsConfig); + }); + await new Promise(resolve => { // when reply starts generating + new MutationObserver((_, obs) => { + if (chatgpt.getStopBtn()) { obs.disconnect(); resolve(); } + }).observe(document.body, obsConfig); + }); + return new Promise(resolve => { // when reply stops generating + new MutationObserver((_, obs) => { + if (!chatgpt.getStopBtn()) { obs.disconnect(); resolve(true); } + }).observe(document.body, obsConfig); }); },