using system menus. fixes #23
This commit is contained in:
parent
17906d4a7e
commit
6f9d80f29e
4 changed files with 169 additions and 5 deletions
|
|
@ -107,6 +107,10 @@ switch (message.command) {
|
||||||
alert(browser.i18n.getMessage('msg_prompt_too_long'));
|
alert(browser.i18n.getMessage('msg_prompt_too_long'));
|
||||||
break;
|
break;
|
||||||
|
|
||||||
|
case 'sendAlert':
|
||||||
|
alert(message.message);
|
||||||
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
// do nothing
|
// do nothing
|
||||||
break;
|
break;
|
||||||
|
|
|
||||||
156
js/mzta-menus.js
Normal file
156
js/mzta-menus.js
Normal file
|
|
@ -0,0 +1,156 @@
|
||||||
|
/*
|
||||||
|
* ThunderAI [https://micz.it/thunderdbird-addon-thunderai/]
|
||||||
|
* Copyright (C) 2024 Mic (m@micz.it)
|
||||||
|
|
||||||
|
* This program is free software: you can redistribute it and/or modify
|
||||||
|
* it under the terms of the GNU General Public License as published by
|
||||||
|
* the Free Software Foundation, either version 3 of the License, or
|
||||||
|
* (at your option) any later version.
|
||||||
|
|
||||||
|
* This program is distributed in the hope that it will be useful,
|
||||||
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
* GNU General Public License for more details.
|
||||||
|
|
||||||
|
* You should have received a copy of the GNU General Public License
|
||||||
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
// Some original methods are derived from https://github.com/ali-raheem/Aify/blob/cfadf52f576b7be3720b5b73af7c8d3129c054da/plugin/html/actions.js
|
||||||
|
|
||||||
|
import { defaultPrompts } from './mzta-prompts.js';
|
||||||
|
import { getLanguageDisplayName } from './mzta-utils.js'
|
||||||
|
|
||||||
|
export class mzta_Menus {
|
||||||
|
|
||||||
|
openChatGPT = null;
|
||||||
|
menu_context_compose = 'compose_action_menu';
|
||||||
|
menu_context_display = 'message_display_action_menu';
|
||||||
|
menu_listeners = {};
|
||||||
|
|
||||||
|
rootMenu = [
|
||||||
|
//{ id: 'ItemC', act: (info, tab) => { console.log('ItemC', info, tab, info.menuItemId); alert('ItemC') } },
|
||||||
|
];
|
||||||
|
|
||||||
|
constructor(openChatGPT) {
|
||||||
|
this.openChatGPT = openChatGPT;
|
||||||
|
defaultPrompts.forEach((prompt) => {
|
||||||
|
this.addAction(prompt)
|
||||||
|
});
|
||||||
|
//this.addMenu(this.rootMenu);
|
||||||
|
}
|
||||||
|
|
||||||
|
addAction = (curr_prompt) => {
|
||||||
|
|
||||||
|
let curr_menu_entry = {id: curr_prompt.id };
|
||||||
|
|
||||||
|
const getHighlight = async () => {
|
||||||
|
const tabs = await browser.tabs.query({ active: true, currentWindow: true });
|
||||||
|
return {tabId: tabs[0].id,
|
||||||
|
selection: await browser.tabs.sendMessage(tabs[0].id, { command: "getSelectedText" }),
|
||||||
|
text: await browser.tabs.sendMessage(tabs[0].id, { command: "getTextOnly" })
|
||||||
|
};
|
||||||
|
};
|
||||||
|
|
||||||
|
curr_menu_entry.act = async () => {
|
||||||
|
const msg_text = await getHighlight();
|
||||||
|
|
||||||
|
//check if a selection is needed
|
||||||
|
if(curr_prompt.need_selected && (msg_text.selection==='')){
|
||||||
|
//A selection is needed, but nothing is selected!
|
||||||
|
//alert(browser.i18n.getMessage('prompt_selection_needed'));
|
||||||
|
const tabs = await browser.tabs.query({ active: true, currentWindow: true });
|
||||||
|
browser.tabs.sendMessage(tabs[0].id, { command: "sendAlert", message : browser.i18n.getMessage('prompt_selection_needed') });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
var body_text = '';
|
||||||
|
if (msg_text.selection!=='') {
|
||||||
|
body_text = msg_text.selection.replace(/\s+/g, ' ').trim();
|
||||||
|
} else {
|
||||||
|
body_text = msg_text.text.replace(/\s+/g, ' ').trim();
|
||||||
|
}
|
||||||
|
//open chatgpt window
|
||||||
|
//console.log("Click menu item...");
|
||||||
|
let prefs = await browser.storage.sync.get({default_chatgpt_lang: getLanguageDisplayName(browser.i18n.getUILanguage())});
|
||||||
|
let chatgpt_lang = prefs.default_chatgpt_lang;
|
||||||
|
|
||||||
|
var fullPrompt = curr_prompt.text + (curr_prompt.need_signature ? " " + await this.getDefaultSignature():"") + " " + browser.i18n.getMessage("prompt_lang") + chatgpt_lang + ". \"" + body_text + "\" ";
|
||||||
|
|
||||||
|
switch(curr_prompt.id){
|
||||||
|
case 'prompt_translate_this':
|
||||||
|
fullPrompt = curr_prompt.text + chatgpt_lang + ". \"" + body_text + "\" ";
|
||||||
|
break;
|
||||||
|
case 'prompt_reply':
|
||||||
|
fullPrompt += "Do not add the subject line to the response."
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
const tabs = await browser.tabs.query({ active: true, currentWindow: true });
|
||||||
|
//browser.runtime.sendMessage({command: "chatgpt_open", prompt: fullPrompt, action: curr_prompt.action, tabId: tabs[0].id});
|
||||||
|
this.openChatGPT(fullPrompt, curr_prompt.action, tabs[0].id);
|
||||||
|
};
|
||||||
|
this.rootMenu.push(curr_menu_entry);
|
||||||
|
};
|
||||||
|
|
||||||
|
async getDefaultSignature(){
|
||||||
|
let prefs = await browser.storage.sync.get({default_sign_name: ''});
|
||||||
|
if(prefs.default_sign_name===''){
|
||||||
|
return '';
|
||||||
|
}else{
|
||||||
|
return "Sign the message as " + prefs.default_sign_name + ".";
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
loadMenus() {
|
||||||
|
this.addMenu(this.rootMenu);
|
||||||
|
let listeners = this.menu_listeners;
|
||||||
|
browser.menus.onClicked.addListener((info, tab) => {
|
||||||
|
listeners[info.menuItemId] && listeners[info.menuItemId] (info, tab);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
addMenu = (menu, root = null) => {
|
||||||
|
for (let item of menu) {
|
||||||
|
let {id, menu, act} = item;
|
||||||
|
|
||||||
|
browser.menus.create({
|
||||||
|
id: id,
|
||||||
|
title: browser.i18n.getMessage(id),
|
||||||
|
contexts: this.getContexts(id),
|
||||||
|
parentId: root
|
||||||
|
});
|
||||||
|
|
||||||
|
if (act) {
|
||||||
|
this.menu_listeners[id] = act;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (menu) {
|
||||||
|
this.addMenu(menu, id);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
};
|
||||||
|
|
||||||
|
getContexts(id){
|
||||||
|
const curr_prompt = defaultPrompts.find(p => p.id === id);
|
||||||
|
if (!curr_prompt) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
switch(curr_prompt.type){
|
||||||
|
case 0:
|
||||||
|
return [this.menu_context_compose, this.menu_context_display];
|
||||||
|
case 1:
|
||||||
|
return [this.menu_context_display];
|
||||||
|
case 2:
|
||||||
|
return [this.menu_context_compose];
|
||||||
|
default:
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -11,14 +11,14 @@
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
"message_display_action": {
|
"message_display_action": {
|
||||||
"default_popup": "messageMenu/mzta-msgmenu.html",
|
|
||||||
"default_title": "__MSG_menu_title__",
|
"default_title": "__MSG_menu_title__",
|
||||||
"default_icon": "images/icon-32px.png"
|
"default_icon": "images/icon-32px.png",
|
||||||
|
"type": "menu"
|
||||||
},
|
},
|
||||||
"compose_action": {
|
"compose_action": {
|
||||||
"default_popup": "messageMenu/mzta-msgmenu.html",
|
|
||||||
"default_title": "__MSG_menu_title__",
|
"default_title": "__MSG_menu_title__",
|
||||||
"default_icon": "images/icon-32px.png"
|
"default_icon": "images/icon-32px.png",
|
||||||
|
"type":"menu"
|
||||||
},
|
},
|
||||||
"permissions": [
|
"permissions": [
|
||||||
"compose",
|
"compose",
|
||||||
|
|
|
||||||
|
|
@ -18,6 +18,7 @@
|
||||||
|
|
||||||
import { mzta_script } from './js/mzta-chatgpt.js';
|
import { mzta_script } from './js/mzta-chatgpt.js';
|
||||||
import { prefs_default } from './options/mzta-options-default.js';
|
import { prefs_default } from './options/mzta-options-default.js';
|
||||||
|
import { mzta_Menus } from './js/mzta-menus.js';
|
||||||
|
|
||||||
var createdWindowID = null;
|
var createdWindowID = null;
|
||||||
|
|
||||||
|
|
@ -41,7 +42,6 @@ for (let messageTab of messageTabs) {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
messenger.runtime.onMessage.addListener(async (message, sender, sendResponse) => {
|
messenger.runtime.onMessage.addListener(async (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.
|
||||||
|
|
@ -169,3 +169,7 @@ function checkScreenDimensions(prefs){
|
||||||
|
|
||||||
return prefs;
|
return prefs;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Menus handling
|
||||||
|
const menus = new mzta_Menus(openChatGPT);
|
||||||
|
menus.loadMenus();
|
||||||
Loading…
Reference in a new issue