From 8f5d6c38587bf2ae41baac049aa87872455066b7 Mon Sep 17 00:00:00 2001 From: buzz-lightsnack-2007 <73412182+buzz-lightsnack-2007@users.noreply.github.com> Date: Sun, 28 Apr 2024 11:05:30 +0800 Subject: [PATCH] add action listener definition function in context menus --- scripts/GUI/menus.js | 26 +++++++++++++++++++++----- 1 file changed, 21 insertions(+), 5 deletions(-) diff --git a/scripts/GUI/menus.js b/scripts/GUI/menus.js index 67f2730..ded176e 100644 --- a/scripts/GUI/menus.js +++ b/scripts/GUI/menus.js @@ -1,4 +1,6 @@ -/* context_menus.js */ +/* context_menus.js +Context menu management +*/ export default class Menu { #options; @@ -43,7 +45,6 @@ export default class Menu { this.hidden = false; 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) { (Object.keys(this.events)).forEach((EVENT) => { chrome.contextMenus[EVENT].addListener((info, tab) => { @@ -87,9 +88,24 @@ export default class Menu { : false; } - /* Run a new function when triggered. */ + /* + Run a new function when triggered. + + @param {function} callback the function to run + */ onclick(callback) { - this.event = {"onClicked": callback}; - this.update(); + 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(); + }; } \ No newline at end of file