listener no more async
This commit is contained in:
parent
a62350309d
commit
cff8a60e1c
1 changed files with 74 additions and 63 deletions
|
|
@ -121,7 +121,7 @@ async function preparePopupMenu(tab) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
messenger.runtime.onMessage.addListener(async (message, sender, sendResponse) => {
|
messenger.runtime.onMessage.addListener((message, sender, sendResponse) => {
|
||||||
// Check what type of message we have received and invoke the appropriate
|
// Check what type of message we have received and invoke the appropriate
|
||||||
// handler function.
|
// handler function.
|
||||||
if (message && message.hasOwnProperty("command")){
|
if (message && message.hasOwnProperty("command")){
|
||||||
|
|
@ -139,70 +139,78 @@ messenger.runtime.onMessage.addListener(async (message, sender, sendResponse) =>
|
||||||
});
|
});
|
||||||
break;
|
break;
|
||||||
case 'chatgpt_replaceSelectedText':
|
case 'chatgpt_replaceSelectedText':
|
||||||
//console.log('chatgpt_replaceSelectedText: [' + message.tabId +'] ' + message.text)
|
async function _replaceSelectedText(tabId, text) {
|
||||||
original_html = await getOriginalBody(message.tabId);
|
//console.log('chatgpt_replaceSelectedText: [' + tabId +'] ' + text)
|
||||||
await browser.tabs.sendMessage(message.tabId, { command: "replaceSelectedText", text: message.text, tabId: message.tabId });
|
original_html = await getOriginalBody(tabId);
|
||||||
|
await browser.tabs.sendMessage(tabId, { command: "replaceSelectedText", text: text, tabId: tabId });
|
||||||
return true;
|
return true;
|
||||||
case 'chatgpt_replyMessage':
|
|
||||||
const paragraphsHtmlString = message.text;
|
|
||||||
//console.log(">>>>>>>>>>>> paragraphsHtmlString: " + paragraphsHtmlString);
|
|
||||||
let prefs = await browser.storage.sync.get({reply_type: 'reply_all'});
|
|
||||||
//console.log('reply_type: ' + prefs.reply_type);
|
|
||||||
let replyType = 'replyToAll';
|
|
||||||
if(prefs.reply_type === 'reply_sender'){
|
|
||||||
replyType = 'replyToSender';
|
|
||||||
}
|
}
|
||||||
//console.log('replyType: ' + replyType);
|
return _replaceSelectedText(message.tabId, message.text);
|
||||||
// browser.messageDisplay.getDisplayedMessage(message.tabId).then(async (mailMessage) => {
|
case 'chatgpt_replyMessage':
|
||||||
// let reply_tab = await browser.compose.beginReply(mailMessage.id, replyType, {
|
async function _replyMessage(message) {
|
||||||
// type: "reply",
|
const paragraphsHtmlString = message.text;
|
||||||
// //body: paragraphsHtmlString,
|
//console.log(">>>>>>>>>>>> paragraphsHtmlString: " + paragraphsHtmlString);
|
||||||
// isPlainText: false,
|
let prefs = await browser.storage.sync.get({reply_type: 'reply_all'});
|
||||||
// identityId: await getCurrentIdentity(mailMessage),
|
//console.log('reply_type: ' + prefs.reply_type);
|
||||||
// })
|
let replyType = 'replyToAll';
|
||||||
//console.log(">>>>>>>>>>>> message.mailMessageId: " + message.mailMessageId);
|
if(prefs.reply_type === 'reply_sender'){
|
||||||
browser.messages.get(message.mailMessageId).then(async (mailMessage) => {
|
replyType = 'replyToSender';
|
||||||
let curr_idn = await getCurrentIdentity(mailMessage)
|
}
|
||||||
let reply_tab = await browser.compose.beginReply(mailMessage.id, replyType, {
|
//console.log('replyType: ' + replyType);
|
||||||
type: "reply",
|
// browser.messageDisplay.getDisplayedMessage(message.tabId).then(async (mailMessage) => {
|
||||||
//body: paragraphsHtmlString,
|
// let reply_tab = await browser.compose.beginReply(mailMessage.id, replyType, {
|
||||||
isPlainText: false,
|
// type: "reply",
|
||||||
identityId: curr_idn,
|
// //body: paragraphsHtmlString,
|
||||||
})
|
// isPlainText: false,
|
||||||
// Wait for tab loaded.
|
// identityId: await getCurrentIdentity(mailMessage),
|
||||||
await new Promise(resolve => {
|
// })
|
||||||
const tabIsLoaded = tab => {
|
//console.log(">>>>>>>>>>>> message.mailMessageId: " + message.mailMessageId);
|
||||||
return tab.status == "complete" && tab.url != "about:blank";
|
let _mailMessage = await browser.messages.get(message.mailMessageId);
|
||||||
};
|
let curr_idn = await getCurrentIdentity(_mailMessage)
|
||||||
const listener = (tabId, changeInfo, updatedTab) => {
|
let reply_tab = await browser.compose.beginReply(_mailMessage.id, replyType, {
|
||||||
if (tabIsLoaded(updatedTab)) {
|
type: "reply",
|
||||||
browser.tabs.onUpdated.removeListener(listener);
|
//body: paragraphsHtmlString,
|
||||||
//console.log(">>>>>>>>>>>> reply_tab: " + tabId);
|
isPlainText: false,
|
||||||
resolve();
|
identityId: curr_idn,
|
||||||
|
})
|
||||||
|
// 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);
|
||||||
|
//console.log(">>>>>>>>>>>> reply_tab: " + tabId);
|
||||||
|
resolve();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
// Early exit if loaded already
|
||||||
// Early exit if loaded already
|
if (tabIsLoaded(reply_tab)) {
|
||||||
if (tabIsLoaded(reply_tab)) {
|
resolve();
|
||||||
resolve();
|
} else {
|
||||||
} else {
|
browser.tabs.onUpdated.addListener(listener);
|
||||||
browser.tabs.onUpdated.addListener(listener);
|
}
|
||||||
}
|
});
|
||||||
});
|
// we need to wait for the compose windows to load the content script
|
||||||
// we need to wait for the compose windows to load the content script
|
//setTimeout(() => browser.tabs.sendMessage(reply_tab.id, { command: "insertText", text: paragraphsHtmlString, tabId: reply_tab.id }), 500);
|
||||||
//setTimeout(() => browser.tabs.sendMessage(reply_tab.id, { command: "insertText", text: paragraphsHtmlString, tabId: reply_tab.id }), 500);
|
setTimeout(async () => await replaceBody(reply_tab.id, paragraphsHtmlString), 500);
|
||||||
setTimeout(async () => await replaceBody(reply_tab.id, paragraphsHtmlString), 500);
|
return true;
|
||||||
return true;
|
}
|
||||||
});
|
return _replyMessage(message);
|
||||||
break;
|
break;
|
||||||
case 'compose_reloadBody':
|
case 'compose_reloadBody':
|
||||||
modified_html = await getOriginalBody(message.tabId);
|
async function _reloadBody(tabId) {
|
||||||
await setBody(message.tabId, original_html);
|
modified_html = await getOriginalBody(tabId);
|
||||||
await setBody(message.tabId, modified_html);
|
await setBody(tabId, original_html);
|
||||||
return true;
|
await setBody(tabId, modified_html);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return _reloadBody(message.tabId);
|
||||||
break;
|
break;
|
||||||
case 'reload_menus':
|
case 'reload_menus':
|
||||||
await menus.reload();
|
menus.reload();
|
||||||
taLog.log("[ThunderAI] Reloaded menus");
|
taLog.log("Reloading menus");
|
||||||
return true;
|
return true;
|
||||||
break;
|
break;
|
||||||
case 'shortcut_do_prompt':
|
case 'shortcut_do_prompt':
|
||||||
|
|
@ -211,12 +219,15 @@ messenger.runtime.onMessage.addListener(async (message, sender, sendResponse) =>
|
||||||
return true;
|
return true;
|
||||||
break;
|
break;
|
||||||
case 'popup_menu_ready':
|
case 'popup_menu_ready':
|
||||||
let tabs = await browser.tabs.query({ active: true, currentWindow: true });
|
async function _popup_menu_ready() {
|
||||||
if(tabs.length == 0){
|
let tabs = await browser.tabs.query({ active: true, currentWindow: true });
|
||||||
return Promise.resolve(false);
|
if(tabs.length == 0){
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
await preparePopupMenu(tabs[0]);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
preparePopupMenu(tabs[0]);
|
return _popup_menu_ready();
|
||||||
return Promise.resolve(true);
|
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
break;
|
break;
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue