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,31 +1,54 @@
/* different from windowman.js, just creates window management. */ /* different from windowman.js, just creates window management. */
export default class Window { export default class Window {
#features; #options;
constructor(url, name, options) { constructor(url, options) {
this.url = url; this.url = url;
this.name = name;
if ((typeof options).includes(`obj`)) { if ((typeof options).includes(`obj`) && options != null) {
this.options = options; this.#options = options;
this.#features = ``; (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) ? `` : `,`);
}
} }
this.options = options; // 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.show(); this.url = chrome.runtime.getURL(this.url);
} }
this.#options = (this.#options) ? this.#options : {};
this.#options[`url`] = (this.url != this.#options[`url`]) ? this.url : this.#options[`url`];
// Remove options only readable here and not in the API.
(this.hidden != null) ? delete this.#options.hidden : this.hidden = false;
// Show the window if it's not hidden.
(!this.hidden) ? this.show() : false;
}
/*
Show the window.
*/
show() { show() {
this.window = window.open(this.url, this.name, this.options); chrome.windows.create(this.#options, (window) => {
this.ID = window.id;
});
}; };
/*
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. /* Create an action listener.
@param {string} event the event to listen for @param {string} event the event to listen for