fixed class referencing

This commit is contained in:
buzz-lightsnack-2007 2024-03-31 15:43:28 +08:00
parent 6a1ea8eb7d
commit 543fe2f913
9 changed files with 383 additions and 377 deletions

View file

@ -13,7 +13,9 @@ export default class logging {
static error(ERROR_CODE, ERROR_MESSAGE, critical = true) { static error(ERROR_CODE, ERROR_MESSAGE, critical = true) {
(async () => { (async () => {
// Import the templating. // Import the templating.
const texts = await import(chrome.runtime.getURL("gui/scripts/read.js")); const texts = (
await import(chrome.runtime.getURL("gui/scripts/read.js"))
)[`texts`];
// Display the error message. // Display the error message.
console.error(texts.read(`error_msg`, [ERROR_CODE, ERROR_MESSAGE])); console.error(texts.read(`error_msg`, [ERROR_CODE, ERROR_MESSAGE]));

View file

@ -1,12 +1,11 @@
import {windowman} from '../../windowman.JS'; import { windowman } from "../../windowman.JS";
function redirect() { function redirect() {
new windowman(`gui/pages/settings.htm`); windowman.new(`gui/pages/settings.htm`);
window.close(); window.close();
} }
function main() { function main() {
redirect(); redirect();
} }

View file

@ -1,14 +1,13 @@
// Open the settings in a pop-up window. // Open the settings in a pop-up window.
import {windowman} from '../../windowman.JS'; import { windowman } from "../../windowman.JS";
function redirect() { function redirect() {
new windowman(`gui/pages/settings.htm`); windowman.new(`gui/pages/settings.htm`);
window.close(); window.close();
} }
function main() { function main() {
redirect(); redirect();
} }

View file

@ -3,13 +3,23 @@
*/ */
// Import modules. // Import modules.
import { windowman } from "../windowman.js"; //import { windowman } from "../windowman.js";
let secretariat = await import(chrome.runtime.getURL("scripts/secretariat.js"));
async function build() {
let secretariat = (
await import(chrome.runtime.getURL("scripts/secretariat.js"))
).secretariat;
let windowman = (
await import(chrome.runtime.getURL("gui/scripts/windowman.js"))
).windowman;
let window = new windowman();
}
/* /*
Arrange the interface. Arrange the interface.
*/ */
function arrange() { /*function arrange() {
async function openLast() { async function openLast() {
let last_opened = ( let last_opened = (
await Promise.all([secretariat.read([`view`, window.location.href], 1)]) await Promise.all([secretariat.read([`view`, window.location.href], 1)])
@ -26,17 +36,24 @@ function arrange() {
openLast(); openLast();
} }
function main() {
windowman.fill();
events();
arrange();
}*/
/* /*
Define the mapping of each button. Define the mapping of each button.
*/ */
function events() { function events() {
windowman.events();
if (document.querySelector(`[data-action="filters,update"]`)) { if (document.querySelector(`[data-action="filters,update"]`)) {
document document
.querySelector(`[data-action="filters,update"]`) .querySelector(`[data-action="filters,update"]`)
.addEventListener(`click`, async () => { .addEventListener(`click`, async () => {
let filters = await import(chrome.runtime.getURL(`scripts/filters.js`)); let filters = (
await import(chrome.runtime.getURL(`scripts/filters.js`))
).default;
filters.update(); filters.update();
}); });
} }
@ -44,19 +61,22 @@ function events() {
document document
.querySelector(`[data-action="storage,clear"]`) .querySelector(`[data-action="storage,clear"]`)
.addEventListener(`click`, async () => { .addEventListener(`click`, async () => {
let storage = await import( let storage = (
chrome.runtime.getURL(`scripts/secretariat.js`) await import(chrome.runtime.getURL(`scripts/secretariat.js`))
); )["secretariat"];
storage.forget(`sites`); storage.forget(`sites`);
}); });
} }
} }
function main() { //main();
windowman.prepare(); function load() {
windowman.fill(); document.addEventListener("DOMContentLoaded", function () {
M.AutoInit();
});
build();
events(); events();
arrange();
} }
main(); load();

View file

@ -1,63 +1,108 @@
/* windowman /* windowman
Window management */ Window and window content management */
import texts from "./read.js"; import texts from "./read.js";
class windowman { class windowman {
/* Initialize the window frame. */ static new(URL, height, width) {
static prepare() {
try {
let UI = {
library: [
chrome.runtime.getURL("gui/styles/interface.external.css"),
chrome.runtime.getURL("gui/styles/ui.css"),
],
script: chrome.runtime.getURL(
"gui/scripts/external/interface.external.js",
),
};
UI.library.forEach((source) => {
$(`head`).append(
`<link rel="stylesheet" type="text/css" href="${source}">`,
);
});
$(`head`).append(`<script type="module" src="${UI.script}"></script>`);
} catch (error) {
console.error(texts.localized(`error_fileNotFound`, [error]));
}
// Prevent scaling, similar to a real window.
$(`head`).append(
`<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />`,
);
}
constructor(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) : 400, width: width ? parseInt(width) : 600,
height: height ? parseInt(height) : 600, height: height ? parseInt(height) : 600,
}); });
} }
static fill() { // Prepare the window with its metadata.
function text() { constructor() {
let text_elements = document.querySelectorAll("[data-text]"); function headers() {
let UI = {
library: [
chrome.runtime.getURL(
"gui/styles/external/mdi/materialdesignicons.min.css",
),
chrome.runtime.getURL(
"gui/styles/external/materialize/css/materialize.css",
),
chrome.runtime.getURL("gui/styles/ui.css"),
],
};
text_elements.forEach((text_element) => { 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.
this[`metadata`] = chrome.windows.getCurrent();
/*
window_metadata[`id`] = window.id;
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. */
function appearance() {
function icons() {
let target_elements = document.querySelectorAll(`[data-icon]`);
target_elements.forEach((element) => {
// Get the content before removing it.
let element_data = {};
// Swap the placement of the existing content.
function swap() {
element_data[`content`] = element.innerHTML;
element.innerHTML = ``;
let element_text = document.createElement(`span`);
element_text.innerHTML = element_data[`content`];
element.appendChild(element_text);
}
// Add the icon.
function iconify() {
// Get the icon.
element_data[`icon`] = element.getAttribute(`data-icon`);
// Get the icon.
let icon_element = document.createElement(`i`);
icon_element.className = `mdi mdi-`.concat(element_data[`icon`]);
element.prepend(icon_element);
}
swap();
iconify();
});
}
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]");
text_elements[`content`].forEach((text_element) => {
let text_inserted = texts.localized( let text_inserted = texts.localized(
text_element.getAttribute(`data-text`), text_element.getAttribute(`for`),
false, false,
text_element.hasAttribute(`data-text-parameter`) text_element.hasAttribute(`for-parameter`)
? text_element.getAttribute(`data-text-parameter`).split(",") ? text_element.getAttribute(`for-parameter`).split(",")
: null, : null,
); );
if (!text_inserted) { if (!text_inserted) {
text_inserted = texts.localized( text_inserted = texts.localized(
`term_`.concat(text_element.getAttribute(`data-text`)), `term_`.concat(text_element.getAttribute(`for`)),
); );
} }
@ -67,10 +112,33 @@ class windowman {
text_element.innerText = text_inserted; text_element.innerText = text_inserted;
} }
}); });
delete text_elements[`content`];
Object.keys(text_elements).forEach((key) => {
if (text_elements[key]) {
text_elements[key].forEach((text_element) => {
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`))),
);
} }
function storage() { text_element.setAttribute(key, text_inserted);
(async () => { });
}
});
}
async function storage() {
// Import the module. // Import the module.
const secretariat = await import( const secretariat = await import(
chrome.runtime.getURL("scripts/secretariat.js") chrome.runtime.getURL("scripts/secretariat.js")
@ -108,18 +176,17 @@ class windowman {
} }
}); });
}); });
})();
} }
text(); text();
icons();
storage(); storage();
} }
/* Add click events. */ // Adds events to the window.
static events() { function events() {
/* Add events related to storage. */ /* Add events related to storage. */
function storage() { async function storage() {
(async () => {
// Import the module. // Import the module.
const secretariat = await import( const secretariat = await import(
chrome.runtime.getURL("scripts/secretariat.js") chrome.runtime.getURL("scripts/secretariat.js")
@ -160,87 +227,6 @@ class windowman {
input_element.addEventListener("change", element[`event`]); input_element.addEventListener("change", element[`event`]);
}); });
})();
}
/* Make it feel more like a native window. */
function functionality() {
/* Adjust the interface based on events. */
function changeUI() {
function tabs() {
let menus = document.querySelectorAll("menu[role=tablist]");
if (menus) {
menus.forEach((menu) => {
let buttons = menu.querySelectorAll("button, a");
if (buttons) {
buttons.forEach((button) => {
let event = function () {
// Prevent selection.
let MENU = this.parentElement;
let BUTTONS = MENU.querySelectorAll("button, a");
BUTTONS.forEach((BUTTON) => {
BUTTON.setAttribute(
`aria-selected`,
String(
BUTTON.getAttribute(`for`) ==
this.getAttribute(`for`),
),
);
});
let CONTAINER = document.getElementById(
MENU.getAttribute(`for`),
);
let SECTIONS = CONTAINER.querySelectorAll(
`#${CONTAINER.id} > section`,
);
SECTIONS.forEach((SECTION) => {
// SECTION.setAttribute(`hidden`, true);
if (!this.getAttribute(`for`).includes(SECTION.id)) {
SECTION.setAttribute(
`hidden`,
this.getAttribute(`for`).includes(SECTION.id),
);
} else {
SECTION.removeAttribute(`hidden`);
}
});
// Save.
(async () => {
const secretariat = await import(
chrome.runtime.getURL("scripts/secretariat.js")
);
// Write the data.
secretariat.write(
[`view`, window.location.href],
parseInt(this.getAttribute(`tab`)),
1,
);
})();
};
button.addEventListener("click", event);
});
}
});
}
}
tabs();
}
document.addEventListener("contextmenu", (event) =>
event.preventDefault(),
);
changeUI();
} }
/* Map buttons to their corresponding action buttons. */ /* Map buttons to their corresponding action buttons. */
@ -252,46 +238,22 @@ class windowman {
buttons.forEach((button) => { buttons.forEach((button) => {
let event = function () { let event = function () {
// Get the data from the button. // Get the data from the button.
let file = {}; let target = {};
file[`target`] = this.getAttribute(`href`); target[`source`] = this.getAttribute(`href`);
// Check if the file exists to only open it when it is the case. // Get the correct path.
function testUrl(URL) { target[`path`] = (
// Code from https://stackoverflow.com/questions/3646914/how-do-i-check-if-file-exists-in-jquery-or-pure-javascript !target[`source`].includes(`://`)
const HTTP = new XMLHttpRequest(); ? window.location.pathname
try { .split(`/`)
HTTP.open(`HEAD`, URL, false); .slice(0, -1)
HTTP.send(); .join(`/`)
return HTTP.status != 404; .concat(`/`)
} catch (error) { : ``
return false; ).concat(target[`source`]);
}
}
if (!file[`target`].includes(`://`)) { windowman.new(
// Get the current path. target[`path`],
file[`path`] = window.location.pathname.split(`/`);
file[`path`] =
file[`path`].slice(0, file[`path`].length - 1).join(`/`) +
`/`;
file[`location`] = file[`path`].concat(file[`target`]);
} else {
file[`location`] = file[`target`];
}
let open_combinations = [``, `.htm`, `.html`];
for (
let open_combination = 0;
open_combination < open_combinations.length;
open_combination++
) {
if (
testUrl(
file[`location`] + open_combinations[open_combination],
)
) {
new windowman(
file[`location`] + open_combinations[open_combination],
this.getAttribute(`tab-height`) this.getAttribute(`tab-height`)
? this.getAttribute(`tab-height`) ? this.getAttribute(`tab-height`)
: null, : null,
@ -299,9 +261,6 @@ class windowman {
? this.getAttribute(`tab-width`) ? this.getAttribute(`tab-width`)
: null, : null,
); );
break;
}
}
}; };
button.addEventListener("click", event); button.addEventListener("click", event);
}); });
@ -352,10 +311,14 @@ class windowman {
} }
storage(); storage();
functionality();
actions(); actions();
updates(); updates();
} }
headers();
appearance();
events();
}
} }
export { windowman }; export { windowman };

View file

@ -2,7 +2,7 @@
This does not stand for "FamiCom" but instead on Finalization and Completion. This script provides installation run scripts. This does not stand for "FamiCom" but instead on Finalization and Completion. This script provides installation run scripts.
*/ */
import { read, write, init } from "./secretariat.js"; import { init } from "./secretariat.js";
let config = chrome.runtime.getURL("config/config.json"); let config = chrome.runtime.getURL("config/config.json");

View file

@ -2,24 +2,36 @@
Manage filters. Manage filters.
*/ */
/* Select the most appropriate filter based on a URL. export default class filters {
constructor() {
this.all = {};
}
@param {string} URL the current URL /* Select the most appropriate filter based on a URL.
*/
export async function select(URL = window.location.href) {}
/* Update all filters or just one. @param {string} URL the current URL
*/
static select(URL = window.location.href) {
this.one = {};
}
@param {string} URL the URL to update /* Update all filters or just one.
@return {boolean} the state
*/ @param {string} URL the URL to update
export async function update(URL) { @return {boolean} the state
*/
static update(URL) {
(async () => {
// Import the updater. // Import the updater.
const secretariat = await import( const secretariat = await import(
chrome.runtime.getURL("scripts/secretariat.js") chrome.runtime.getURL("scripts/secretariat.js")
); );
const net = await import(chrome.runtime.getURL("scripts/net.js")); const net = await import(chrome.runtime.getURL("scripts/net.js"));
const texts = await import(chrome.runtime.getURL("gui/scripts/read.js")); const texts = (await import(chrome.runtime.getURL("gui/scripts/read.js")))
.default;
const alerts = (
await import(chrome.runtime.getURL("gui/scripts/alerts.js"))
).default;
// Apparently, JS doesn't have a native queueing system, but it might best work here. // Apparently, JS doesn't have a native queueing system, but it might best work here.
class Queue { class Queue {
@ -67,8 +79,10 @@ export async function update(URL) {
let filter_URL = filters.dequeue(); let filter_URL = filters.dequeue();
// Inform the user of download state. // Inform the user of download state.
console.log( alerts.log(
texts.read(`settings_filters_update_status`, null, [filter_URL]), texts.localized(`settings_filters_update_status`, null, [
filter_URL,
]),
); );
// Create promise of downloading. // Create promise of downloading.
@ -80,24 +94,29 @@ export async function update(URL) {
// Write the filter to storage. // Write the filter to storage.
secretariat.write(["filters", filter_URL], result, -1); secretariat.write(["filters", filter_URL], result, -1);
console.log( console.log(
texts.read(`settings_filters_update_status_complete`, null, [ texts.localized(
filter_URL, `settings_filters_update_status_complete`,
]), null,
[filter_URL],
),
); );
} }
}) })
.catch((error) => { .catch((error) => {
// Inform the user of the download failure. // Inform the user of the download failure.
console.log( console.log(
texts.read(`settings_filters_update_status_failure`, null, [ texts.localized(
error, `settings_filters_update_status_failure`,
filter_URL, null,
]), [error, filter_URL],
),
); );
}); });
} }
} else { } else {
// Inform the user of the download being unnecessary. // Inform the user of the download being unnecessary.
console.log(texts.read(`settings_filters_update_stop`)); alerts.warn(texts.localized(`settings_filters_update_stop`));
}
})();
} }
} }

View file

@ -27,11 +27,11 @@ export async function read(DATA_NAME, CLOUD = 0, PARAMETER_TEST = null) {
} }
/* /*
Find the data now. Get all dataset.
@param {number} SOURCE the data source @param {number} SOURCE the data source
*/ */
function read_database(SOURCE = -1) { async function read_database(SOURCE = -1) {
let data = {}; let data = {};
let data_returned; let data_returned;
@ -263,7 +263,9 @@ export function forget(preference, subpreference, CLOUD = 0) {
(async () => { (async () => {
// Import alerts module. // Import alerts module.
let alerts = await import(chrome.runtime.getURL(`gui/scripts/alerts.js`)); let alerts = (await import(chrome.runtime.getURL(`gui/scripts/alerts.js`)))[
`alerts`
];
// Confirm the action. // Confirm the action.
let forget_action = alerts.confirm_action(); let forget_action = alerts.confirm_action();
@ -388,7 +390,7 @@ Run a script when the browser storage has been changed.
@param {object} reaction the function to run @param {object} reaction the function to run
*/ */
export async function observe(reaction) { export function observe(reaction) {
chrome.storage.onChanged.addListener((changes, namespace) => { chrome.storage.onChanged.addListener((changes, namespace) => {
reaction(changes, namespace); reaction(changes, namespace);
}); });

View file

@ -7,8 +7,10 @@ Be sensitive to changes and update the state.
let secretariat = await import( let secretariat = await import(
chrome.runtime.getURL("scripts/secretariat.js") chrome.runtime.getURL("scripts/secretariat.js")
); );
let filters = await import(chrome.runtime.getURL("scripts/filters.js")); let filters = (await import(chrome.runtime.getURL("scripts/filters.js")))[
let reader = await import(chrome.runtime.getURL("scripts/reader.js")); `filters`
];
// let reader = await import(chrome.runtime.getURL("scripts/reader.js"));
class watchman { class watchman {
/* Check the current URL. /* Check the current URL.