rewrite the window creation to use Chrome APIs

Add properties to show and hide the window. The object is set to have the configuration of that window.
This commit is contained in:
buzz-lightsnack-2007 2024-04-28 18:10:30 +08:00
parent 093511e738
commit 780b99785e

View file

@ -1,41 +1,64 @@
/* different from windowman.js, just creates window management. */
export default class Window {
#features;
#options;
constructor(url, name, options) {
this.url = url;
this.name = name;
constructor(url, options) {
this.url = url;
if ((typeof options).includes(`obj`)) {
this.options = options;
this.#features = ``;
if ((typeof options).includes(`obj`) && options != null) {
this.#options = options;
(Object.keys(options)).forEach((OPTION) => {
this[OPTION] = options[OPTION];
});
}
let FEATURES = Object.keys(options);
for (let i = 0; i < FEATURES.length; i++) {
this.#features = this.#features.concat(`${FEATURES[i]}=${options[FEATURES[i]]}`).concat((i == Object.keys(options).length - 1) ? `` : `,`);
}
}
// Check if the URL starts with a valid protocol. If not, it is most likely an extension page.
if (!(this.url.startsWith(`http`) && this.url.contains(`://`))) {
this.url = chrome.runtime.getURL(this.url);
}
this.options = options;
this.#options = (this.#options) ? this.#options : {};
this.#options[`url`] = (this.url != this.#options[`url`]) ? this.url : this.#options[`url`];
this.show();
}
// Remove options only readable here and not in the API.
(this.hidden != null) ? delete this.#options.hidden : this.hidden = false;
show() {
this.window = window.open(this.url, this.name, this.options);
};
// Show the window if it's not hidden.
(!this.hidden) ? this.show() : false;
}
/* Create an action listener.
/*
Show the window.
*/
show() {
chrome.windows.create(this.#options, (window) => {
this.ID = window.id;
});
};
@param {string} event the event to listen for
@param {function} callback the callback function
*/
static addActionListener(event, callback) {
// Correct possible syntax error on "on" prefix.
event = (!event.includes(`on`)) ? `on${event}` : event;
// Add the event.
chrome.windows[event].addListener(callback);
}
/*
Hide the window.
*/
hide() {
// Close the window if it is still defined.
(this.ID) ? chrome.windows.remove(this.ID) : false;
// Remove the ID.
delete this.ID;
}
/* Create an action listener.
@param {string} event the event to listen for
@param {function} callback the callback function
*/
static addActionListener(event, callback) {
// Correct possible syntax error on "on" prefix.
event = (!event.includes(`on`)) ? `on${event}` : event;
// Add the event.
chrome.windows[event].addListener(callback);
}
};