autoformatted syntax
This commit is contained in:
parent
23625b4ba8
commit
3e475d162a
1 changed files with 280 additions and 238 deletions
|
@ -12,18 +12,19 @@ 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.
|
// Initialize the selected pref data.
|
||||||
let DATA = {}, DATA_ALL = {}, DATA_RETURNED = {};
|
let DATA = {},
|
||||||
|
DATA_ALL = {},
|
||||||
|
DATA_RETURNED = {};
|
||||||
|
|
||||||
// Convert the entered prefname to an array if it is not one.
|
// Convert the entered prefname to an array if it is not one.
|
||||||
if (!(typeof DATA_NAME).includes(`object`)) {
|
if (!(typeof DATA_NAME).includes(`object`)) {
|
||||||
// Avoid null
|
// Avoid null
|
||||||
if (((typeof DATA_NAME).includes(`str`)) ? DATA_NAME.trim() : DATA_NAME) {
|
if ((typeof DATA_NAME).includes(`str`) ? DATA_NAME.trim() : DATA_NAME) {
|
||||||
// Syntax of splitting is by commas.
|
// Syntax of splitting is by commas.
|
||||||
DATA_NAME = (String(DATA_NAME).trim()).split(",");
|
DATA_NAME = String(DATA_NAME).trim().split(",");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
Find the data now.
|
Find the data now.
|
||||||
|
@ -36,7 +37,7 @@ export async function read(DATA_NAME, CLOUD = 0, PARAMETER_TEST = null) {
|
||||||
|
|
||||||
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));
|
||||||
|
@ -46,11 +47,11 @@ export async function read(DATA_NAME, CLOUD = 0, PARAMETER_TEST = null) {
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
};
|
}
|
||||||
|
|
||||||
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));
|
||||||
|
@ -69,8 +70,8 @@ export async function read(DATA_NAME, CLOUD = 0, PARAMETER_TEST = null) {
|
||||||
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.
|
||||||
|
|
||||||
|
@ -99,16 +100,27 @@ export async function read(DATA_NAME, CLOUD = 0, PARAMETER_TEST = null) {
|
||||||
// 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.
|
// Perform a sequential search.
|
||||||
for (let DATA_SELECTED_KEY_INDEX = 0; ((DATA_SELECTED_KEY_INDEX < DATA_SELECTED_KEYS.length) || (!QUALIFIED)); DATA_SELECTED_KEY_INDEX++) {
|
for (
|
||||||
PARAMETER_TEST[`value`] = DATA_SELECTED[DATA_SELECTED_KEYS[DATA_SELECTED_KEY_INDEX]][PARAMETER_TEST[`field`]]
|
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`]) {
|
if (PARAMETER_TEST[`value`]) {
|
||||||
QUALIFIED = (((new RegExp(String(PARAMETER_TEST[`value`])).test(PARAMETER_TEST[`test value`])) || (PARAMETER_TEST[`test value`].includes(PARAMETER_TEST[`value`]))));
|
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 =
|
||||||
|
DATA_SELECTED[DATA_SELECTED_KEYS[DATA_SELECTED_KEY_INDEX]];
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -124,33 +136,42 @@ export async function read(DATA_NAME, CLOUD = 0, PARAMETER_TEST = null) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Now return the data.
|
// Now return the data.
|
||||||
return (DATA_SELECTED);
|
return DATA_SELECTED;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Read data from local and sync storage (asynchronous operations)
|
// Read data from local and sync storage (asynchronous operations)
|
||||||
try {
|
try {
|
||||||
if (CLOUD <= 0) {
|
if (CLOUD <= 0) {
|
||||||
[DATA_ALL[`local`]] = await Promise.all([read_database(-1)]);
|
[DATA_ALL[`local`]] = await Promise.all([read_database(-1)]);
|
||||||
};
|
}
|
||||||
if (CLOUD >= 0) {
|
if (CLOUD >= 0) {
|
||||||
[DATA_ALL[`sync`]] = await Promise.all([read_database(1)]);
|
[DATA_ALL[`sync`]] = await Promise.all([read_database(1)]);
|
||||||
};
|
}
|
||||||
} catch ({name, message}) {
|
} catch ({ name, message }) {
|
||||||
logging.error(name, message);
|
logging.error(name, message);
|
||||||
};
|
}
|
||||||
|
|
||||||
// Let's get through everything and then determine which one has…
|
// Let's get through everything and then determine which one has…
|
||||||
(Object.keys(DATA_ALL)).forEach((DATA_SOURCE) => {
|
Object.keys(DATA_ALL).forEach((DATA_SOURCE) => {
|
||||||
if (DATA_ALL[DATA_SOURCE]) {
|
if (DATA_ALL[DATA_SOURCE]) {
|
||||||
DATA[DATA_SOURCE] = (DATA_NAME) ? find_data(DATA_ALL[DATA_SOURCE], DATA_NAME, PARAMETER_TEST) : DATA_ALL[DATA_SOURCE];
|
DATA[DATA_SOURCE] = DATA_NAME
|
||||||
|
? find_data(DATA_ALL[DATA_SOURCE], DATA_NAME, PARAMETER_TEST)
|
||||||
|
: DATA_ALL[DATA_SOURCE];
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
// Now return the data.
|
// Now return the data.
|
||||||
DATA_RETURNED[`source`] = (CLOUD != 0) ? ((CLOUD > 0) ? `sync` : `local`) : (((DATA[`sync`]) ? (DATA[`sync`].length <= 0) : (DATA[`sync`])) ? `sync` : `local`)
|
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`]];
|
DATA_RETURNED[`value`] = DATA[DATA_RETURNED[`source`]];
|
||||||
|
|
||||||
return(DATA_RETURNED[`value`]);
|
return DATA_RETURNED[`value`];
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Write the data on the selected prefname.
|
/* Write the data on the selected prefname.
|
||||||
|
@ -174,8 +195,8 @@ export function write(PATH, DATA, CLOUD = -1) {
|
||||||
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.
|
||||||
|
|
||||||
|
@ -193,26 +214,33 @@ export function write(PATH, DATA, CLOUD = -1) {
|
||||||
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`]] = {};
|
||||||
|
}
|
||||||
|
DATABASE[PATH[`current`]] = nest(
|
||||||
|
DATABASE[PATH[`current`]],
|
||||||
|
PATH[`target`],
|
||||||
|
VALUE,
|
||||||
|
);
|
||||||
} else {
|
} else {
|
||||||
DATABASE[PATH[`current`]] = VALUE;
|
DATABASE[PATH[`current`]] = VALUE;
|
||||||
};
|
}
|
||||||
// Return the value.
|
// Return the value.
|
||||||
return (DATABASE);
|
return DATABASE;
|
||||||
};
|
}
|
||||||
|
|
||||||
(read(null, CLOUD)).then((DATA_ALL) => {
|
|
||||||
|
|
||||||
|
read(null, CLOUD).then((DATA_ALL) => {
|
||||||
// handle empty collected data.
|
// handle empty collected data.
|
||||||
if (!DATA_ALL) {DATA_ALL = {};}
|
if (!DATA_ALL) {
|
||||||
|
DATA_ALL = {};
|
||||||
|
}
|
||||||
|
|
||||||
let DATA_NAME = PATH;
|
let DATA_NAME = PATH;
|
||||||
|
|
||||||
// Convert the entered prefname to an array if it is not one.
|
// Convert the entered prefname to an array if it is not one.
|
||||||
if (!(typeof SUBPATH).includes(`object`)) {
|
if (!(typeof SUBPATH).includes(`object`)) {
|
||||||
// Split what is not an object.
|
// Split what is not an object.
|
||||||
DATA_NAME = (String(PATH).trim()).split(",");
|
DATA_NAME = String(PATH).trim().split(",");
|
||||||
}
|
}
|
||||||
|
|
||||||
// Merge!
|
// Merge!
|
||||||
|
@ -223,7 +251,6 @@ export function write(PATH, DATA, CLOUD = -1) {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* 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,7 +259,6 @@ 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 () => {
|
(async () => {
|
||||||
|
@ -261,24 +287,28 @@ export function forget(preference, subpreference, CLOUD = 0) {
|
||||||
|
|
||||||
chrome.storage.local.set(data, (result) => {});
|
chrome.storage.local.set(data, (result) => {});
|
||||||
});
|
});
|
||||||
};
|
}
|
||||||
if (CLOUD >= 0) {
|
if (CLOUD >= 0) {
|
||||||
chrome.storage.sync.get(null, (data) => {
|
chrome.storage.sync.get(null, (data) => {
|
||||||
delete data[preference];
|
delete data[preference];
|
||||||
|
|
||||||
chrome.storage.sync.set(data, (result) => {});
|
chrome.storage.sync.set(data, (result) => {});
|
||||||
});
|
});
|
||||||
};
|
}
|
||||||
};
|
}
|
||||||
} else {
|
} else {
|
||||||
// Clear the data storage.
|
// Clear the data storage.
|
||||||
if (CLOUD >= 0) {chrome.storage.sync.clear();};
|
if (CLOUD >= 0) {
|
||||||
if (CLOUD <= 0) {chrome.storage.local.clear();};
|
chrome.storage.sync.clear();
|
||||||
};
|
}
|
||||||
};
|
if (CLOUD <= 0) {
|
||||||
|
chrome.storage.local.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
})();
|
})();
|
||||||
|
|
||||||
return (forget_action);
|
return forget_action;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Initialize the storage.
|
/* Initialize the storage.
|
||||||
|
@ -290,15 +320,15 @@ export function init(data) {
|
||||||
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;
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -306,37 +336,49 @@ export function init(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
|
// Import build data
|
||||||
if (PREFERENCES_ALL[`build`]) {
|
if (PREFERENCES_ALL[`build`]) {
|
||||||
(Object.keys(PREFERENCES_ALL[`build`])).forEach((item) => {
|
Object.keys(PREFERENCES_ALL[`build`]).forEach((item) => {
|
||||||
let PREFERENCE = {'name': item, 'existing': false};
|
let PREFERENCE = { name: item, existing: false };
|
||||||
|
|
||||||
PREFERENCE[`existing`] = (
|
PREFERENCE[`existing`] =
|
||||||
((PREFERENCES_ALL[`sync`]) ? (PREFERENCES_ALL[`sync`]).hasOwnProperty(PREFERENCE[`name`]) : false) ||
|
(PREFERENCES_ALL[`sync`]
|
||||||
((PREFERENCES_ALL[`managed`]) ? (PREFERENCES_ALL[`managed`]).hasOwnProperty(PREFERENCE[`name`]) : false) ||
|
? PREFERENCES_ALL[`sync`].hasOwnProperty(PREFERENCE[`name`])
|
||||||
((PREFERENCES_ALL[`local`]) ? (PREFERENCES_ALL[`local`]).hasOwnProperty(PREFERENCE[`local`]) : false)
|
: false) ||
|
||||||
);
|
(PREFERENCES_ALL[`managed`]
|
||||||
|
? PREFERENCES_ALL[`managed`].hasOwnProperty(PREFERENCE[`name`])
|
||||||
|
: false) ||
|
||||||
|
(PREFERENCES_ALL[`local`]
|
||||||
|
? PREFERENCES_ALL[`local`].hasOwnProperty(PREFERENCE[`local`])
|
||||||
|
: false);
|
||||||
|
|
||||||
if (!PREFERENCE[`existing`]) {
|
if (!PREFERENCE[`existing`]) {
|
||||||
write(PREFERENCE[`name`], PREFERENCES_ALL[`build`][PREFERENCE[`name`]], -1);
|
write(
|
||||||
|
PREFERENCE[`name`],
|
||||||
|
PREFERENCES_ALL[`build`][PREFERENCE[`name`]],
|
||||||
|
-1,
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue