crash the window when the building is wrong

This commit is contained in:
buzzcode2007 2024-04-01 14:57:57 +08:00
parent 42d2278e6c
commit 493abd746a

View file

@ -3,322 +3,348 @@ Window and window content management */
import texts from "./read.js"; import texts from "./read.js";
// MAKE SURE TO TURN THIS OFF DURING BUILD.
let DEBUG = false;
class windowman { class windowman {
static new(URL, height, width) { static new(URL, height, width) {
this.window = chrome.windows.create({ this.window = chrome.windows.create({
url: chrome.runtime.getURL(URL), url: chrome.runtime.getURL(URL),
type: "popup", type: "popup",
width: width ? parseInt(width) : 600, width: width ? parseInt(width) : 600,
height: height ? parseInt(height) : 600, height: height ? parseInt(height) : 600,
}); });
} }
// Prepare the window with its metadata. // Prepare the window with its metadata.
constructor() { constructor() {
function headers() { function headers() {
let UI = { let LOAD_STATE = true;
library: [ let UI = {
chrome.runtime.getURL( "CSS": [
"gui/styles/external/mdi/materialdesignicons.min.css", chrome.runtime.getURL(
), "gui/styles/external/fonts/materialdesignicons.min.css",
chrome.runtime.getURL( ),
"gui/styles/external/materialize/css/materialize.css", chrome.runtime.getURL(
), "gui/styles/external/materialize/css/materialize.css",
chrome.runtime.getURL("gui/styles/ui.css"), ),
], chrome.runtime.getURL("gui/styles/ui.css"),
}; ],
};
UI.library.forEach((source) => {
let metadata_element = document.createElement(`link`);
metadata_element.setAttribute(`rel`, `stylesheet`);
metadata_element.setAttribute(`type`, `text/css`);
metadata_element.setAttribute(`href`, source);
document.querySelector(`head`).appendChild(metadata_element);
});
}
// Get the window. for (let index = 0; index < UI[`CSS`].length; index++) {
this[`metadata`] = chrome.windows.getCurrent(); const source = UI.CSS[index];
/* (async () => {
window_metadata[`id`] = window.id; const net = await import(chrome.runtime.getURL(`/scripts/net.js`));
window_metadata[`focused`] = window.focused;
window_metadata[`state`] = window.state;
window_metadata[`type`] = window.type;
window_metadata[`incognito`] = window.incognito;
window_metadata[`alwaysOnTop`] = window.alwaysOnTop;
window_metadata[`sessionId`] = window.sessionId;
window_metadata[`tabs`] = window.tabs;*/
/* Fill in data and events. */ let resource = false;
function appearance() { try {
function icons() { resource = await net.download(source, `text`, true);
let target_elements = document.querySelectorAll(`[data-icon]`); } catch(err) {};
target_elements.forEach((element) => { if (resource) {
// Get the content before removing it. let metadata_element = document.createElement(`link`);
let element_data = {}; metadata_element.setAttribute(`rel`, `stylesheet`);
metadata_element.setAttribute(`type`, `text/css`);
metadata_element.setAttribute(`href`, source);
document.querySelector(`head`).appendChild(metadata_element);
} else {
const alerts = (await import(chrome.runtime.getURL(`/gui/scripts/alerts.js`))).default;
const reader = (await import(chrome.runtime.getURL(`/gui/scripts/read.js`))).default;
// Swap the placement of the existing content. alerts.error(-1, reader.localized(`error_msg_fileNotFound`), true, [source]);
function swap() {
element_data[`content`] = element.innerHTML;
element.innerHTML = ``;
let element_text = document.createElement(`span`); // Stop loading the page when an error has occured; it's not going to work!
element_text.innerHTML = element_data[`content`]; if (!DEBUG) {window.close()};
}
})();
}
}
element.appendChild(element_text); // Get the window.
} this[`metadata`] = chrome.windows.getCurrent();
// Add the icon. /*
function iconify() { window_metadata[`id`] = window.id;
// Get the icon. window_metadata[`focused`] = window.focused;
element_data[`icon`] = element.getAttribute(`data-icon`); window_metadata[`state`] = window.state;
window_metadata[`type`] = window.type;
window_metadata[`incognito`] = window.incognito;
window_metadata[`alwaysOnTop`] = window.alwaysOnTop;
window_metadata[`sessionId`] = window.sessionId;
window_metadata[`tabs`] = window.tabs;*/
// Get the icon. /* Fill in data and events. */
let icon_element = document.createElement(`i`); function appearance() {
icon_element.className = `mdi mdi-`.concat(element_data[`icon`]); function icons() {
element.prepend(icon_element); let target_elements = document.querySelectorAll(`[data-icon]`);
}
swap(); target_elements.forEach((element) => {
iconify(); // Get the content before removing it.
}); let element_data = {};
}
function text() { // Swap the placement of the existing content.
let text_elements = {}; function swap() {
text_elements[`content`] = document.querySelectorAll("[for]"); element_data[`content`] = element.innerHTML;
text_elements[`alt`] = document.querySelectorAll("[alt-for]"); element.innerHTML = ``;
text_elements[`title`] = document.querySelectorAll("[title-for]");
text_elements[`content`].forEach((text_element) => { let element_text = document.createElement(`span`);
let text_inserted = texts.localized( element_text.innerHTML = element_data[`content`];
text_element.getAttribute(`for`),
false,
text_element.hasAttribute(`for-parameter`)
? text_element.getAttribute(`for-parameter`).split(",")
: null,
);
if (!text_inserted) {
text_inserted = texts.localized(
`term_`.concat(text_element.getAttribute(`for`)),
);
}
if (text_element.tagName.toLowerCase().includes(`input`)) { element.appendChild(element_text);
text_element.setAttribute(`placholder`, text_inserted); }
} else {
text_element.innerText = text_inserted;
}
});
delete text_elements[`content`]; // Add the icon.
Object.keys(text_elements).forEach((key) => { function iconify() {
if (text_elements[key]) { // Get the icon.
text_elements[key].forEach((text_element) => { element_data[`icon`] = element.getAttribute(`data-icon`);
let text_inserted = texts.localized(
text_element.getAttribute(key.concat(`-for`)),
false,
text_element.hasAttribute(key.concat(`for-parameter`))
? text_element
.getAttribute(key.concat(`for-parameter`))
.split(",")
: null,
);
if (!text_inserted) {
text_inserted = texts.localized(
`term_`.concat(text_element.getAttribute(key.concat(`-for`))),
);
}
text_element.setAttribute(key, text_inserted); // Get the icon.
}); let icon_element = document.createElement(`i`);
} icon_element.className = `mdi mdi-`.concat(element_data[`icon`]);
}); element.prepend(icon_element);
} }
async function storage() { swap();
// Import the module. iconify();
const secretariat = await import( });
chrome.runtime.getURL("scripts/secretariat.js") }
);
let input_elements = document.querySelectorAll("[data-store]"); function text() {
let text_elements = {};
text_elements[`content`] = document.querySelectorAll("[for]");
text_elements[`alt`] = document.querySelectorAll("[alt-for]");
text_elements[`title`] = document.querySelectorAll("[title-for]");
input_elements.forEach((input_element) => { text_elements[`content`].forEach((text_element) => {
// Gather data about the element. let text_inserted = texts.localized(
// Get the corresponding storage data. text_element.getAttribute(`for`),
let data = {}; false,
data[`source`] = input_element.getAttribute(`data-store`); text_element.hasAttribute(`for-parameter`)
data[`value`] = secretariat.read(data[`source`], -1); ? text_element.getAttribute(`for-parameter`).split(",")
: null,
);
if (!text_inserted) {
text_inserted = texts.localized(
`term_`.concat(text_element.getAttribute(`for`)),
);
}
data[`value`].then((value) => { if (text_element.tagName.toLowerCase().includes(`input`)) {
switch (input_element.getAttribute(`type`).toLowerCase()) { text_element.setAttribute(`placholder`, text_inserted);
case `checkbox`: } else {
input_element.checked = value; text_element.innerText = text_inserted;
break; }
case `progress`: });
case `range`:
// Ensure that it is a positive floating-point number.
value = !value ? 0 : Math.abs(parseFloat(value));
if (value > 100) {
value = value / 100;
}
// Set the attribute of the progress bar. delete text_elements[`content`];
input_element.setAttribute(`value`, value); Object.keys(text_elements).forEach((key) => {
input_element.setAttribute(`max`, 1); if (text_elements[key]) {
break; text_elements[key].forEach((text_element) => {
default: let text_inserted = texts.localized(
input_element.value = value ? value : ``; text_element.getAttribute(key.concat(`-for`)),
break; false,
} text_element.hasAttribute(key.concat(`for-parameter`))
}); ? text_element
}); .getAttribute(key.concat(`for-parameter`))
} .split(",")
: null,
);
if (!text_inserted) {
text_inserted = texts.localized(
`term_`.concat(text_element.getAttribute(key.concat(`-for`))),
);
}
text(); text_element.setAttribute(key, text_inserted);
icons(); });
storage(); }
} });
}
// Adds events to the window. async function storage() {
function events() { // Import the module.
/* Add events related to storage. */ const secretariat = await import(
async function storage() { chrome.runtime.getURL("scripts/secretariat.js")
// Import the module. );
const secretariat = await import(
chrome.runtime.getURL("scripts/secretariat.js")
);
let input_elements = document.querySelectorAll("[data-store]"); let input_elements = document.querySelectorAll("[data-store]");
input_elements.forEach((input_element) => { input_elements.forEach((input_element) => {
// Gather data about the element. // Gather data about the element.
// Get the corresponding storage data. // Get the corresponding storage data.
let data = {};
data[`source`] = input_element.getAttribute(`data-store`);
data[`value`] = secretariat.read(data[`source`], -1);
let element = {}; data[`value`].then((value) => {
element[`type`] = input_element.getAttribute(`type`).toLowerCase(); switch (input_element.getAttribute(`type`).toLowerCase()) {
element[`event`] = function () {}; case `checkbox`:
input_element.checked = value;
break;
case `progress`:
case `range`:
// Ensure that it is a positive floating-point number.
value = !value ? 0 : Math.abs(parseFloat(value));
if (value > 100) {
value = value / 100;
}
switch (element[`type`]) { // Set the attribute of the progress bar.
case `checkbox`: input_element.setAttribute(`value`, value);
element[`event`] = function () { input_element.setAttribute(`max`, 1);
let UI_item = {}; break;
UI_item[`source`] = this.getAttribute(`data-store`); default:
UI_item[`value`] = this.checked; input_element.value = value ? value : ``;
secretariat.write(UI_item[`source`], UI_item[`value`]); break;
}; }
break; });
default: });
element[`event`] = function () { }
let UI_item = {};
UI_item[`source`] = this.getAttribute(`data-store`);
UI_item[`value`] = element[`type`].includes(`num`)
? parseFloat(this.value) % 1 != 0
? parseFloat(this.value)
: parseInt(this.value)
: this.value;
secretariat.write(UI_item[`source`], UI_item[`value`]);
};
break;
}
input_element.addEventListener("change", element[`event`]); text();
}); icons();
} storage();
}
/* Map buttons to their corresponding action buttons. */ // Adds events to the window.
function actions() { function events() {
function links() { /* Add events related to storage. */
let buttons = document.querySelectorAll("button[href]"); async function storage() {
// Import the module.
const secretariat = await import(
chrome.runtime.getURL("scripts/secretariat.js")
);
if (buttons) { let input_elements = document.querySelectorAll("[data-store]");
buttons.forEach((button) => {
let event = function () {
// Get the data from the button.
let target = {};
target[`source`] = this.getAttribute(`href`);
// Get the correct path. input_elements.forEach((input_element) => {
target[`path`] = ( // Gather data about the element.
!target[`source`].includes(`://`) // Get the corresponding storage data.
? window.location.pathname
.split(`/`)
.slice(0, -1)
.join(`/`)
.concat(`/`)
: ``
).concat(target[`source`]);
windowman.new( let element = {};
target[`path`], element[`type`] = input_element.getAttribute(`type`).toLowerCase();
this.getAttribute(`tab-height`) element[`event`] = function () {};
? this.getAttribute(`tab-height`)
: null,
this.getAttribute(`tab-width`)
? this.getAttribute(`tab-width`)
: null,
);
};
button.addEventListener("click", event);
});
}
}
links(); switch (element[`type`]) {
} case `checkbox`:
element[`event`] = function () {
let UI_item = {};
UI_item[`source`] = this.getAttribute(`data-store`);
UI_item[`value`] = this.checked;
secretariat.write(UI_item[`source`], UI_item[`value`]);
};
break;
default:
element[`event`] = function () {
let UI_item = {};
UI_item[`source`] = this.getAttribute(`data-store`);
UI_item[`value`] = element[`type`].includes(`num`)
? parseFloat(this.value) % 1 != 0
? parseFloat(this.value)
: parseInt(this.value)
: this.value;
secretariat.write(UI_item[`source`], UI_item[`value`]);
};
break;
}
/* input_element.addEventListener("change", element[`event`]);
Update the interface based on the storage data changes. });
*/ }
async function updates() {
// Import the module.
const secretariat = await import(
chrome.runtime.getURL("scripts/secretariat.js")
);
// Get the storage data. /* Map buttons to their corresponding action buttons. */
let storage_data = await secretariat.read(); function actions() {
function links() {
let buttons = document.querySelectorAll("button[href]");
async function update_interface() { if (buttons) {
let input_elements = document.querySelectorAll("[data-enable]"); buttons.forEach((button) => {
let event = function () {
// Get the data from the button.
let target = {};
target[`source`] = this.getAttribute(`href`);
if (input_elements) { // Get the correct path.
input_elements.forEach((input_element) => { target[`path`] = (
if (input_element.getAttribute("data-enable")) { !target[`source`].includes(`://`)
(async () => { ? window.location.pathname
input_element.disabled = .split(`/`)
(await secretariat.read( .slice(0, -1)
input_element.getAttribute("data-enable"), .join(`/`)
)) == null || .concat(`/`)
(await secretariat.read( : ``
input_element.getAttribute("data-enable"), ).concat(target[`source`]);
));
})();
}
});
}
}
// Update the input elements. windowman.new(
secretariat.observe((what) => { target[`path`],
update_interface(); this.getAttribute(`tab-height`)
}); ? this.getAttribute(`tab-height`)
: null,
this.getAttribute(`tab-width`)
? this.getAttribute(`tab-width`)
: null,
);
};
button.addEventListener("click", event);
});
}
}
update_interface(); links();
} }
storage(); /*
actions(); Update the interface based on the storage data changes.
updates(); */
} async function updates() {
// Import the module.
const secretariat = await import(
chrome.runtime.getURL("scripts/secretariat.js")
);
headers(); // Get the storage data.
appearance(); let storage_data = await secretariat.read();
events();
} async function update_interface() {
let input_elements = document.querySelectorAll("[data-enable]");
if (input_elements) {
input_elements.forEach((input_element) => {
if (input_element.getAttribute("data-enable")) {
(async () => {
input_element.disabled =
(await secretariat.read(
input_element.getAttribute("data-enable"),
)) == null ||
(await secretariat.read(
input_element.getAttribute("data-enable"),
));
})();
}
});
}
}
// Update the input elements.
secretariat.observe((what) => {
update_interface();
});
update_interface();
}
storage();
actions();
updates();
}
headers();
appearance();
events();
}
} }
export { windowman }; export { windowman };