Fixed a possible race condition injecting the script. Thanks to John Bieling.
This commit is contained in:
parent
5e4de1c01a
commit
0ecf655245
1 changed files with 34 additions and 15 deletions
|
|
@ -79,27 +79,46 @@ messenger.runtime.onMessage.addListener(async (message, sender, sendResponse) =>
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
||||||
async function openChatGPT(promptText,action,curr_tabId){
|
async function openChatGPT(promptText, action, curr_tabId) {
|
||||||
let prefs = await browser.storage.sync.get(prefs_default);
|
let prefs = await browser.storage.sync.get(prefs_default);
|
||||||
prefs = checkScreenDimensions(prefs);
|
prefs = checkScreenDimensions(prefs);
|
||||||
return browser.windows.create({
|
let newWindow = await browser.windows.create({
|
||||||
url: "https://chat.openai.com",
|
url: "https://chat.openai.com",
|
||||||
type: "popup",
|
type: "popup",
|
||||||
width: prefs.chatgpt_win_width,
|
width: prefs.chatgpt_win_width,
|
||||||
height: prefs.chatgpt_win_height
|
height: prefs.chatgpt_win_height
|
||||||
}).then((newWindow) => {
|
});
|
||||||
console.log("Script started...");
|
|
||||||
createdWindowID = newWindow.id;
|
console.log("Script started...");
|
||||||
//console.log(promptText);
|
createdWindowID = newWindow.id;
|
||||||
const tabId = newWindow.tabs[0].id;
|
const createdTab = newWindow.tabs[0];
|
||||||
browser.tabs.executeScript(tabId,{code: mzta_script})
|
|
||||||
.then(async () => {
|
// Wait for tab loaded.
|
||||||
console.log("Script injected successfully");
|
await new Promise(resolve => {
|
||||||
browser.tabs.sendMessage(tabId, {command: "chatgpt_send", prompt: promptText, action: action, tabId: curr_tabId});
|
const tabIsLoaded = tab => {
|
||||||
}).catch(err => {
|
return tab.status == "complete" && tab.url != "about:blank";
|
||||||
console.error("Error injecting the script: ", err);
|
};
|
||||||
});
|
const listener = (tabId, changeInfo, updatedTab) => {
|
||||||
});
|
if (tabIsLoaded(updatedTab)) {
|
||||||
|
browser.tabs.onUpdated.removeListener(listener);
|
||||||
|
resolve();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Early exit if loaded already
|
||||||
|
if (tabIsLoaded(createdTab)) {
|
||||||
|
resolve();
|
||||||
|
}
|
||||||
|
browser.tabs.onUpdated.addListener(listener);
|
||||||
|
})
|
||||||
|
|
||||||
|
browser.tabs.executeScript(createdTab.id, { code: mzta_script, matchAboutBlank: false })
|
||||||
|
.then(async () => {
|
||||||
|
console.log("Script injected successfully");
|
||||||
|
browser.tabs.sendMessage(createdTab.id, { command: "chatgpt_send", prompt: promptText, action: action, tabId: curr_tabId });
|
||||||
|
})
|
||||||
|
.catch(err => {
|
||||||
|
console.error("Error injecting the script: ", err);
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function checkScreenDimensions(prefs){
|
function checkScreenDimensions(prefs){
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue