fix import method to support service worker

This commit is contained in:
buzz-lightsnack-2007 2024-04-14 13:01:12 +08:00
parent cd0384c3d1
commit 7eb0b86640
3 changed files with 77 additions and 60 deletions

View file

@ -2,12 +2,11 @@
Alert management system.
*/
const reader = (await import(chrome.runtime.getURL("gui/scripts/read.js"))).default;
import texts from "/gui/scripts/read.js";
export default class alerts {
static async confirm(MESSAGE) {
let user_response = confirm(
reader.localized((MESSAGE) ? MESSAGE : `GUI_alert_confirm_action_text`),
texts.localized((MESSAGE) ? MESSAGE : `GUI_alert_confirm_action_text`),
);
// Return the user response.
@ -46,13 +45,13 @@ export default class alerts {
*/
static warn(message, critical = false) {
console.warn(message);
if (critical) {
alert(message);
} else {
try {
M.toast({ text: message });
} catch (err) {}
}
if (critical) {
alert(message);
} else {
try {
M.toast({ text: message });
} catch (err) {}
}
}
/*
@ -62,20 +61,18 @@ export default class alerts {
@param {number} ERROR_MESSAGE the custom error message
@param {boolean} critical the critical nature
*/
static error(ERROR_CODE, ERROR_MESSAGE, ERROR_STACK, critical = true) {
(async () => {
// Import the templating.
const texts = (await import(chrome.runtime.getURL("gui/scripts/read.js"))).default;
static async error(ERROR_CODE, ERROR_MESSAGE, ERROR_STACK, critical = true) {
// Import the templating.
const texts = (await import(chrome.runtime.getURL("gui/scripts/read.js"))).default;
// Display the error message.
console.error(texts.localized(`error_msg`, false, [ERROR_CODE, ERROR_MESSAGE, ERROR_STACK]));
if (critical) {
alert(texts.localized(`error_msg_GUI`, false, [String(ERROR_CODE), ERROR_MESSAGE]));
} else {
try {
M.toast({ text: ERROR_MESSAGE });
} catch (err) {};
};
})();
// Display the error message.
console.error(texts.localized(`error_msg`, false, [ERROR_CODE, ERROR_MESSAGE, ERROR_STACK]));
if (critical) {
alert(texts.localized(`error_msg_GUI`, false, [String(ERROR_CODE), ERROR_MESSAGE]));
} else {
try {
M.toast({ text: ERROR_MESSAGE });
} catch (err) {};
};
}
}

View file

@ -3,11 +3,11 @@ This does not stand for "FamiCom" but instead on Finalization and Completion. Th
*/
import { init, read, write } from "./secretariat.js";
import filters from "./filters.js";
let config = chrome.runtime.getURL("config/config.json");
export default class fc {
/* Start the out of the box experience. */
// Start the out of the box experience.
static hello() {
// the OOBE must be in the config.
fetch(config)
@ -26,16 +26,18 @@ export default class fc {
});
}
/* Initialize the configuration. */
// Initialize the configuration.
static setup() {
// the OOBE must be in the config.
fetch(config)
.then((response) => response.json())
.then((jsonData) => {
.then(async (jsonData) => {
// const secretariat = await import(chrome.runtime.getURL("scripts/secretariat.js"));
let configuration = jsonData;
// Run the storage initialization.
init(configuration);
// secretariat.init(configuration);
})
.catch((error) => {
console.error(error);
@ -51,25 +53,42 @@ export default class fc {
});
}
/* main function */
// main function
static run() {
fc.trigger();
fc.every();
}
static async every() {
let DURATION_PREFERENCES = await read([`settings`,`sync`]);
read([`settings`,`sync`]).then(async (DURATION_PREFERENCES) => {
// Forcibly create the preference if it doesn't exist. It's required!
if (!(typeof DURATION_PREFERENCES).includes(`obj`) || DURATION_PREFERENCES == null || Array.isArray(DURATION_PREFERENCES)) {
DURATION_PREFERENCES = {};
DURATION_PREFERENCES[`duration`] = 24;
if (((typeof DURATION_PREFERENCES).includes(`obj`) && DURATION_PREFERENCES != null && !Array.isArray(DURATION_PREFERENCES)) ? ((DURATION_PREFERENCES[`duration`]) ? (DURATION_PREFERENCES[`duration`] > 0) : false) : false) {
// Convert DURATION_PREFERENCES[`duration`]) from hrs to milliseconds.
DURATION_PREFERENCES[`duration`] = DURATION_PREFERENCES[`duration`] * 60 * 60 * 1000;
let filters = new (await import(chrome.runtime.getURL(`scripts/filters.js`))).default();
// Write it.
write([`settings`, `sync`], DURATION_PREFERENCES, -1);
};
if (((typeof DURATION_PREFERENCES).includes(`obj`) && DURATION_PREFERENCES != null && !Array.isArray(DURATION_PREFERENCES)) ? ((DURATION_PREFERENCES[`duration`]) ? (DURATION_PREFERENCES[`duration`] > 0) : false) : false) {
// Convert DURATION_PREFERENCES[`duration`]) from hrs to milliseconds.
DURATION_PREFERENCES[`duration`] = DURATION_PREFERENCES[`duration`] * 60 * 60 * 1000;
let FILTERS = new filters;
// Now, set the interval.
let updater_set = () => {
setInterval(async () => {
// Update the filters.
filters.update();
}, DURATION_PREFERENCES[`duration`]);
};
// Provide a way to cancel the interval.
let updater_cancel = () => {
}
};
})
// Now, set the interval.
setInterval(async () => {
// Update the filters.
filters.update();
}, DURATION_PREFERENCES[`duration`]);
};
};
}

View file

@ -2,16 +2,17 @@
Manage filters.
*/
const secretariat = await import(chrome.runtime.getURL("scripts/secretariat.js"));
const net = await import(chrome.runtime.getURL("scripts/net.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;
const common = (await import(chrome.runtime.getURL("scripts/common.js")));
import {read, write} from "./secretariat.js";
import {download} from "./net.js";
import texts from "/gui/scripts/read.js";
import {Queue} from "./common.js";
import alerts from "/gui/scripts/alerts.js"
// const alerts = (await import(chrome.runtime.getURL("gui/scripts/alerts.js"))).default;
export default class filters {
constructor() {
this.all = async () => {
return secretariat.read(`filters`, -1).then((filters) => {
return read(`filters`, -1).then((filters) => {
return filters;
});
};
@ -24,13 +25,13 @@ export default class filters {
async select(URL = window.location.href) {
this.one = await (async () => {
// Get the filters.
let filter = await secretariat.search(`filters`, -1, [`URL`]);
let filter = await search(`filters`, -1, [`URL`]);
// If there are filters, then filter the URL.
return filter;
})();
return this.one;
return (this.one);
}
/* Update all filters or just one.
@ -40,7 +41,7 @@ export default class filters {
*/
async update(URL) {
// Create a queue of the filters.
let filters = new common.Queue();
let filters = new Queue();
if (URL) {
// Check if the URL is in a valid protocol
@ -50,9 +51,9 @@ export default class filters {
}
} else {
// Add every item to the queue based on what was loaded first.
if (await secretariat.read(`filters`, -1)) {
for (let FILTER_URL_INDEX = 0; FILTER_URL_INDEX < Object.keys(await secretariat.read(`filters`, -1)).length; FILTER_URL_INDEX++) {
let FILTER_URL = Object.keys(await secretariat.read([`settings`, `filters`], 1))[FILTER_URL_INDEX];
if (await read(`filters`, -1)) {
for (let FILTER_URL_INDEX = 0; FILTER_URL_INDEX < Object.keys(await read(`filters`, -1)).length; FILTER_URL_INDEX++) {
let FILTER_URL = Object.keys(await read([`settings`, `filters`], 1))[FILTER_URL_INDEX];
if (FILTER_URL.includes(`://`)) {
filters.enqueue(FILTER_URL);
}
@ -68,18 +69,18 @@ export default class filters {
new alerts (texts.localized(`settings_filters_update_status`, null, [filter_URL]));
// Create promise of downloading.
let filter_download = net.download(filter_URL, `JSON`, false, true);
let filter_download = download(filter_URL, `JSON`, false, true);
filter_download
.then(async function (result) {
// Only work when the filter is valid.
if (result) {
// Write the filter to storage.
secretariat.write(["filters", filter_URL], result, -1);
write(["filters", filter_URL], result, -1);
alerts.log(texts.localized(`settings_filters_update_status_complete`,null,[filter_URL]));
// Add the filter to the sync list.
if ((await secretariat.read(["settings", `filters`, filter_URL], 1)) == null) {
secretariat.write(["settings", `filters`, filter_URL], true, 1);
if ((await read(["settings", `filters`, filter_URL], 1)) == null) {
write(["settings", `filters`, filter_URL], true, 1);
}
}
})
@ -94,7 +95,7 @@ export default class filters {
}
// Regardless of the download result, update will also mean setting the filters object to whatever is in storage.
this.all = await secretariat.read(`filters`, -1);
this.all = await read(`filters`, -1);
return this.all;
}
@ -105,7 +106,7 @@ export default class filters {
*/
async remove(URL) {
if (URL.includes(`://`)) {
return (await secretariat.forget([`filters`, URL], -1, false)) ? await secretariat.forget([`settings`, `filters`, URL], 1, true) : false;
return (await forget([`filters`, URL], -1, false)) ? await forget([`settings`, `filters`, URL], 1, true) : false;
} else {
// Inform the user of the removal being unnecessary.
alerts.warn(texts.localized(`settings_filters_removal_stop`));