From 7eb0b8664085c298fc0b588a1233c6474c232eb0 Mon Sep 17 00:00:00 2001 From: buzz-lightsnack-2007 <73412182+buzz-lightsnack-2007@users.noreply.github.com> Date: Sun, 14 Apr 2024 13:01:12 +0800 Subject: [PATCH] fix import method to support service worker --- gui/scripts/alerts.js | 45 +++++++++++++++++------------------ scripts/fc.js | 55 +++++++++++++++++++++++++++++-------------- scripts/filters.js | 37 +++++++++++++++-------------- 3 files changed, 77 insertions(+), 60 deletions(-) diff --git a/gui/scripts/alerts.js b/gui/scripts/alerts.js index c518998..00e2d63 100644 --- a/gui/scripts/alerts.js +++ b/gui/scripts/alerts.js @@ -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) {}; + }; } } diff --git a/scripts/fc.js b/scripts/fc.js index 0addce3..10c1332 100644 --- a/scripts/fc.js +++ b/scripts/fc.js @@ -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; + + // 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 = () => { + + } + }; + }) - 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(); - - // Now, set the interval. - setInterval(async () => { - // Update the filters. - filters.update(); - }, DURATION_PREFERENCES[`duration`]); - }; }; } diff --git a/scripts/filters.js b/scripts/filters.js index bfd8649..9ab8ae8 100644 --- a/scripts/filters.js +++ b/scripts/filters.js @@ -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`));