split the popup generation from the extension icon

This commit is contained in:
buzz-lightsnack-2007 2024-04-27 23:25:54 +08:00
parent 6644a4d267
commit 24ea042d32
2 changed files with 82 additions and 4 deletions

View file

@ -14,10 +14,9 @@ class BrowserIcon {
return ((parameters) ? Object.assign(option, parameters) : option); return ((parameters) ? Object.assign(option, parameters) : option);
} }
(options.Icon) ? chrome.browserAction.setIcon(format({"path": path}, parameters)) : null; (options.Icon) ? chrome.action.setIcon(format({"path": path}, parameters)) : null;
(options.BadgeText) ? chrome.browserAction.setBadgeText(format({"text": String(options.text)}, parameters)) : null; (options.BadgeText) ? chrome.action.setBadgeText(format({"text": String(options.text).trim()}, parameters)) : null;
(options.BadgeBackgroundColor) ? chrome.browserAction.setBadgeBackgroundColor(format({"color": color}, parameters)) : null; (options.BadgeBackgroundColor) ? chrome.action.setBadgeBackgroundColor(format({"color": color}, parameters)) : null;
(options.Popup) ? chrome.browserAction.setPopup(format({"popup": popup}, parameters)) : null;
}; };
/* /*

79
scripts/GUI/popup.js Normal file
View file

@ -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}