add action listener definition function in context menus

This commit is contained in:
buzz-lightsnack-2007 2024-04-28 11:05:30 +08:00
parent 1aa887cab1
commit 8f5d6c3858

View file

@ -1,4 +1,6 @@
/* context_menus.js */ /* context_menus.js
Context menu management
*/
export default class Menu { export default class Menu {
#options; #options;
@ -43,7 +45,6 @@ export default class Menu {
this.hidden = false; this.hidden = false;
this.ID = chrome.contextMenus.create(this.#options); this.ID = chrome.contextMenus.create(this.#options);
console.log(Object.keys(this.events));
if (((this.events && (typeof this.events).includes(`obj`) && !Array.isArray(this.events))) ? Object.keys(this.events).length > 0 : false) { if (((this.events && (typeof this.events).includes(`obj`) && !Array.isArray(this.events))) ? Object.keys(this.events).length > 0 : false) {
(Object.keys(this.events)).forEach((EVENT) => { (Object.keys(this.events)).forEach((EVENT) => {
chrome.contextMenus[EVENT].addListener((info, tab) => { chrome.contextMenus[EVENT].addListener((info, tab) => {
@ -87,9 +88,24 @@ export default class Menu {
: false; : false;
} }
/* Run a new function when triggered. */ /*
Run a new function when triggered.
@param {function} callback the function to run
*/
onclick(callback) { onclick(callback) {
this.event = {"onClicked": callback}; this.addActionListener("onClicked", callback);
}
/*
Run an event following an action.
@param {string} event the event
@param {function} callback the function to run
*/
addActionListener(event, callback) {
this.events = (this.events == null) ? {} : this.events;
this.events[event] = callback;
this.update(); this.update();
} };
} }