From 7fa035ad2ae352a3f8f94fe0367127bfde545a78 Mon Sep 17 00:00:00 2001 From: buzz-lightsnack-2007 <73412182+buzz-lightsnack-2007@users.noreply.github.com> Date: Sat, 8 Jun 2024 09:07:35 +0800 Subject: [PATCH 01/16] grant access to all extension-bundled filters --- src/manifest.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/manifest.json b/src/manifest.json index b058623..ef5d392 100644 --- a/src/manifest.json +++ b/src/manifest.json @@ -21,7 +21,7 @@ "web_accessible_resources": [ { "matches": ["http://*/*", "https://*/*"], - "resources": ["scripts/*.js", "scripts/platform/*.js"] + "resources": ["scripts/*.js", "scripts/platform/*.js", "config/filters/*.filter"] } ], From a2cb524edd5517de0d2cc5ab97c798adf4350747 Mon Sep 17 00:00:00 2001 From: buzz-lightsnack-2007 <73412182+buzz-lightsnack-2007@users.noreply.github.com> Date: Sat, 8 Jun 2024 09:33:41 +0800 Subject: [PATCH 02/16] Fix reading global data for duration --- src/scripts/background/check.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/scripts/background/check.js b/src/scripts/background/check.js index 8a90503..1a6b90c 100644 --- a/src/scripts/background/check.js +++ b/src/scripts/background/check.js @@ -54,8 +54,8 @@ export default class BackgroundCheck { const updater_run = async () => { filter.update(); - // Update the last time. - return(await global.write([`settings`,`sync`,`last`], Date.now(), -1)); + // Update the last update time. + return(await global.write([`settings`,`sync`,`last`], Date.now(), -1, {"verify": false})); }; // Set the interval. @@ -87,7 +87,7 @@ export default class BackgroundCheck { if ((await global.read([`settings`, `sync`, `duration`])) ? (await global.read([`settings`, `sync`, `duration`] * (60 ** 2) * 1000 != DURATION_PREFERENCES[`duration`])) : false) { if (await global.global.read([`settings`, `sync`, `duration`])) { // Get the new time. - DURATION_PREFERENCES[`duration`] = await global.global.read([`settings`, `sync`, `duration`]) * (60 ** 2) * 1000; + DURATION_PREFERENCES[`duration`] = await global.read([`settings`, `sync`, `duration`]) * (60 ** 2) * 1000; // Reset the updater. updater_reset(); From 296c9af17227d67805f70aa8978e800996a880a7 Mon Sep 17 00:00:00 2001 From: buzz-lightsnack-2007 <73412182+buzz-lightsnack-2007@users.noreply.github.com> Date: Sat, 8 Jun 2024 09:34:48 +0800 Subject: [PATCH 03/16] Add support for bundling filters locally --- src/scripts/background/importer.js | 43 +++++++++++++++++++++++++----- 1 file changed, 37 insertions(+), 6 deletions(-) diff --git a/src/scripts/background/importer.js b/src/scripts/background/importer.js index b372509..b08b3b4 100644 --- a/src/scripts/background/importer.js +++ b/src/scripts/background/importer.js @@ -6,6 +6,8 @@ This script provides installation run scripts. // File importation import {template, global} from "../secretariat.js"; import pointer from "../data/pointer.js"; +import {URLs} from "../utils/URLs.js"; +import net from "../utils/net.js"; // The URL for the configuration file const config = chrome.runtime.getURL("config/config.json"); @@ -41,21 +43,50 @@ export default class BackgroundImporter { .then(async (jsonData) => { let configuration = jsonData; - // Run the storage initialization. - delete configuration[`OOBE`]; - template.set(configuration); + if ((configuration) ? Object.keys(configuration).length : false) { + // Run the storage initialization. + delete configuration[`OOBE`]; + + // Replace local URLs of filters to corresponding chrome extension pages. + if (((typeof configuration[`settings`][`filters`]).includes(`obj`) && configuration[`settings`][`filters`]) ? Object.keys(configuration[`settings`][`filters`]).length : false) { + for (let FILTER_NUMBER = 0; FILTER_NUMBER < Object.keys(configuration[`settings`][`filters`]).length; FILTER_NUMBER++) { + let SOURCE = (Object.keys(configuration[`settings`][`filters`]))[FILTER_NUMBER]; + + // Check if the URL is invalid. + + if (!(URLs.test(SOURCE))) { + // Set the URL. + let ORIGIN = {"raw": SOURCE}; + + // If it is, it's most likely located within the extension. + ORIGIN[`local`] = chrome.runtime.getURL(`config/filters/`.concat(ORIGIN[`raw`])); + + // Attempt to verify the existence of the file. + if (await net.download(ORIGIN[`local`], `json`, true)) { + configuration[`settings`][`filters`][ORIGIN[`local`]] = configuration[`settings`][`filters`][ORIGIN[`raw`]]; + }; + + // Delete the illegal URLs. + delete configuration[`settings`][`filters`][ORIGIN[`raw`]]; + }; + }; + }; + + // Set the template. + template.set(configuration); + }; }) .catch((error) => { console.error(error); }); - } - + }; + trigger() { chrome.runtime.onInstalled.addListener((details) => { (details.reason == chrome.runtime.OnInstalledReason.INSTALL) ? this.hello() : null; this.setup(); }); - } + }; // main function constructor () { From ab38de0c33b246fa36ed4dc16403956766435f54 Mon Sep 17 00:00:00 2001 From: buzz-lightsnack-2007 <73412182+buzz-lightsnack-2007@users.noreply.github.com> Date: Sat, 8 Jun 2024 09:35:20 +0800 Subject: [PATCH 04/16] Test the URL using standardized methods when updating all filters --- src/scripts/filters.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/scripts/filters.js b/src/scripts/filters.js index 2ed9bfb..4223744 100644 --- a/src/scripts/filters.js +++ b/src/scripts/filters.js @@ -71,7 +71,9 @@ export default class FilterManager { if (((typeof (FILTERS_ALL)).includes(`obj`) && !Array.isArray(FILTERS_ALL) && FILTERS_ALL) ? Object.keys(FILTERS_ALL).length > 0 : false) { for (let FILTER_URL_INDEX = 0; FILTER_URL_INDEX < Object.keys(FILTERS_ALL).length; FILTER_URL_INDEX++) { let FILTER_URL = (Object.keys(FILTERS_ALL, 1))[FILTER_URL_INDEX]; - if (FILTER_URL.includes(`://`)) { + + // Test the URL. + if (URLs.test(FILTER_URL)) { filters.enqueue(FILTER_URL); } } From d73ddbdd7ef2ec06e4ec67f777d5ef58748ad937 Mon Sep 17 00:00:00 2001 From: buzz-lightsnack-2007 <73412182+buzz-lightsnack-2007@users.noreply.github.com> Date: Sat, 8 Jun 2024 09:35:41 +0800 Subject: [PATCH 05/16] Change the comment to reduce lines --- src/scripts/secretariat.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/scripts/secretariat.js b/src/scripts/secretariat.js index c322cd1..39ba9e0 100644 --- a/src/scripts/secretariat.js +++ b/src/scripts/secretariat.js @@ -285,8 +285,7 @@ class template { }) }); - // Merge the data. - // Managed > Synchronized > Imported > Local + // Merge the data, such that Managed > Synchronized > Imported > Local. // Set managed preferences. managed.reinforce(); From aca048f3d69f9a8138d92c54bb9033857db0879a Mon Sep 17 00:00:00 2001 From: buzz-lightsnack-2007 <73412182+buzz-lightsnack-2007@users.noreply.github.com> Date: Sat, 8 Jun 2024 09:35:53 +0800 Subject: [PATCH 06/16] reduce lines on template.configure() --- src/scripts/secretariat.js | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/scripts/secretariat.js b/src/scripts/secretariat.js index 39ba9e0..30ffd33 100644 --- a/src/scripts/secretariat.js +++ b/src/scripts/secretariat.js @@ -309,16 +309,15 @@ class template { ? global.write(PREFERENCE[`name`], PREFERENCES[`all`][`build`][PREFERENCE[`name`]], -1) : false; }); - } + }; + }; /* Use our preferences when handling the data. */ static configure() { - chrome.storage.session.setAccessLevel( - {accessLevel: 'TRUSTED_AND_UNTRUSTED_CONTEXTS'} - ); + chrome.storage.session.setAccessLevel({accessLevel: 'TRUSTED_AND_UNTRUSTED_CONTEXTS'}); } } From c6f1249c442b375fb84b830836496936977fa0d3 Mon Sep 17 00:00:00 2001 From: buzz-lightsnack-2007 <73412182+buzz-lightsnack-2007@users.noreply.github.com> Date: Sat, 8 Jun 2024 17:36:41 +0800 Subject: [PATCH 07/16] Loop through each provided entry --- src/scripts/background/importer.js | 39 ++++++++++++++++++------------ 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/src/scripts/background/importer.js b/src/scripts/background/importer.js index b08b3b4..2c1e0a3 100644 --- a/src/scripts/background/importer.js +++ b/src/scripts/background/importer.js @@ -48,29 +48,36 @@ export default class BackgroundImporter { delete configuration[`OOBE`]; // Replace local URLs of filters to corresponding chrome extension pages. - if (((typeof configuration[`settings`][`filters`]).includes(`obj`) && configuration[`settings`][`filters`]) ? Object.keys(configuration[`settings`][`filters`]).length : false) { - for (let FILTER_NUMBER = 0; FILTER_NUMBER < Object.keys(configuration[`settings`][`filters`]).length; FILTER_NUMBER++) { - let SOURCE = (Object.keys(configuration[`settings`][`filters`]))[FILTER_NUMBER]; + const checkURL = async () => { + if (((typeof configuration[`settings`][`filters`]).includes(`obj`) && configuration[`settings`][`filters`]) ? Object.keys(configuration[`settings`][`filters`]).length : false) { + let FILTERS = {}; + FILTERS[`current`] = configuration[`settings`][`filters`]; + FILTERS[`updated`] = {}; - // Check if the URL is invalid. - - if (!(URLs.test(SOURCE))) { - // Set the URL. - let ORIGIN = {"raw": SOURCE}; + for (let FILTER_NUMBER = 0; FILTER_NUMBER < Object.keys(FILTERS[`current`]).length; FILTER_NUMBER++) { + let SOURCE = Object.keys(FILTERS[`current`])[FILTER_NUMBER]; - // If it is, it's most likely located within the extension. - ORIGIN[`local`] = chrome.runtime.getURL(`config/filters/`.concat(ORIGIN[`raw`])); + // Check if the URL is invalid. + if (!(URLs.test(SOURCE))) { + // Set the URL. + let ORIGIN = {"raw": SOURCE}; - // Attempt to verify the existence of the file. - if (await net.download(ORIGIN[`local`], `json`, true)) { - configuration[`settings`][`filters`][ORIGIN[`local`]] = configuration[`settings`][`filters`][ORIGIN[`raw`]]; + // If it is, it's most likely located within the extension. + ORIGIN[`local`] = chrome.runtime.getURL(`config/filters/`.concat(ORIGIN[`raw`])); + + // Attempt to verify the existence of the file. + if (await net.download(ORIGIN[`local`], `json`, true)) { + FILTERS[`updated`][ORIGIN[`local`]] = FILTERS[`current`][ORIGIN[`raw`]]; + }; + } else { + FILTERS[`updated`][SOURCE] = FILTERS[`current`][SOURCE]; }; - - // Delete the illegal URLs. - delete configuration[`settings`][`filters`][ORIGIN[`raw`]]; }; + configuration[`settings`][`filters`] = FILTERS[`updated`]; + return(FILTERS[`updated`]); }; }; + await checkURL(); // Set the template. template.set(configuration); From fffef232c0b45ac9ecd93bd97ee8dc66d5281f3e Mon Sep 17 00:00:00 2001 From: buzz-lightsnack-2007 <73412182+buzz-lightsnack-2007@users.noreply.github.com> Date: Sat, 8 Jun 2024 17:45:07 +0800 Subject: [PATCH 08/16] Failure to write is a failure to update --- src/scripts/filters.js | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/scripts/filters.js b/src/scripts/filters.js index 4223744..ea97899 100644 --- a/src/scripts/filters.js +++ b/src/scripts/filters.js @@ -94,16 +94,20 @@ export default class FilterManager { // Only work when the filter is valid. if (result) { // Write the filter to storage. - await global.write(["filters", filter_URL], result, -1, {"silent": true}); + if (!(await global.write(["filters", filter_URL], result, -1, {"silent": true}))) { + throw ReferenceError; + }; // Add the filter to the sync list. if ((await global.read(["settings", `filters`])) ? !((Object.keys(await global.read(["settings", `filters`]))).includes(filter_URL)) : true) { - global.write(["settings", `filters`, filter_URL], true, 1, {"silent": true}); + if (!(global.write(["settings", `filters`, filter_URL], true, 1, {"silent": true}))) { + throw ReferenceError; + }; }; // Notify that the update is completed. new logging(texts.localized(`settings_filters_update_status_complete`),filter_URL); - } + }; }) .catch((error) => { // Inform the user of the download failure. From ed47e40bd12e55f04ddc54c1712362685a5bc9ed Mon Sep 17 00:00:00 2001 From: buzz-lightsnack-2007 <73412182+buzz-lightsnack-2007@users.noreply.github.com> Date: Sat, 8 Jun 2024 17:48:21 +0800 Subject: [PATCH 09/16] change method of writing filter content to disk Overwrite instead of merging --- src/scripts/filters.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/scripts/filters.js b/src/scripts/filters.js index ea97899..457fe17 100644 --- a/src/scripts/filters.js +++ b/src/scripts/filters.js @@ -94,7 +94,7 @@ export default class FilterManager { // Only work when the filter is valid. if (result) { // Write the filter to storage. - if (!(await global.write(["filters", filter_URL], result, -1, {"silent": true}))) { + if (!(await global.write(["filters", filter_URL], result, -1, {"silent": true, "strict": true}))) { throw ReferenceError; }; From f38d690b578c50e96a1aa56cfe08d160f1515d28 Mon Sep 17 00:00:00 2001 From: buzz-lightsnack-2007 <73412182+buzz-lightsnack-2007@users.noreply.github.com> Date: Sat, 8 Jun 2024 17:59:52 +0800 Subject: [PATCH 10/16] Asynchronously wait for the writing to complete --- src/scripts/secretariat.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/scripts/secretariat.js b/src/scripts/secretariat.js index 30ffd33..a254096 100644 --- a/src/scripts/secretariat.js +++ b/src/scripts/secretariat.js @@ -136,9 +136,9 @@ class global { : false; // Write! - chrome.storage[(CLOUD > 0) ? `sync` : `local`].set(DATA_INJECTED); + await chrome.storage[(CLOUD > 0) ? `sync` : `local`].set(DATA_INJECTED); GUI_INFO[`log`] ? GUI_INFO[`log`].clear() : false; - return ((OPTIONS[`verify`] != null ? (OPTIONS[`verify`]) : true) ? verify(DATA_NAME, data) : true); + return ((OPTIONS[`verify`] != null ? (OPTIONS[`verify`]) : true) ? await verify(DATA_NAME, data) : true); } /* From 9c2759c41e62e681ebfa6d7a2fcbf8f64114fc69 Mon Sep 17 00:00:00 2001 From: buzz-lightsnack-2007 <73412182+buzz-lightsnack-2007@users.noreply.github.com> Date: Sat, 8 Jun 2024 22:26:57 +0800 Subject: [PATCH 11/16] forcing downloading to be synchronous to ensure successful writing --- src/scripts/filters.js | 50 ++++++++++++++++++++---------------------- 1 file changed, 24 insertions(+), 26 deletions(-) diff --git a/src/scripts/filters.js b/src/scripts/filters.js index 457fe17..9e27da2 100644 --- a/src/scripts/filters.js +++ b/src/scripts/filters.js @@ -78,7 +78,7 @@ export default class FilterManager { } } } - } + }; if (!filters.isEmpty()) { while (!filters.isEmpty()) { @@ -87,32 +87,30 @@ export default class FilterManager { // Inform the user of download state. new logging (texts.localized(`settings_filters_update_status`), filter_URL); - // Create promise of downloading. - let filter_download = net.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. - if (!(await global.write(["filters", filter_URL], result, -1, {"silent": true, "strict": true}))) { - throw ReferenceError; - }; - - // Add the filter to the sync list. - if ((await global.read(["settings", `filters`])) ? !((Object.keys(await global.read(["settings", `filters`]))).includes(filter_URL)) : true) { - if (!(global.write(["settings", `filters`, filter_URL], true, 1, {"silent": true}))) { - throw ReferenceError; - }; - }; - - // Notify that the update is completed. - new logging(texts.localized(`settings_filters_update_status_complete`),filter_URL); + try { + let DOWNLOAD = await net.download(filter_URL, `JSON`, false, true); + + // Only work when the filter is valid. + if (DOWNLOAD) { + // Write the filter to storage. + if (!(await global.write(["filters", filter_URL], DOWNLOAD, -1, {"silent": true, "strict": true}))) { + throw ReferenceError; }; - }) - .catch((error) => { - // Inform the user of the download failure. - logging.error(error.name, texts.localized(`settings_filters_update_status_failure`, null, [error.name, filter_URL]), error.stack); - }); + + // Add the filter to the sync list. + if (((await global.read(["settings", `filters`])) ? !((Object.keys(await global.read(["settings", `filters`]))).includes(filter_URL)) : true) + ? (!(await global.write(["settings", `filters`, filter_URL], true, 1, {"silent": true}))) + : false) { + throw ReferenceError; + }; + + // Notify that the update is completed. + new logging(texts.localized(`settings_filters_update_status_complete`),filter_URL); + }; + } catch (error) { + // Inform the user of the download failure. + logging.error(error.name, texts.localized(`settings_filters_update_status_failure`, null, [error.name, filter_URL]), error.stack); + }; } } else { // Inform the user of the download being unnecessary. From 7cc22f8b11608d470c616e0488401a2b6f2302ac Mon Sep 17 00:00:00 2001 From: buzz-lightsnack-2007 <73412182+buzz-lightsnack-2007@users.noreply.github.com> Date: Sat, 8 Jun 2024 22:27:48 +0800 Subject: [PATCH 12/16] pass options to verification in secretariat.js --- src/scripts/secretariat.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/scripts/secretariat.js b/src/scripts/secretariat.js index a254096..ea63197 100644 --- a/src/scripts/secretariat.js +++ b/src/scripts/secretariat.js @@ -94,7 +94,7 @@ class global { static async write(path, data, CLOUD = -1, OPTIONS = {}) { let DATA_INJECTED = {}; - async function verify (NAME, DATA) { + async function verify (NAME, DATA, OPTIONS) { let DATA_CHECK = {}; // Verify the presence of the data. From ec72d1bc01e08a5a777345f7c806a1efebd91a8c Mon Sep 17 00:00:00 2001 From: buzz-lightsnack-2007 <73412182+buzz-lightsnack-2007@users.noreply.github.com> Date: Sat, 8 Jun 2024 22:29:30 +0800 Subject: [PATCH 13/16] Get all data and set a blank value if it doesn't exist yet. --- src/scripts/secretariat.js | 35 +++++++++++++++++++++++------------ 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/src/scripts/secretariat.js b/src/scripts/secretariat.js index ea63197..fc2691a 100644 --- a/src/scripts/secretariat.js +++ b/src/scripts/secretariat.js @@ -117,11 +117,24 @@ class global { }; // Get all data and set a blank value if it doesn't exist yet. - DATA_ALL = await global.read(null, CLOUD); - DATA_ALL = ((DATA_ALL != null && DATA_ALL != undefined && (typeof DATA_ALL).includes(`obj`)) ? Object.keys(DATA_ALL).length <= 0 : true) + if (CLOUD == 0 || CLOUD == null) { + const ORIGINS = {}; + ORIGINS[-1] = `local`; + ORIGINS[1] = `global`; + + for (let SOURCE = -1; SOURCE != 0 && SOURCE < 2; SOURCE = SOURCE + 2) { + DATA_ALL[ORIGINS[SOURCE]] = await global.read(null, CLOUD); + }; + + CLOUD = (DATA_ALL[`local`] != null) ? -1 : 1; + } else { + DATA_ALL = await global.read(null, CLOUD); + }; + + DATA_ALL = ((!(typeof DATA_ALL).includes(`undef`)) ? ((DATA_ALL != null && (typeof DATA_ALL).includes(`obj`)) ? Object.keys(DATA_ALL).length <= 0 : true) : false) ? {} : DATA_ALL; - + // Set the data name. let DATA_NAME = (!(Array.isArray(path)) && path && path != undefined) ? String(path).trim().split(",") @@ -130,15 +143,14 @@ class global { // Merge! DATA_INJECTED = nested.dictionary.set(DATA_ALL, (DATA_NAME != null) ? [...DATA_NAME] : DATA_NAME, data, OPTIONS); - // If cloud is not selected, get where the data is already existent. - (CLOUD == 0 || CLOUD == null) - ? (CLOUD = (DATA_ALL[`local`] != null) ? -1 : 1) - : false; - // Write! - await chrome.storage[(CLOUD > 0) ? `sync` : `local`].set(DATA_INJECTED); - GUI_INFO[`log`] ? GUI_INFO[`log`].clear() : false; - return ((OPTIONS[`verify`] != null ? (OPTIONS[`verify`]) : true) ? await verify(DATA_NAME, data) : true); + return(chrome.storage[(CLOUD > 0) ? `sync` : `local`].set(DATA_INJECTED).then(async () => { + GUI_INFO[`log`] ? GUI_INFO[`log`].clear() : false; + return (((OPTIONS[`verify`] != null ? (OPTIONS[`verify`]) : true) ? await verify(DATA_NAME, data, OPTIONS) : true)); + }).catch((err) => { + logging.error((new texts(`error_msg_save_failed`)).localized, DATA_NAME.join(` → `), JSON.stringify(data)) + return (false); + })); } /* @@ -257,7 +269,6 @@ export async function compare(PATH, DATA) { return (RESULT); } - let COMPARISON = {}; COMPARISON[`test`] = (PATH) ? DATA : DATA[1]; COMPARISON[`against`] = (PATH) ? (await global.read((Array.isArray(PATH)) ? [...PATH] : PATH)) : DATA[0]; From 1654538159abc643bc4d6a2790bfa2dff764b74e Mon Sep 17 00:00:00 2001 From: buzz-lightsnack-2007 <73412182+buzz-lightsnack-2007@users.noreply.github.com> Date: Sat, 8 Jun 2024 23:39:22 +0800 Subject: [PATCH 14/16] Cancel the verbose logging --- src/scripts/filters.js | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/scripts/filters.js b/src/scripts/filters.js index 9e27da2..451cee8 100644 --- a/src/scripts/filters.js +++ b/src/scripts/filters.js @@ -81,12 +81,12 @@ export default class FilterManager { }; if (!filters.isEmpty()) { + // Inform the user of download state. + new logging (texts.localized(`settings_filters_update_status`)); + while (!filters.isEmpty()) { let filter_URL = filters.dequeue(); - // Inform the user of download state. - new logging (texts.localized(`settings_filters_update_status`), filter_URL); - try { let DOWNLOAD = await net.download(filter_URL, `JSON`, false, true); @@ -103,15 +103,15 @@ export default class FilterManager { : false) { throw ReferenceError; }; - - // Notify that the update is completed. - new logging(texts.localized(`settings_filters_update_status_complete`),filter_URL); }; } catch (error) { // Inform the user of the download failure. logging.error(error.name, texts.localized(`settings_filters_update_status_failure`, null, [error.name, filter_URL]), error.stack); }; - } + }; + + // Notify that the update is completed. + new logging(texts.localized(`settings_filters_update_status_complete`)); } else { // Inform the user of the download being unnecessary. logging.warn(texts.localized(`settings_filters_update_stop`)); From 7a6c6deba97a7680c47ab7d4c0af6a2ac59f3a47 Mon Sep 17 00:00:00 2001 From: buzz-lightsnack-2007 <73412182+buzz-lightsnack-2007@users.noreply.github.com> Date: Sat, 8 Jun 2024 23:39:44 +0800 Subject: [PATCH 15/16] set ready state of the extension --- src/scripts/background/importer.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/scripts/background/importer.js b/src/scripts/background/importer.js index 2c1e0a3..475fd7b 100644 --- a/src/scripts/background/importer.js +++ b/src/scripts/background/importer.js @@ -81,6 +81,7 @@ export default class BackgroundImporter { // Set the template. template.set(configuration); + await global.write([`ready`], true, -1); }; }) .catch((error) => { From ff19ca4ec16735bfd3be2dd45473360106241b82 Mon Sep 17 00:00:00 2001 From: buzz-lightsnack-2007 <73412182+buzz-lightsnack-2007@users.noreply.github.com> Date: Sat, 8 Jun 2024 23:39:59 +0800 Subject: [PATCH 16/16] Run the checker if the extension is ready. --- src/scripts/background/check.js | 179 +++++++++++++++++--------------- 1 file changed, 98 insertions(+), 81 deletions(-) diff --git a/src/scripts/background/check.js b/src/scripts/background/check.js index 1a6b90c..80141b6 100644 --- a/src/scripts/background/check.js +++ b/src/scripts/background/check.js @@ -10,10 +10,25 @@ import {background, global} from "/scripts/secretariat.js"; export default class BackgroundCheck { update = {}; + status = {}; constructor () { - this.manager = new EntryManager(); - this.updater(); + const main = () => { + global.read([`ready`]).then((STATE) => { + if (STATE && !this.status[`ready`]) { + this.manager = new EntryManager(); + this.updater(); + + this.status[`ready`] = true; + }; + + if (this.status[`ready`]) { + (this.status.wait) ? this.status.wait.cancel() : false; + }; + }); + }; + + this.status.wait = new background(() => {main();}) }; updater() { @@ -26,94 +41,96 @@ export default class BackgroundCheck { if (!(typeof DURATION_PREFERENCES).includes(`obj`) || !DURATION_PREFERENCES || Array.isArray(DURATION_PREFERENCES)) { DURATION_PREFERENCES = {}; DURATION_PREFERENCES[`duration`] = 24; - + // Write it. - return(await global.write([`settings`, `sync`], DURATION_PREFERENCES, -1, {"silent": true})); - } else {return (true)}; + return(global.write([`settings`, `sync`, `duration`], DURATION_PREFERENCES[`duration`], -1, {"silent": true})); + } else {return(true)}; + }; + + // Set the filter management. + let filter = new FilterManager(); + + /* + Run the update. + + @return {Promise} the last update status. + */ + const updater_run = async () => { + filter.update(); + + // Update the last update time. + return(await global.write([`settings`,`sync`,`last`], Date.now(), -1, {"silent": true})); + }; + + /* + Check if it's time to update the filters through comparing the difference of the current time and the last updated time to the expected duration. + */ + async function updater_check() { + let TIME = {}; + TIME[`now`] = Date.now(); + TIME[`last`] = await global.read([`settings`,`sync`,`last`], -1); + + // Run if the last time is not set or if the difference is greater than the expected duration. + return (TIME[`last`] ? ((TIME[`now`] - TIME[`last`]) > DURATION_PREFERENCES[`duration`]) : true); + }; + + // Set the interval. + let updater_set = () => { + this.update[`checker`] = setInterval(async () => { + // Update the filters. + updater_run(); + }, DURATION_PREFERENCES[`duration`]); }; - setDefaults().then((result) => { - if (result) { - /* - Check if it's time to update the filters through comparing the difference of the current time and the last updated time to the expected duration. - */ - async function updater_check() { - let TIME = {}; - TIME[`now`] = Date.now(); - TIME[`last`] = await global.read([`settings`,`sync`,`last`], -1); + /* + Reset the interval. + */ + const updater_reset = () => { + // Cancel the interval. + (this.update[`checker`]) ? clearInterval(this.update[`checker`]) : false; - // Run if the last time is not set or if the difference is greater than the expected duration. - return (TIME[`last`] ? ((TIME[`now`] - TIME[`last`]) > DURATION_PREFERENCES[`duration`]) : true); + // Run the updater, if necessary. + (updater_check()) + ? updater_run() + : false; + + // Start the new interval. + updater_set(); + } + + const updater_background = () => { + this.update[`background`] = async () => { + if ((await global.read([`settings`, `sync`, `duration`])) ? (await global.read([`settings`, `sync`, `duration`] * (60 ** 2) * 1000 != DURATION_PREFERENCES[`duration`])) : false) { + if (await global.global.read([`settings`, `sync`, `duration`])) { + // Get the new time. + DURATION_PREFERENCES[`duration`] = await global.read([`settings`, `sync`, `duration`]) * (60 ** 2) * 1000; + + // Reset the updater. + updater_reset(); + } }; + }; - /* - Run the update. + // Set the background checker. + new background(() => {return(this.update.background())}); + } + + if (!(DURATION_PREFERENCES)) { + DURATION_PREFERENCES = {}; + }; - @return {Promise} the promise that, once resolved, contains the last update status. - */ - const updater_run = async () => { - filter.update(); + // Convert DURATION_PREFERENCES[`duration`]) from hrs to milliseconds. + if (DURATION_PREFERENCES[`duration`]) { + DURATION_PREFERENCES[`duration`] = DURATION_PREFERENCES[`duration`] * (60 ** 2) * 1000; + }; - // Update the last update time. - return(await global.write([`settings`,`sync`,`last`], Date.now(), -1, {"verify": false})); - }; + // When the browser is started, run the updater immediately only when the filters are not updated yet. + (updater_check() || !DURATION_PREFERENCES) + ? updater_run() + : false; - // Set the interval. - let updater_set = () => { - this.update[`checker`] = setInterval(async () => { - // Update the filters. - updater_run(); - }, DURATION_PREFERENCES[`duration`]); - }; - - /* - Reset the interval. - */ - const updater_reset = () => { - // Cancel the interval. - (this.update[`checker`]) ? clearInterval(this.update[`checker`]) : false; - - // Run the updater, if necessary. - (updater_check()) - ? updater_run() - : false; - - // Start the new interval. - updater_set(); - } - - const updater_background = () => { - this.update[`background`] = async () => { - if ((await global.read([`settings`, `sync`, `duration`])) ? (await global.read([`settings`, `sync`, `duration`] * (60 ** 2) * 1000 != DURATION_PREFERENCES[`duration`])) : false) { - if (await global.global.read([`settings`, `sync`, `duration`])) { - // Get the new time. - DURATION_PREFERENCES[`duration`] = await global.read([`settings`, `sync`, `duration`]) * (60 ** 2) * 1000; - - // Reset the updater. - updater_reset(); - } - }; - }; - - // Set the background checker. - new background(() => {return(this.update.background())}); - } - - // Convert DURATION_PREFERENCES[`duration`]) from hrs to milliseconds. - DURATION_PREFERENCES[`duration`] = DURATION_PREFERENCES[`duration`] * (60 ** 2) * 1000; - - // Set the filter management. - let filter = new FilterManager(); - - // When the browser is started, run the updater immediately only when the filters are not updated yet. - (updater_check()) - ? updater_run() - : false; - - // Run the background. - updater_background(); - } - }); + // Run the background. + updater_background(); }) } };