Save the status to the local product data

This commit is contained in:
buzz-lightsnack-2007 2024-05-05 09:06:27 +08:00
parent 0ff26c8bea
commit 785e6e8e11

View file

@ -36,6 +36,9 @@ export default class product {
// Set private variables. // Set private variables.
this.#options = options; this.#options = options;
// Set the status.
this.status = {};
}; };
/* Attach the product data to the storage. */ /* Attach the product data to the storage. */
@ -44,7 +47,6 @@ export default class product {
this.#snip = (await hash.digest(this.details, {"output": "Array"})); this.#snip = (await hash.digest(this.details, {"output": "Array"}));
// Add the status about this data. // Add the status about this data.
this.status = {};
this.status[`update`] = !(await (compare([`sites`, this.URL, `snip`], this.#snip))); this.status[`update`] = !(await (compare([`sites`, this.URL, `snip`], this.#snip)));
} }
@ -55,34 +57,33 @@ export default class product {
// There is only a need to save the data if an update is needed. // There is only a need to save the data if an update is needed.
if (this.status[`update`]) { if (this.status[`update`]) {
// Save the data to the storage. // Save the data to the storage.
await global.write([`sites`, this.URL, `status`], this.status, -1);
await global.write([`sites`, this.URL, `snip`], this.#snip, 1); await global.write([`sites`, this.URL, `snip`], this.#snip, 1);
// Write the analysis data to the storage. // Write the analysis data to the storage.
(this[`analysis`]) ? global.write([`sites`, this.URL, `analysis`], this.analysis, 1): false; (this[`analysis`]) ? global.write([`sites`, this.URL, `analysis`], this.analysis, 1): false;
} }
}; };
async analyze() { async analyze() {
// Stop when the data is already analyzed. // Stop when the data is already analyzed.
if (this[`analysis`]) {return(this.analysis)} if (this[`analysis`]) {return(this.analysis)}
else if (this.status ? (!this.status.update) : false) {this.analysis = await global.read([`sites`, this.URL, `analysis`]);} else if (this.status ? (!this.status.update) : false) {this.analysis = await global.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.
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 global.read([`settings`,`analysis`,`api`,`key`]), `gemini-pro`); let analyzer = new gemini (await global.read([`settings`,`analysis`,`api`,`key`]), `gemini-pro`);
// Analyze the data.
let PROMPT = [];
// Add the prompt. // Add the prompt.
let 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);
// Raise an error if the product analysis is blocked. // Raise an error if the product analysis is blocked.
if (analyzer.blocked) { this.status[`blocked`] = analyzer.blocked;
if (this.status[`blocked`]) {
throw new Error((new texts(`error_msg_blocked`)).localized) throw new Error((new texts(`error_msg_blocked`)).localized)
}; };