From 580410c4b30a843a058470010e0cdc3a8988ade7 Mon Sep 17 00:00:00 2001 From: mic Date: Sun, 21 Jul 2024 22:26:40 +0200 Subject: [PATCH] waiting for the tab to load properly --- mzta-background.js | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/mzta-background.js b/mzta-background.js index e159b060..9838e8eb 100644 --- a/mzta-background.js +++ b/mzta-background.js @@ -210,6 +210,25 @@ async function openChatGPT(promptText, action, curr_tabId, prompt_name = '', do_ createdWindowID = newWindow.id; let createdTab = newWindow.tabs[0]; + // Wait for tab loaded. + await new Promise(resolve => { + const tabIsLoaded = tab => { + return tab.status == "complete" && tab.url != "about:blank"; + }; + const listener = (tabId, changeInfo, updatedTab) => { + if (tabIsLoaded(updatedTab)) { + browser.tabs.onUpdated.removeListener(listener); + resolve(); + } + } + // Early exit if loaded already + if (tabIsLoaded(createdTab)) { + resolve(); + } else { + browser.tabs.onUpdated.addListener(listener); + } + }); + let mailMessage = await browser.messageDisplay.getDisplayedMessage(curr_tabId); let mailMessageId = -1; if(mailMessage) mailMessageId = mailMessage.id;