From 24ea042d3286126c29a62e035488c206a6752af1 Mon Sep 17 00:00:00 2001 From: buzz-lightsnack-2007 <73412182+buzz-lightsnack-2007@users.noreply.github.com> Date: Sat, 27 Apr 2024 23:25:54 +0800 Subject: [PATCH] split the popup generation from the extension icon --- scripts/GUI/extensionIcon.js | 7 ++-- scripts/GUI/popup.js | 79 ++++++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+), 4 deletions(-) create mode 100644 scripts/GUI/popup.js diff --git a/scripts/GUI/extensionIcon.js b/scripts/GUI/extensionIcon.js index eb651cc..3335aa0 100644 --- a/scripts/GUI/extensionIcon.js +++ b/scripts/GUI/extensionIcon.js @@ -14,10 +14,9 @@ class BrowserIcon { return ((parameters) ? Object.assign(option, parameters) : option); } - (options.Icon) ? chrome.browserAction.setIcon(format({"path": path}, parameters)) : null; - (options.BadgeText) ? chrome.browserAction.setBadgeText(format({"text": String(options.text)}, parameters)) : null; - (options.BadgeBackgroundColor) ? chrome.browserAction.setBadgeBackgroundColor(format({"color": color}, parameters)) : null; - (options.Popup) ? chrome.browserAction.setPopup(format({"popup": popup}, parameters)) : null; + (options.Icon) ? chrome.action.setIcon(format({"path": path}, parameters)) : null; + (options.BadgeText) ? chrome.action.setBadgeText(format({"text": String(options.text).trim()}, parameters)) : null; + (options.BadgeBackgroundColor) ? chrome.action.setBadgeBackgroundColor(format({"color": color}, parameters)) : null; }; /* diff --git a/scripts/GUI/popup.js b/scripts/GUI/popup.js new file mode 100644 index 0000000..4ea7cbf --- /dev/null +++ b/scripts/GUI/popup.js @@ -0,0 +1,79 @@ +/* +popup.js +Manage extension popups. +*/ + +class Popup { + options; // The options for the popup + path; // The URL of the popup + enabled = true; // The popup's enabled state + + /* Create a new pop-up configuration. + + @param {Object} options The options for the popup. If string, this is set to the URL; otherwise, this is passed directly as the options. + */ + constructor (options) { + // Set the side panel options. + this.options = ((typeof options).includes(`str`)) ? { "popup": options } : options; + + // Set the other options not to be directly passed to the Chrome API. + [`hidden`, `enabled`].forEach((key) => { + this[key] = (Object.keys(this.options).length > 0 ? (this.options[key] != null) : false) ? this.options[key] : true; + delete this.options[key]; + }) + + // Set the popup path. + chrome.browserAction.setPopup(this.options); + + // Set the popup state. + this[(this.enabled) ? `enable` : `disable`](); + (!this.hidden && this.hidden != null) ? this.show() : false; + + // Remove untrackable variables. + delete this.hidden; + } + + /* + Open the side panel. + */ + show () { + if (this.enabled) { + // Set the options if in case it was previously overwritten. + chrome.browserAction.setPopup(this.options); + + // Open the pop-up. + chrome.action.openPopup(); + }; + }; + + /* + Disable the popup. + */ + disable () { + chrome.action.disable(); + this.enabled = false; + } + + /* + Enable the popup. + */ + enable () { + chrome.action.enable(); + this.enabled = true; + } + + /* + Set the options. + + @param {object} options the options + */ + setOptions(options) { + // Merge the options. + options = Object.assign(this.options, options); + + // Set the options. + chrome.browserAction.setPopup(options); + } +} + +export {Popup as default} \ No newline at end of file