fixed correct reloading of the listener. see #12

This commit is contained in:
mic 2024-05-14 17:48:40 +02:00
parent e2c99e5b3e
commit 6818414184

View file

@ -38,10 +38,14 @@ export class mzta_Menus {
this.menu_context_display = getMenuContextDisplay(); this.menu_context_display = getMenuContextDisplay();
this.openChatGPT = openChatGPT; this.openChatGPT = openChatGPT;
this.allPrompts = []; this.allPrompts = [];
this.listener = this.listener.bind(this);
} }
async initialize() { async initialize() {
this.allPrompts = [];
this.rootMenu = [];
this.menu_listeners = {};
this.allPrompts = await getPrompts(true); this.allPrompts = await getPrompts(true);
this.allPrompts.forEach((prompt) => { this.allPrompts.forEach((prompt) => {
this.addAction(prompt) this.addAction(prompt)
@ -52,8 +56,7 @@ export class mzta_Menus {
await browser.menus.removeAll().catch(error => { await browser.menus.removeAll().catch(error => {
console.error(">>>>>>>> ERROR removing the menus: ", error); console.error(">>>>>>>> ERROR removing the menus: ", error);
}); });
this.allPrompts = []; this.removeClickListener();
this.rootMenu = [];
this.loadMenus(); this.loadMenus();
} }
@ -125,10 +128,22 @@ export class mzta_Menus {
async loadMenus() { async loadMenus() {
await this.initialize(); await this.initialize();
await this.addMenu(this.rootMenu); await this.addMenu(this.rootMenu);
this.addClickListener();
}
listener(info, tab) {
let listeners = this.menu_listeners; let listeners = this.menu_listeners;
browser.menus.onClicked.addListener((info, tab) => { if (listeners[info.menuItemId]) {
listeners[info.menuItemId] && listeners[info.menuItemId] (info, tab); listeners[info.menuItemId](info, tab);
}); }
}
addClickListener() {
browser.menus.onClicked.addListener(this.listener);
}
removeClickListener() {
browser.menus.onClicked.removeListener(this.listener);
} }
addMenu = async (menu, root = null) => { addMenu = async (menu, root = null) => {