move hash to utilities folder

This commit is contained in:
H. Saw 2024-04-26 03:00:42 +00:00
parent b4ea93911e
commit 0b7be21d03
3 changed files with 51 additions and 51 deletions

View file

@ -3,14 +3,14 @@ Ask product information to Google Gemini. */
// Import the storage management module. // Import the storage management module.
const secretariat = await import(chrome.runtime.getURL("scripts/secretariat.js")); const secretariat = await import(chrome.runtime.getURL("scripts/secretariat.js"));
import hash from "/scripts/strings/hash.js"; import hash from "/scripts/utils/hash.js";
import texts from "/scripts/strings/read.js"; import texts from "/scripts/strings/read.js";
// Don't forget to set the class as export default. // Don't forget to set the class as export default.
export default class product { export default class product {
// Create private variables for explicit use for the storage. // Create private variables for explicit use for the storage.
#snip; #snip;
#options; #options;
/* Initialize a new product with its details. /* Initialize a new product with its details.
@ -25,11 +25,11 @@ export default class product {
/* Remove uneeded data or formatting from the URL and the data. */ /* Remove uneeded data or formatting from the URL and the data. */
let clean = (URL) => { let clean = (URL) => {
// Remove the protocol from the URL. // Remove the protocol from the URL.
return((URL.replace(/(^\w+:|^)\/\//, ``).split(`?`))[0]); return((URL.replace(/(^\w+:|^)\/\//, ``).split(`?`))[0]);
} }
// Set this product's details as part of the object's properties. // Set this product's details as part of the object's properties.
this.URL = clean(URL); this.URL = clean(URL);
this.details = details; this.details = details;
@ -39,22 +39,22 @@ export default class product {
/* Attach the product data to the storage. */ /* Attach the product data to the storage. */
async attach() { async attach() {
// Add the data digest. // Add the data digest.
this.#snip = (await hash.digest(this.details, {"output": "Number"})); this.#snip = (await hash.digest(this.details, {"output": "Number"}));
// Add the status about this data. // Add the status about this data.
this.status = {}; this.status = {};
this.status[`update`] = !secretariat.compare([`sites`, this.URL, `snip`], this.#snip); this.status[`update`] = !secretariat.compare([`sites`, this.URL, `snip`], this.#snip);
} }
async save() { async save() {
// Stop when not attached (basically, not entirely initialized). // Stop when not attached (basically, not entirely initialized).
if (!this.#snip) {throw new ReferenceError((new texts(`error_msg_notattached`)).localized)}; if (!this.#snip) {throw new ReferenceError((new texts(`error_msg_notattached`)).localized)};
// Save the data to the storage. // Save the data to the storage.
await secretariat.write([`sites`, this.URL, `snip`], this.#snip, 1); await secretariat.write([`sites`, this.URL, `snip`], this.#snip, 1);
// Write the analysis data to the storage. // Write the analysis data to the storage.
(this[`analysis`]) ? secretariat.write([`sites`, this.URL, `analysis`], this.analysis, 1): false; (this[`analysis`]) ? secretariat.write([`sites`, this.URL, `analysis`], this.analysis, 1): false;
}; };
@ -63,21 +63,21 @@ export default class product {
if (this[`analysis`]) {return(this.analysis)} if (this[`analysis`]) {return(this.analysis)}
else if (this.status ? (!this.status.update) : false) {this.analysis = await secretariat.read([`sites`, this.URL, `analysis`]);} else if (this.status ? (!this.status.update) : false) {this.analysis = await secretariat.read([`sites`, this.URL, `analysis`]);}
if ((this.analysis && this.analysis != null && this.analysis != undefined) ? !((typeof this.analysis).includes(`obj`) && !Array.isArray(this.analysis)) : true) { if ((this.analysis && this.analysis != null && this.analysis != undefined) ? !((typeof this.analysis).includes(`obj`) && !Array.isArray(this.analysis)) : true) {
// Analyze the data. // Analyze the data.
const gemini = (await import(chrome.runtime.getURL("scripts/AI/gemini.js"))).default; const gemini = (await import(chrome.runtime.getURL("scripts/AI/gemini.js"))).default;
let analyzer = new gemini (await secretariat.read([`settings`,`analysis`,`api`,`key`]), `gemini-pro`); let analyzer = new gemini (await secretariat.read([`settings`,`analysis`,`api`,`key`]), `gemini-pro`);
// Analyze the data. // Analyze the data.
let PROMPT = []; let PROMPT = [];
// Add the prompt. // Add the prompt.
PROMPT.push({"text": ((new texts(`AI_message_prompt`)).localized).concat(JSON.stringify(this.details))}); PROMPT.push({"text": ((new texts(`AI_message_prompt`)).localized).concat(JSON.stringify(this.details))});
// Run the analysis. // Run the analysis.
await analyzer.generate(PROMPT); await analyzer.generate(PROMPT);
if (analyzer.candidate) { if (analyzer.candidate) {
// Remove all markdown formatting. // Remove all markdown formatting.
this.analysis = JSON.parse(analyzer.candidate.replace(/(```json|```|`)/g, '')); this.analysis = JSON.parse(analyzer.candidate.replace(/(```json|```|`)/g, ''));
}; };
}; };

View file

@ -4,7 +4,7 @@ Manage the local cache.
import logging from "/scripts/logging.js"; import logging from "/scripts/logging.js";
import texts from "/scripts/strings/read.js"; import texts from "/scripts/strings/read.js";
import hash from "/scripts/strings/hash.js"; import hash from "/scripts/utils/hash.js";
/* Read all stored data in the browser cache. /* Read all stored data in the browser cache.
@ -18,7 +18,7 @@ export async function read(DATA_NAME, CLOUD = 0) {
let DATA, DATA_RETURNED; let DATA, DATA_RETURNED;
/* /*
Get all storage values. Get all storage values.
@param {number} SOURCE the data source @param {number} SOURCE the data source
*/ */
@ -94,7 +94,7 @@ export async function read(DATA_NAME, CLOUD = 0) {
} }
switch (CLOUD) { switch (CLOUD) {
case 0: case 0:
DATA = {}; DATA_RETURNED = {}; DATA = {}; DATA_RETURNED = {};
DATA[`sync`] = await read((DATA_NAME) ? [...DATA_NAME] : null, 1); DATA[`sync`] = await read((DATA_NAME) ? [...DATA_NAME] : null, 1);
@ -106,7 +106,7 @@ export async function read(DATA_NAME, CLOUD = 0) {
return DATA_RETURNED[`value`]; return DATA_RETURNED[`value`];
break; break;
default: default:
CLOUD = (CLOUD > 0) ? 1 : -1; CLOUD = (CLOUD > 0) ? 1 : -1;
DATA = await read_database(CLOUD); DATA = await read_database(CLOUD);
DATA_RETURNED = (DATA_NAME) ? find_data(DATA, DATA_NAME) : DATA; DATA_RETURNED = (DATA_NAME) ? find_data(DATA, DATA_NAME) : DATA;
@ -138,7 +138,7 @@ export async function search(SOURCE, TERM, ADDITIONAL_PLACES, STRICT = 0, OPTION
RESULTS[DATA_NAME] = DATA[DATA_NAME]; RESULTS[DATA_NAME] = DATA[DATA_NAME];
} }
}); });
// Then, get the additional places. // Then, get the additional places.
if ((ADDITIONAL_PLACES != null ? Array.isArray(ADDITIONAL_PLACES) : false) ? ADDITIONAL_PLACES.length > 0 : false) { if ((ADDITIONAL_PLACES != null ? Array.isArray(ADDITIONAL_PLACES) : false) ? ADDITIONAL_PLACES.length > 0 : false) {
for (let PARAMETER_PRIORITY_NUMBER = 0; PARAMETER_PRIORITY_NUMBER < ADDITIONAL_PLACES.length; PARAMETER_PRIORITY_NUMBER++) { for (let PARAMETER_PRIORITY_NUMBER = 0; PARAMETER_PRIORITY_NUMBER < ADDITIONAL_PLACES.length; PARAMETER_PRIORITY_NUMBER++) {
@ -147,15 +147,15 @@ export async function search(SOURCE, TERM, ADDITIONAL_PLACES, STRICT = 0, OPTION
}; };
} }
} else if (((typeof ADDITIONAL_PLACES).includes(`str`) && (ADDITIONAL_PLACES)) ? ADDITIONAL_PLACES.trim() : false) { } else if (((typeof ADDITIONAL_PLACES).includes(`str`) && (ADDITIONAL_PLACES)) ? ADDITIONAL_PLACES.trim() : false) {
// Perform a sequential search on the data. // Perform a sequential search on the data.
if ((typeof DATA).includes(`obj`) && !Array.isArray(DATA) && SOURCE != null) { if ((typeof DATA).includes(`obj`) && !Array.isArray(DATA) && SOURCE != null) {
let VALUE = {}; let VALUE = {};
for (let DICTIONARY_INDEX = 0; DICTIONARY_INDEX < (Object.keys(DATA)).length; DICTIONARY_INDEX++) { for (let DICTIONARY_INDEX = 0; DICTIONARY_INDEX < (Object.keys(DATA)).length; DICTIONARY_INDEX++) {
VALUE[`parent`] = DATA[(Object.keys(DATA))[DICTIONARY_INDEX]]; VALUE[`parent`] = DATA[(Object.keys(DATA))[DICTIONARY_INDEX]];
/* Test for a valid RegEx. /* Test for a valid RegEx.
@param {string} item the item to test @param {string} item the item to test
*/ */
function isRegEx(item) { function isRegEx(item) {
@ -174,7 +174,7 @@ export async function search(SOURCE, TERM, ADDITIONAL_PLACES, STRICT = 0, OPTION
} }
if (VALUE[`current`] ? ((STRICT >= 1) ? VALUE[`current`] == TERM : (((STRICT < 0.5) ? (VALUE[`current`].includes(TERM)) : false) || TERM.includes(VALUE[`current`]) || (isRegEx(VALUE[`current`]) ? (new RegExp(VALUE[`current`])).test(TERM) : false))) : false) { if (VALUE[`current`] ? ((STRICT >= 1) ? VALUE[`current`] == TERM : (((STRICT < 0.5) ? (VALUE[`current`].includes(TERM)) : false) || TERM.includes(VALUE[`current`]) || (isRegEx(VALUE[`current`]) ? (new RegExp(VALUE[`current`])).test(TERM) : false))) : false) {
// Add the data. // Add the data.
RESULTS[(Object.keys(DATA))[DICTIONARY_INDEX]] = (Object.entries(DATA))[DICTIONARY_INDEX][1]; RESULTS[(Object.keys(DATA))[DICTIONARY_INDEX]] = (Object.entries(DATA))[DICTIONARY_INDEX][1];
}; };
}; };
@ -183,7 +183,7 @@ export async function search(SOURCE, TERM, ADDITIONAL_PLACES, STRICT = 0, OPTION
if ( if (
((STRICT || (typeof DATA[ELEMENT_INDEX]).includes(`num`)) && DATA[ELEMENT_INDEX] == TERM) || ((STRICT || (typeof DATA[ELEMENT_INDEX]).includes(`num`)) && DATA[ELEMENT_INDEX] == TERM) ||
((!STRICT && !((typeof DATA[ELEMENT_INDEX]).includes(`num`))) ((!STRICT && !((typeof DATA[ELEMENT_INDEX]).includes(`num`)))
? (TERM.includes(DATA[ELEMENT_INDEX]) || DATA[ELEMENT_INDEX].includes(TERM) || ? (TERM.includes(DATA[ELEMENT_INDEX]) || DATA[ELEMENT_INDEX].includes(TERM) ||
(typeof(DATA[ELEMENT_INDEX])).includes(`str`) (typeof(DATA[ELEMENT_INDEX])).includes(`str`)
? new RegExp(DATA[ELEMENT_INDEX]).test(TERM) ? new RegExp(DATA[ELEMENT_INDEX]).test(TERM)
: false : false
@ -197,7 +197,7 @@ export async function search(SOURCE, TERM, ADDITIONAL_PLACES, STRICT = 0, OPTION
} }
} }
} }
return RESULTS; return RESULTS;
} }
@ -210,7 +210,7 @@ export async function search(SOURCE, TERM, ADDITIONAL_PLACES, STRICT = 0, OPTION
export async function write(PATH, DATA, CLOUD = -1) { export async function write(PATH, DATA, CLOUD = -1) {
let DATA_INJECTED = {}; let DATA_INJECTED = {};
// Inform the user that saving is in progress. // Inform the user that saving is in progress.
let notification = new logging ((new texts(`saving_current`)).localized, (new texts(`saving_current_message`)).localized, false); let notification = new logging ((new texts(`saving_current`)).localized, (new texts(`saving_current_message`)).localized, false);
/* Forcibly write the data to chrome database /* Forcibly write the data to chrome database
@ -252,12 +252,12 @@ export async function write(PATH, DATA, CLOUD = -1) {
async function verify (NAME, DATA) { async function verify (NAME, DATA) {
let DATA_CHECK = {}; let DATA_CHECK = {};
// Verify the presence of the data. // Verify the presence of the data.
DATA_CHECK[`state`] = await compare(NAME, DATA); DATA_CHECK[`state`] = await compare(NAME, DATA);
if (!DATA_CHECK[`state`]) {logging.error((new texts(`error_msg_save_failed`)).localized, String(PATH), JSON.stringify(DATA))} else { if (!DATA_CHECK[`state`]) {logging.error((new texts(`error_msg_save_failed`)).localized, String(PATH), JSON.stringify(DATA))} else {
// Inform the user that the saving operation is completed. // Inform the user that the saving operation is completed.
notification = new logging (new texts(`saving_done`).localized); notification = new logging (new texts(`saving_done`).localized);
}; };
return (DATA_CHECK[`state`]); return (DATA_CHECK[`state`]);
@ -323,7 +323,7 @@ class session {
return (DATA[`selected`]); return (DATA[`selected`]);
} }
static async write(PATH, DATA) { static async write(PATH, 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
@ -333,7 +333,7 @@ class session {
*/ */
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();
@ -348,7 +348,7 @@ class session {
// Return the value. // Return the value.
return DATABASE; return DATABASE;
} }
/* Forcibly write the data to chrome database /* Forcibly write the data to chrome database
@param {object} DATA the data @param {object} DATA the data
@ -369,20 +369,20 @@ class session {
DATA[`inject`] = nest(DATA[`all`], [...TARGET], DATA[`write`]); DATA[`inject`] = nest(DATA[`all`], [...TARGET], DATA[`write`]);
// Write! // Write!
store(DATA[`inject`]); store(DATA[`inject`]);
} }
} }
/* Temporarily hold data in browser session storage. /* Temporarily hold data in browser session storage.
@param {string} PATH the name @param {string} PATH the name
@param {object} DATA the data to hold @param {object} DATA the data to hold
*/ */
export async function dump(PATH, DATA) { export async function dump(PATH, DATA) {
} }
/* Compare a data against the stored data. Useful when comparing dictionaries. /* Compare a data against the stored data. Useful when comparing dictionaries.
@param {string} PATH the name @param {string} PATH the name
@param {object} DATA the data to compare to @param {object} DATA the data to compare to
@ -390,22 +390,22 @@ export async function dump(PATH, DATA) {
export async function compare(PATH, DATA) { export async function compare(PATH, DATA) {
/* The actual comparison of data. */ /* The actual comparison of data. */
async function comparison(DATA_ONE, DATA_TWO) { async function comparison(DATA_ONE, DATA_TWO) {
let RESULT = true; let RESULT = true;
// The first round of checking is on the data type. // The first round of checking is on the data type.
console.log(DATA_ONE, DATA_TWO); console.log(DATA_ONE, DATA_TWO);
RESULT = ((typeof DATA_ONE == typeof DATA_TWO) ? ((Array.isArray(DATA_TWO) == Array.isArray(DATA_ONE)) && !((DATA_ONE == null && DATA_TWO != null) || (DATA_ONE != null && DATA_TWO == null))) : false) ? ((typeof DATA_ONE).includes(`obj`) ? (await hash.digest(DATA_ONE, {"output": "Number"}) == await hash.digest(DATA_TWO, {"output": "Number"})) : DATA_ONE == DATA_TWO) : false; RESULT = ((typeof DATA_ONE == typeof DATA_TWO) ? ((Array.isArray(DATA_TWO) == Array.isArray(DATA_ONE)) && !((DATA_ONE == null && DATA_TWO != null) || (DATA_ONE != null && DATA_TWO == null))) : false) ? ((typeof DATA_ONE).includes(`obj`) ? (await hash.digest(DATA_ONE, {"output": "Number"}) == await hash.digest(DATA_TWO, {"output": "Number"})) : DATA_ONE == DATA_TWO) : false;
return (RESULT); return (RESULT);
} }
let COMPARISON = {}; let COMPARISON = {};
COMPARISON[`test`] = (PATH) ? DATA : DATA[1]; COMPARISON[`test`] = (PATH) ? DATA : DATA[1];
COMPARISON[`against`] = (PATH) ? (await read((Array.isArray(PATH)) ? [...PATH] : PATH)) : DATA[0]; COMPARISON[`against`] = (PATH) ? (await read((Array.isArray(PATH)) ? [...PATH] : PATH)) : DATA[0];
COMPARISON[`result`] = comparison(COMPARISON[`against`], COMPARISON[`test`]); COMPARISON[`result`] = comparison(COMPARISON[`against`], COMPARISON[`test`]);
// Return the result. // Return the result.
return (COMPARISON[`result`]); return (COMPARISON[`result`]);
} }
@ -426,13 +426,13 @@ export async function forget(preference, CLOUD = 0, override = false) {
if (!(Array.isArray(preference))) { if (!(Array.isArray(preference))) {
preference = String(preference).trim().split(","); preference = String(preference).trim().split(",");
}; };
let DATA = await read((preference.length > 1) ? [...preference.slice(0,-1)] : null, CLOUD); let DATA = await read((preference.length > 1) ? [...preference.slice(0,-1)] : null, CLOUD);
if (((((typeof (DATA)).includes(`obj`) && !Array.isArray(DATA) && DATA != null) ? Object.keys(DATA) : false) ? Object.keys(DATA).includes((preference.slice(-1))[0]) : false)) { if (((((typeof (DATA)).includes(`obj`) && !Array.isArray(DATA) && DATA != null) ? Object.keys(DATA) : false) ? Object.keys(DATA).includes((preference.slice(-1))[0]) : false)) {
delete DATA[preference.slice(-1)]; delete DATA[preference.slice(-1)];
}; };
await write(preference.slice(0,-1), DATA, CLOUD); await write(preference.slice(0,-1), DATA, CLOUD);
}; };
@ -539,4 +539,4 @@ export function observe(reaction) {
}); });
} }
export {session} export {session}