autoformatted syntax

This commit is contained in:
buzz-lightsnack-2007 2024-03-28 11:14:52 +08:00
parent 23625b4ba8
commit 3e475d162a

View file

@ -12,145 +12,166 @@ import logging from "/gui/scripts/logging.JS";
@return {object} the data @return {object} the data
*/ */
export async function read(DATA_NAME, CLOUD = 0, PARAMETER_TEST = null) { export async function read(DATA_NAME, CLOUD = 0, PARAMETER_TEST = null) {
// Initialize the selected pref data.
let DATA = {},
DATA_ALL = {},
DATA_RETURNED = {};
// Initialize the selected pref data. // Convert the entered prefname to an array if it is not one.
let DATA = {}, DATA_ALL = {}, DATA_RETURNED = {}; if (!(typeof DATA_NAME).includes(`object`)) {
// Avoid null
if ((typeof DATA_NAME).includes(`str`) ? DATA_NAME.trim() : DATA_NAME) {
// Syntax of splitting is by commas.
DATA_NAME = String(DATA_NAME).trim().split(",");
}
}
// Convert the entered prefname to an array if it is not one. /*
if (!(typeof DATA_NAME).includes(`object`)) {
// Avoid null
if (((typeof DATA_NAME).includes(`str`)) ? DATA_NAME.trim() : DATA_NAME) {
// Syntax of splitting is by commas.
DATA_NAME = (String(DATA_NAME).trim()).split(",");
}
};
/*
Find the data now. Find the data now.
@param {number} SOURCE the data source @param {number} SOURCE the data source
*/ */
function read_database(SOURCE = -1) { function read_database(SOURCE = -1) {
let data = {}; let data = {};
let data_returned; let data_returned;
async function read_database_local() { async function read_database_local() {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
chrome.storage.local.get(null, function(result) { chrome.storage.local.get(null, function (result) {
if (chrome.runtime.lastError) { if (chrome.runtime.lastError) {
// Something went wrong // Something went wrong
reject(new Error(chrome.runtime.lastError)); reject(new Error(chrome.runtime.lastError));
} else { } else {
// If the key exists, return the value // If the key exists, return the value
resolve(result); resolve(result);
} }
}); });
}); });
}; }
async function read_database_sync() { async function read_database_sync() {
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
chrome.storage.sync.get(null, function(result) { chrome.storage.sync.get(null, function (result) {
if (chrome.runtime.lastError) { if (chrome.runtime.lastError) {
// Something went wrong // Something went wrong
reject(new Error(chrome.runtime.lastError)); reject(new Error(chrome.runtime.lastError));
} else { } else {
// If the key exists, return the value // If the key exists, return the value
resolve(result); resolve(result);
} }
}); });
}); });
} }
// Return the data. // Return the data.
if (SOURCE > 0) { if (SOURCE > 0) {
data_returned = read_database_sync(); data_returned = read_database_sync();
} else { } else {
data_returned = read_database_local(); data_returned = read_database_local();
} }
return(data_returned); return data_returned;
}; }
/* Recursively find through each data, returning either that value or null when the object is not found. /* Recursively find through each data, returning either that value or null when the object is not found.
@param {dictionary} DATA_ALL the data @param {dictionary} DATA_ALL the data
@param {object} DATA_PATH the path of the data @param {object} DATA_PATH the path of the data
@return {object} the data @return {object} the data
*/ */
function find_data(DATA_ALL, DATA_PATH, PARAMETER_TEST) { function find_data(DATA_ALL, DATA_PATH, PARAMETER_TEST) {
// Pull the data out. // Pull the data out.
let DATA_PATH_SELECTED = String(DATA_PATH.shift()).trim(); let DATA_PATH_SELECTED = String(DATA_PATH.shift()).trim();
let DATA_SELECTED = DATA_ALL; let DATA_SELECTED = DATA_ALL;
// Only run when the data is valid. // Only run when the data is valid.
if (DATA_ALL) { if (DATA_ALL) {
if (DATA_SELECTED) { if (DATA_SELECTED) {
// Get the selected data. // Get the selected data.
DATA_SELECTED = DATA_ALL[DATA_PATH_SELECTED]; DATA_SELECTED = DATA_ALL[DATA_PATH_SELECTED];
if (DATA_PATH.length > 0) { if (DATA_PATH.length > 0) {
// Recursively run to make use of the existing data. // Recursively run to make use of the existing data.
DATA_SELECTED = find_data(DATA_SELECTED, DATA_PATH, PARAMETER_TEST); DATA_SELECTED = find_data(DATA_SELECTED, DATA_PATH, PARAMETER_TEST);
} }
} else if (PARAMETER_TEST && DATA_SELECTED) { } else if (PARAMETER_TEST && DATA_SELECTED) {
let QUALIFIED = false; let QUALIFIED = false;
// The expected keys are "field" and "test value" // The expected keys are "field" and "test value"
DATA_SELECTED_KEYS = Object.keys(DATA_SELECTED); DATA_SELECTED_KEYS = Object.keys(DATA_SELECTED);
if (PARAMETER_TEST[`field`] && PARAMETER_TEST[`test value`]) { if (PARAMETER_TEST[`field`] && PARAMETER_TEST[`test value`]) {
// Perform a sequential search.
for (
let DATA_SELECTED_KEY_INDEX = 0;
DATA_SELECTED_KEY_INDEX < DATA_SELECTED_KEYS.length || !QUALIFIED;
DATA_SELECTED_KEY_INDEX++
) {
PARAMETER_TEST[`value`] =
DATA_SELECTED[DATA_SELECTED_KEYS[DATA_SELECTED_KEY_INDEX]][
PARAMETER_TEST[`field`]
];
if (PARAMETER_TEST[`value`]) {
QUALIFIED =
new RegExp(String(PARAMETER_TEST[`value`])).test(
PARAMETER_TEST[`test value`],
) ||
PARAMETER_TEST[`test value`].includes(PARAMETER_TEST[`value`]);
}
// Perform a sequential search. if (QUALIFIED) {
for (let DATA_SELECTED_KEY_INDEX = 0; ((DATA_SELECTED_KEY_INDEX < DATA_SELECTED_KEYS.length) || (!QUALIFIED)); DATA_SELECTED_KEY_INDEX++) { DATA_SELECTED =
PARAMETER_TEST[`value`] = DATA_SELECTED[DATA_SELECTED_KEYS[DATA_SELECTED_KEY_INDEX]][PARAMETER_TEST[`field`]] DATA_SELECTED[DATA_SELECTED_KEYS[DATA_SELECTED_KEY_INDEX]];
if (PARAMETER_TEST[`value`]) { break;
QUALIFIED = (((new RegExp(String(PARAMETER_TEST[`value`])).test(PARAMETER_TEST[`test value`])) || (PARAMETER_TEST[`test value`].includes(PARAMETER_TEST[`value`])))); }
}; }
if (QUALIFIED) { if (!QUALIFIED) {
DATA_SELECTED = DATA_SELECTED[DATA_SELECTED_KEYS[DATA_SELECTED_KEY_INDEX]]; DATA_SELECTED = null;
break; }
} } else {
} // It is not valid, so do not return anything.
DATA_SELECTED = null;
}
}
}
if (!QUALIFIED) { // Now return the data.
DATA_SELECTED = null; return DATA_SELECTED;
} }
} else {
// It is not valid, so do not return anything.
DATA_SELECTED = null;
}
}
}
// Now return the data. // Read data from local and sync storage (asynchronous operations)
return (DATA_SELECTED); try {
} if (CLOUD <= 0) {
[DATA_ALL[`local`]] = await Promise.all([read_database(-1)]);
}
if (CLOUD >= 0) {
[DATA_ALL[`sync`]] = await Promise.all([read_database(1)]);
}
} catch ({ name, message }) {
logging.error(name, message);
}
// Read data from local and sync storage (asynchronous operations) // Let's get through everything and then determine which one has…
try { Object.keys(DATA_ALL).forEach((DATA_SOURCE) => {
if (CLOUD <= 0) { if (DATA_ALL[DATA_SOURCE]) {
[DATA_ALL[`local`]] = await Promise.all([read_database(-1)]); DATA[DATA_SOURCE] = DATA_NAME
}; ? find_data(DATA_ALL[DATA_SOURCE], DATA_NAME, PARAMETER_TEST)
if (CLOUD >= 0) { : DATA_ALL[DATA_SOURCE];
[DATA_ALL[`sync`]] = await Promise.all([read_database(1)]); }
}; });
} catch ({name, message}) {
logging.error(name, message);
};
// Let's get through everything and then determine which one has… // Now return the data.
(Object.keys(DATA_ALL)).forEach((DATA_SOURCE) => { DATA_RETURNED[`source`] =
if (DATA_ALL[DATA_SOURCE]) { CLOUD != 0
DATA[DATA_SOURCE] = (DATA_NAME) ? find_data(DATA_ALL[DATA_SOURCE], DATA_NAME, PARAMETER_TEST) : DATA_ALL[DATA_SOURCE]; ? CLOUD > 0
} ? `sync`
}); : `local`
: (DATA[`sync`] ? DATA[`sync`].length <= 0 : DATA[`sync`])
? `sync`
: `local`;
DATA_RETURNED[`value`] = DATA[DATA_RETURNED[`source`]];
// Now return the data. return DATA_RETURNED[`value`];
DATA_RETURNED[`source`] = (CLOUD != 0) ? ((CLOUD > 0) ? `sync` : `local`) : (((DATA[`sync`]) ? (DATA[`sync`].length <= 0) : (DATA[`sync`])) ? `sync` : `local`)
DATA_RETURNED[`value`] = DATA[DATA_RETURNED[`source`]];
return(DATA_RETURNED[`value`]);
} }
/* Write the data on the selected prefname. /* Write the data on the selected prefname.
@ -160,70 +181,76 @@ export async function read(DATA_NAME, CLOUD = 0, PARAMETER_TEST = null) {
@param {int} CLOUD store in the cloud; otherwise set to automatic @param {int} CLOUD store in the cloud; otherwise set to automatic
*/ */
export function write(PATH, DATA, CLOUD = -1) { export function write(PATH, DATA, CLOUD = -1) {
let DATA_INJECTED = {}; let DATA_INJECTED = {};
/* Forcibly write the data to chrome database /* Forcibly write the data to chrome database
@param {object} DATA the data @param {object} DATA the data
@param {number} CLOUD the storage @param {number} CLOUD the storage
*/ */
function write_database(DATA, CLOUD = 0) { function write_database(DATA, CLOUD = 0) {
// If CLOUD is set to 0, it should automatically determine where the previous source of data was taken from. // If CLOUD is set to 0, it should automatically determine where the previous source of data was taken from.
if (CLOUD > 0) { if (CLOUD > 0) {
chrome.storage.sync.set(DATA); chrome.storage.sync.set(DATA);
} else if (CLOUD < 0) { } else if (CLOUD < 0) {
chrome.storage.local.set(DATA); chrome.storage.local.set(DATA);
}; }
}; }
/* Appropriately nest and merge the data. /* Appropriately nest and merge the data.
@param {object} EXISTING the original data @param {object} EXISTING the original data
@param {object} PATH the subpath @param {object} PATH the subpath
@param {object} VALUE the value @param {object} VALUE the value
@return {object} the updated data @return {object} the updated data
*/ */
function nest(EXISTING, SUBPATH, VALUE) { function nest(EXISTING, SUBPATH, VALUE) {
let DATABASE = EXISTING; let DATABASE = EXISTING;
// Get the current path. // Get the current path.
let PATH = {}; let PATH = {};
PATH[`current`] = String(SUBPATH.shift()).trim(); PATH[`current`] = String(SUBPATH.shift()).trim();
PATH[`target`] = SUBPATH; PATH[`target`] = SUBPATH;
if (PATH[`target`].length > 0) { if (PATH[`target`].length > 0) {
if (DATABASE[PATH[`current`]] == null) {DATABASE[PATH[`current`]] = {}}; if (DATABASE[PATH[`current`]] == null) {
DATABASE[PATH[`current`]] = nest(DATABASE[PATH[`current`]], PATH[`target`], VALUE); DATABASE[PATH[`current`]] = {};
} else { }
DATABASE[PATH[`current`]] = VALUE; DATABASE[PATH[`current`]] = nest(
}; DATABASE[PATH[`current`]],
// Return the value. PATH[`target`],
return (DATABASE); VALUE,
}; );
} else {
DATABASE[PATH[`current`]] = VALUE;
}
// Return the value.
return DATABASE;
}
(read(null, CLOUD)).then((DATA_ALL) => { read(null, CLOUD).then((DATA_ALL) => {
// handle empty collected data.
if (!DATA_ALL) {
DATA_ALL = {};
}
// handle empty collected data. let DATA_NAME = PATH;
if (!DATA_ALL) {DATA_ALL = {};}
let DATA_NAME = PATH; // Convert the entered prefname to an array if it is not one.
if (!(typeof SUBPATH).includes(`object`)) {
// Split what is not an object.
DATA_NAME = String(PATH).trim().split(",");
}
// Convert the entered prefname to an array if it is not one. // Merge!
if (!(typeof SUBPATH).includes(`object`)) { DATA_INJECTED = nest(DATA_ALL, DATA_NAME, DATA);
// Split what is not an object.
DATA_NAME = (String(PATH).trim()).split(",");
}
// Merge! // Write!
DATA_INJECTED = nest(DATA_ALL, DATA_NAME, DATA); write_database(DATA_INJECTED, CLOUD);
});
// Write!
write_database(DATA_INJECTED, CLOUD);
});
} }
/* Dangerous: Resets all data or a domain's data. /* Dangerous: Resets all data or a domain's data.
@param {string} preference the preference name to delete @param {string} preference the preference name to delete
@ -232,53 +259,56 @@ export function write(PATH, DATA, CLOUD = -1) {
@return {boolean} the user's confirmation @return {boolean} the user's confirmation
*/ */
export function forget(preference, subpreference, CLOUD = 0) { export function forget(preference, subpreference, CLOUD = 0) {
let forget_action = false;
let forget_action = false; (async () => {
// Import alerts module.
let alerts = await import(chrome.runtime.getURL(`gui/scripts/alerts.js`));
(async () => { // Confirm the action.
// Import alerts module. let forget_action = alerts.confirm_action();
let alerts = await import(chrome.runtime.getURL(`gui/scripts/alerts.js`));
// Confirm the action. if (forget_action) {
let forget_action = alerts.confirm_action(); if (preference) {
if (subpreference) {
// Get the data.
data = read(preference, CLOUD);
if (forget_action) { // Should only run when existent
if (preference) { if (data[subpreference]) {
if (subpreference) { delete data[subpreference];
// Get the data. write([preference, subpreference], data, CLOUD);
data = read(preference, CLOUD); }
} else {
// Remove that particular data.
if (CLOUD <= 0) {
chrome.storage.local.get(null, (data) => {
delete data[preference];
// Should only run when existent chrome.storage.local.set(data, (result) => {});
if (data[subpreference]) { });
delete data[subpreference]; }
write([preference, subpreference], data, CLOUD); if (CLOUD >= 0) {
} chrome.storage.sync.get(null, (data) => {
} else { delete data[preference];
// Remove that particular data.
if (CLOUD <= 0) {
chrome.storage.local.get(null, (data) => {
delete data[preference];
chrome.storage.local.set(data, (result) => {}); chrome.storage.sync.set(data, (result) => {});
}); });
}; }
if (CLOUD >= 0) { }
chrome.storage.sync.get(null, (data) => { } else {
delete data[preference]; // Clear the data storage.
if (CLOUD >= 0) {
chrome.storage.sync.clear();
}
if (CLOUD <= 0) {
chrome.storage.local.clear();
}
}
}
})();
chrome.storage.sync.set(data, (result) => {}); return forget_action;
});
};
};
} else {
// Clear the data storage.
if (CLOUD >= 0) {chrome.storage.sync.clear();};
if (CLOUD <= 0) {chrome.storage.local.clear();};
};
};
})();
return (forget_action);
} }
/* Initialize the storage. /* Initialize the storage.
@ -286,57 +316,69 @@ export function forget(preference, subpreference, CLOUD = 0) {
@param {dictionary} data this build's managed data @param {dictionary} data this build's managed data
*/ */
export function init(data) { export function init(data) {
let PREFERENCES_ALL = {}; let PREFERENCES_ALL = {};
PREFERENCES_ALL[`build`] = data; PREFERENCES_ALL[`build`] = data;
// Read all data. // Read all data.
chrome.storage.managed.get(null, function(DATA_MANAGED){ chrome.storage.managed.get(null, function (DATA_MANAGED) {
PREFERENCES_ALL[`managed`] = DATA_MANAGED; PREFERENCES_ALL[`managed`] = DATA_MANAGED;
}); });
chrome.storage.local.get(null, function(DATA_LOCAL){ chrome.storage.local.get(null, function (DATA_LOCAL) {
PREFERENCES_ALL[`local`] = DATA_LOCAL; PREFERENCES_ALL[`local`] = DATA_LOCAL;
}); });
chrome.storage.sync.get(null, function(DATA_SYNC){ chrome.storage.sync.get(null, function (DATA_SYNC) {
PREFERENCES_ALL[`sync`] = DATA_SYNC; PREFERENCES_ALL[`sync`] = DATA_SYNC;
}); });
// Merge data. // Merge data.
// Managed > Synchronized > Imported > Local // Managed > Synchronized > Imported > Local
if (PREFERENCES_ALL[`managed`]) { if (PREFERENCES_ALL[`managed`]) {
(Object.keys(PREFERENCES_ALL[`managed`])).forEach((item) => { Object.keys(PREFERENCES_ALL[`managed`]).forEach((item) => {
let PREFERENCE = {'name': item, 'existing': false}; let PREFERENCE = { name: item, existing: false };
if (PREFERENCES_ALL[`sync`]) { if (PREFERENCES_ALL[`sync`]) {
PREFERENCE[`existing`] = (PREFERENCES_ALL[`sync`]).hasOwnProperty(PREFERENCE[`name`]); PREFERENCE[`existing`] = PREFERENCES_ALL[`sync`].hasOwnProperty(
} PREFERENCE[`name`],
);
}
if (!PREFERENCE[`existing`]) { if (!PREFERENCE[`existing`]) {
// Do not allow synchronized data to interfere with managed data. // Do not allow synchronized data to interfere with managed data.
forget(PREFERENCE[`name`]); forget(PREFERENCE[`name`]);
write(PREFERENCE[`name`], PREFERENCES_ALL[`managed`][PREFERENCE[`name`]]); write(
} PREFERENCE[`name`],
PREFERENCES_ALL[`managed`][PREFERENCE[`name`]],
);
}
});
}
}); // Import build data
} if (PREFERENCES_ALL[`build`]) {
Object.keys(PREFERENCES_ALL[`build`]).forEach((item) => {
let PREFERENCE = { name: item, existing: false };
// Import build data PREFERENCE[`existing`] =
if (PREFERENCES_ALL[`build`]) { (PREFERENCES_ALL[`sync`]
(Object.keys(PREFERENCES_ALL[`build`])).forEach((item) => { ? PREFERENCES_ALL[`sync`].hasOwnProperty(PREFERENCE[`name`])
let PREFERENCE = {'name': item, 'existing': false}; : false) ||
(PREFERENCES_ALL[`managed`]
? PREFERENCES_ALL[`managed`].hasOwnProperty(PREFERENCE[`name`])
: false) ||
(PREFERENCES_ALL[`local`]
? PREFERENCES_ALL[`local`].hasOwnProperty(PREFERENCE[`local`])
: false);
PREFERENCE[`existing`] = ( if (!PREFERENCE[`existing`]) {
((PREFERENCES_ALL[`sync`]) ? (PREFERENCES_ALL[`sync`]).hasOwnProperty(PREFERENCE[`name`]) : false) || write(
((PREFERENCES_ALL[`managed`]) ? (PREFERENCES_ALL[`managed`]).hasOwnProperty(PREFERENCE[`name`]) : false) || PREFERENCE[`name`],
((PREFERENCES_ALL[`local`]) ? (PREFERENCES_ALL[`local`]).hasOwnProperty(PREFERENCE[`local`]) : false) PREFERENCES_ALL[`build`][PREFERENCE[`name`]],
); -1,
);
if (!PREFERENCE[`existing`]) { }
write(PREFERENCE[`name`], PREFERENCES_ALL[`build`][PREFERENCE[`name`]], -1); });
} }
});
}
} }