remove the reference to the nonexistent session storage

This commit is contained in:
buzz-lightsnack-2007 2024-05-25 09:37:29 +08:00
parent b47a641b88
commit 0915265f40
4 changed files with 79 additions and 79 deletions

View file

@ -2,7 +2,7 @@
Ask product information to Google Gemini. */ Ask product information to Google Gemini. */
// Import the storage management module. // Import the storage management module.
import {global, session, compare} from "/scripts/secretariat.js"; import {global, compare} from "/scripts/secretariat.js";
import hash from "/scripts/utils/hash.js"; import hash from "/scripts/utils/hash.js";
import {URLs} from "/scripts/utils/URLs.js"; import {URLs} from "/scripts/utils/URLs.js";
@ -29,18 +29,18 @@ export default class product {
// Set private variables. // Set private variables.
this.#options = options; this.#options = options;
// Set the status. // Set the status.
this.status = {}; this.status = {};
}; };
/* /*
Check the data with data from the storage. Check the data with data from the storage.
*/ */
async read() { async read() {
if (this.details) { if (this.details) {
// Add the data digest. // Add the data digest.
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[`update`] = !(await (compare([`sites`, this.URL, `snip`], this.snip))); this.status[`update`] = !(await (compare([`sites`, this.URL, `snip`], this.snip)));
}; };
@ -52,9 +52,9 @@ export default class product {
} }
async save() { async save() {
// 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 (Object.hasOwn(this.status, `update`) ? this.status[`update`] : true) { if (Object.hasOwn(this.status, `update`) ? this.status[`update`] : true) {
// Save the snip data. // Save the snip data.
(this.snip) ? await global.write([`sites`, this.URL, `snip`], this.snip, 1) : false; (this.snip) ? await global.write([`sites`, this.URL, `snip`], this.snip, 1) : false;
// Write the analysis data to the storage. // Write the analysis data to the storage.

View file

@ -1,5 +1,5 @@
/* /*
Display the error screen details. Display the error screen details.
*/ */
import Page from "/scripts/pages/page.js"; import Page from "/scripts/pages/page.js";
@ -22,7 +22,7 @@ class Page_Error extends Page {
}; };
async background() { async background() {
// Wait until a change in the session storage. // Wait until a change in the storage.
new background(async (changes) => { new background(async (changes) => {
await this.update(); await this.update();
this.fill(); this.fill();
@ -30,25 +30,25 @@ class Page_Error extends Page {
} }
/* /*
Update the data. Update the data.
*/ */
async update() { async update() {
// Set the reference website when overriding or unset. // Set the reference website when overriding or unset.
if (!this[`ref`]) {this[`ref`] = await pointer.read(`URL`)}; if (!this[`ref`]) {this[`ref`] = await pointer.read(`URL`)};
// Get all the data to be used here. // Get all the data to be used here.
let STORAGE_DATA = await global.read([`sites`, this[`ref`], `status`, `error`], -1); let STORAGE_DATA = await global.read([`sites`, this[`ref`], `status`, `error`], -1);
// Update all other data. // Update all other data.
this[`status`][`error`] = ((STORAGE_DATA && (typeof STORAGE_DATA).includes(`obj`)) ? (Object.keys(STORAGE_DATA).length) : false) this[`status`][`error`] = ((STORAGE_DATA && (typeof STORAGE_DATA).includes(`obj`)) ? (Object.keys(STORAGE_DATA).length) : false)
? STORAGE_DATA ? STORAGE_DATA
// Accomodate data erasure. // Accomodate data erasure.
: ((this[`status`][`error`]) : ((this[`status`][`error`])
? this[`status`][`error`] ? this[`status`][`error`]
: {}); : {});
const parse = (error) => { const parse = (error) => {
// If the error isn't the correct type, try to disect it assuming it's in the stack format. // If the error isn't the correct type, try to disect it assuming it's in the stack format.
this[`status`][`error`] = {}; this[`status`][`error`] = {};
try { try {
@ -57,7 +57,7 @@ class Page_Error extends Page {
"message": (((error.split(`\n`))[0]).split(texts.localized(`delimiter_error`))).slice(1).join(texts.localized(`delimiter_error`)).trim(), "message": (((error.split(`\n`))[0]).split(texts.localized(`delimiter_error`))).slice(1).join(texts.localized(`delimiter_error`)).trim(),
"stack": error.split(`\n`).slice(1).join(`\n`) "stack": error.split(`\n`).slice(1).join(`\n`)
}; };
(Object.keys(FIELDS)).forEach((KEY) => { (Object.keys(FIELDS)).forEach((KEY) => {
this[`status`][`error`][KEY] = (FIELDS[KEY]) ? FIELDS[KEY] : ``; this[`status`][`error`][KEY] = (FIELDS[KEY]) ? FIELDS[KEY] : ``;
}) })
@ -77,7 +77,7 @@ class Page_Error extends Page {
} }
/* /*
Extract the contents of the page. Extract the contents of the page.
*/ */
content () { content () {
this[`elements`] = (this[`elements`]) ? this[`elements`] : {}; this[`elements`] = (this[`elements`]) ? this[`elements`] : {};
@ -85,12 +85,12 @@ class Page_Error extends Page {
const error_display = () => { const error_display = () => {
this[`elements`][`error display`] = {}; this[`elements`][`error display`] = {};
let ERROR_CONTENTS = document.querySelectorAll(`[data-error]`); let ERROR_CONTENTS = document.querySelectorAll(`[data-error]`);
ERROR_CONTENTS.forEach((ELEMENT) => { ERROR_CONTENTS.forEach((ELEMENT) => {
let PROPERTY = ELEMENT.getAttribute(`data-error`).trim(); let PROPERTY = ELEMENT.getAttribute(`data-error`).trim();
this[`elements`][`error display`][PROPERTY] = ELEMENT; this[`elements`][`error display`][PROPERTY] = ELEMENT;
// Remove properties used to construct since it is already saved. // Remove properties used to construct since it is already saved.
ELEMENT.removeAttribute(`data-error`); ELEMENT.removeAttribute(`data-error`);
}); });
}; };
@ -100,7 +100,7 @@ class Page_Error extends Page {
}; };
/* /*
Fill in the content of the page. Fill in the content of the page.
*/ */
async fill () { async fill () {
await this.update(); await this.update();
@ -113,10 +113,10 @@ class Page_Error extends Page {
} }
/* /*
Add event listeners to the page. Add event listeners to the page.
*/ */
events () { events () {
// Add an event listener to the refresh button. // Add an event listener to the refresh button.
(this[`window`][`elements`][`interactive`][`action`] ? this[`window`][`elements`][`interactive`][`action`].length : false) (this[`window`][`elements`][`interactive`][`action`] ? this[`window`][`elements`][`interactive`][`action`].length : false)
? (this[`window`][`elements`][`interactive`][`action`][`refresh`] ? this[`window`][`elements`][`interactive`][`action`][`refresh`].length : false) ? (this[`window`][`elements`][`interactive`][`action`][`refresh`] ? this[`window`][`elements`][`interactive`][`action`][`refresh`].length : false)
? (this[`window`][`elements`][`interactive`][`action`][`refresh`]).forEach((ELEMENT) => { ? (this[`window`][`elements`][`interactive`][`action`][`refresh`]).forEach((ELEMENT) => {
@ -133,7 +133,7 @@ class Page_Error extends Page {
*/ */
send() { send() {
try { try {
// Send a message to the content script. // Send a message to the content script.
Tabs.query(null, 0).then((TAB) => { Tabs.query(null, 0).then((TAB) => {
chrome.tabs.sendMessage(TAB.id, {"refresh": "manual"}); chrome.tabs.sendMessage(TAB.id, {"refresh": "manual"});
}); });
@ -143,4 +143,4 @@ class Page_Error extends Page {
}; };
} }
new Page_Error() new Page_Error()

View file

@ -18,31 +18,31 @@ class Page_Popup extends Page {
}; };
async background() { async background() {
// Wait until a change in the session storage. // Wait until a change in the storage.
new background((changes) => { new background((changes) => {
this.update(); this.update();
this.switch(); this.switch();
}); });
} }
/* /*
Update the data used by the page. Update the data used by the page.
@param {boolean} override override the current data. @param {boolean} override override the current data.
*/ */
async update(override = false) { async update(override = false) {
// Set the reference website when overriding or unset. // Set the reference website when overriding or unset.
if (override || !this[`ref`]) {this[`ref`] = await global.read([`last`])}; if (override || !this[`ref`]) {this[`ref`] = await global.read([`last`])};
// Get all the data to be used here. // Get all the data to be used here.
let DATA = {}; let DATA = {};
DATA[`status`] = await global.read([`sites`, this[`ref`], `status`], -1); DATA[`status`] = await global.read([`sites`, this[`ref`], `status`], -1);
DATA[`init`] = (await global.read([`init`])) && (await global.read([`settings`,`analysis`,`api`,`key`])); DATA[`init`] = (await global.read([`init`])) && (await global.read([`settings`,`analysis`,`api`,`key`]));
// Update all other data. // Update all other data.
this[`status`] = (DATA[`status`] != null) this[`status`] = (DATA[`status`] != null)
? DATA[`status`] ? DATA[`status`]
// Accomodate data erasure. // Accomodate data erasure.
: ((this[`status`]) : ((this[`status`])
? this[`status`] ? this[`status`]
: {}); : {});
@ -65,15 +65,15 @@ class Page_Popup extends Page {
"OOBE": "hello.htm", "OOBE": "hello.htm",
"error": "error.htm" "error": "error.htm"
}; };
// Set the width and the height. // Set the width and the height.
const PAGES_DIMENSIONS = { const PAGES_DIMENSIONS = {
"loading": {"width": "200pt", "height": "100pt"}, "loading": {"width": "200pt", "height": "100pt"},
"error": {"width": "250pt", "height": "300pt"}, "error": {"width": "250pt", "height": "300pt"},
"results": {"width": "250pt", "height": "225pt"}, "results": {"width": "250pt", "height": "225pt"},
"OOBE": {"width": "350pt", "height": "300pt"}, "OOBE": {"width": "350pt", "height": "300pt"},
}; };
// Prepare all the necessary data. // Prepare all the necessary data.
this.update().then(() => { this.update().then(() => {
// Make sure that the website has been selected! // Make sure that the website has been selected!
if (this[`ref`]) { if (this[`ref`]) {
@ -85,20 +85,20 @@ class Page_Popup extends Page {
: `loading`) : `loading`)
: `OOBE`; : `OOBE`;
let PAGE = chrome.runtime.getURL(`pages/popup/`.concat(PAGES[SELECTION])); let PAGE = chrome.runtime.getURL(`pages/popup/`.concat(PAGES[SELECTION]));
// Replace the iframe src with the new page. // Replace the iframe src with the new page.
(this.elements[`frame`].src != PAGE) ? this.elements[`frame`].src = PAGE : false; (this.elements[`frame`].src != PAGE) ? this.elements[`frame`].src = PAGE : false;
// The results and OOBE pages has its own container. // The results and OOBE pages has its own container.
this.elements[`container`].classList[([`results`, `OOBE`].includes(SELECTION)) ? `remove` : `add`](`container`); this.elements[`container`].classList[([`results`, `OOBE`].includes(SELECTION)) ? `remove` : `add`](`container`);
// Set the title bar content. // Set the title bar content.
this[`window`][`navigation bar`][([`OOBE`, `loading`].includes(SELECTION)) ? `hide` : `show`](`header`, `result`); this[`window`][`navigation bar`][([`OOBE`, `loading`].includes(SELECTION)) ? `hide` : `show`](`header`, `result`);
[`loading`, `OOBE`].forEach((NAME) => { [`loading`, `OOBE`].forEach((NAME) => {
this[`window`][`navigation bar`][(NAME == SELECTION) ? `show` : `hide`](`header`, NAME); this[`window`][`navigation bar`][(NAME == SELECTION) ? `show` : `hide`](`header`, NAME);
}); });
// Set the dimensions of the body. // Set the dimensions of the body.
Object.keys(PAGES_DIMENSIONS[SELECTION]).forEach((DIMENSION) => { Object.keys(PAGES_DIMENSIONS[SELECTION]).forEach((DIMENSION) => {
document.body.style[DIMENSION] = PAGES_DIMENSIONS[SELECTION][DIMENSION]; document.body.style[DIMENSION] = PAGES_DIMENSIONS[SELECTION][DIMENSION];
}); });
@ -106,18 +106,18 @@ class Page_Popup extends Page {
}); });
} }
// Also set the loader. // Also set the loader.
(this[`elements`][`loader`]) (this[`elements`][`loader`])
? ((this[`status`] ? (this[`status`][`done`] ? (this[`status`][`done`] <= 1) : false) : false) ? parseFloat(this[`elements`][`loader`].update(this[`status`][`done`])) : false) ? ((this[`status`] ? (this[`status`][`done`] ? (this[`status`][`done`] <= 1) : false) : false) ? parseFloat(this[`elements`][`loader`].update(this[`status`][`done`])) : false)
: false : false
}; };
async content() { async content() {
this.elements = {}; this.elements = {};
this.elements[`container`] = document.querySelector(`main`); this.elements[`container`] = document.querySelector(`main`);
this.elements[`frame`] = document.querySelector(`main > iframe.viewer`); this.elements[`frame`] = document.querySelector(`main > iframe.viewer`);
this.elements[`nav`] = document.querySelector(`nav`); this.elements[`nav`] = document.querySelector(`nav`);
// Check if the frame is available. // Check if the frame is available.
if (this.elements[`frame`]) { if (this.elements[`frame`]) {
this.switch(); this.switch();
@ -132,14 +132,14 @@ class Page_Popup extends Page {
}; };
/* /*
Call for the scraper and analyzer. Call for the scraper and analyzer.
*/ */
send(options) { send(options) {
// Make sure that it is the correct format. // Make sure that it is the correct format.
let OPTIONS = (options && (typeof options).includes(`obj`) && !Array.isArray(options)) ? options : {}; let OPTIONS = (options && (typeof options).includes(`obj`) && !Array.isArray(options)) ? options : {};
try { try {
// Send a message to the content script. // Send a message to the content script.
Tabs.query(null, 0).then((TAB) => { Tabs.query(null, 0).then((TAB) => {
chrome.tabs.sendMessage(TAB.id, OPTIONS); chrome.tabs.sendMessage(TAB.id, OPTIONS);
}); });
@ -153,7 +153,7 @@ class Page_Popup extends Page {
ACTIONS[`open,settings`] = () => {chrome.runtime.openOptionsPage();}; ACTIONS[`open,settings`] = () => {chrome.runtime.openOptionsPage();};
ACTIONS[`analysis,reload`] = () => {this.send({"refresh": "manual"});} ACTIONS[`analysis,reload`] = () => {this.send({"refresh": "manual"});}
// Add the event listeners. // Add the event listeners.
(Object.keys(ACTIONS)).forEach((NAME) => { (Object.keys(ACTIONS)).forEach((NAME) => {
(this.window.elements[`interactive`][`action`][NAME] ? this.window.elements[`interactive`][`action`][NAME].length : false) (this.window.elements[`interactive`][`action`][NAME] ? this.window.elements[`interactive`][`action`][NAME].length : false)
? this.window.elements[`interactive`][`action`][NAME].forEach((ELEMENT) => { ? this.window.elements[`interactive`][`action`][NAME].forEach((ELEMENT) => {
@ -164,4 +164,4 @@ class Page_Popup extends Page {
} }
} }
new Page_Popup(); new Page_Popup();

View file

@ -17,68 +17,68 @@ class Page_Results extends Page {
}; };
async background() { async background() {
// Wait until a change in the session storage. // Wait until a change in the storage.
new background((changes) => { new background((changes) => {
this.update(); this.update();
this.content(); this.content();
// First, update site data but retain the URL. // First, update site data but retain the URL.
}); });
} }
/* /*
Update the data used by the page. Update the data used by the page.
@param {boolean} override override the current data. @param {boolean} override override the current data.
*/ */
async update(override = false) { async update(override = false) {
// Set the reference website when overriding or unset. // Set the reference website when overriding or unset.
if (override || !this[`ref`]) { if (override || !this[`ref`]) {
let RECORD = await global.read([`last`]); let RECORD = await global.read([`last`]);
(RECORD) ? this[`ref`] = RECORD : false; (RECORD) ? this[`ref`] = RECORD : false;
}; };
if (this[`ref`]) { if (this[`ref`]) {
// Get all the data. // Get all the data.
let DATA = { let DATA = {
"data": await global.read([`sites`, this[`ref`]]) "data": await global.read([`sites`, this[`ref`]])
} }
// Set the data. // Set the data.
this[`data`] = (DATA[`data`] && (typeof DATA[`data`]).includes(`obj`)) ? DATA[`data`] : (this[`data`] ? this[`data`] : {}); this[`data`] = (DATA[`data`] && (typeof DATA[`data`]).includes(`obj`)) ? DATA[`data`] : (this[`data`] ? this[`data`] : {});
} }
} }
async content() { async content() {
// Select all the elements and add it to the object. // Select all the elements and add it to the object.
if (document.querySelectorAll(`[data-active-result]`)) { if (document.querySelectorAll(`[data-active-result]`)) {
this.elements = {} this.elements = {}
document.querySelectorAll(`[data-active-result]`).forEach((ELEMENT) => { document.querySelectorAll(`[data-active-result]`).forEach((ELEMENT) => {
let PROPERTY = ELEMENT.getAttribute(`data-active-result`).trim(); let PROPERTY = ELEMENT.getAttribute(`data-active-result`).trim();
this.elements[PROPERTY] = ELEMENT; this.elements[PROPERTY] = ELEMENT;
// Copy the expected type of sub-elements, if any. // Copy the expected type of sub-elements, if any.
if (ELEMENT.getAttribute(`data-active-result-type`)) { if (ELEMENT.getAttribute(`data-active-result-type`)) {
this.elements[PROPERTY][`target element type`] = ELEMENT.getAttribute(`data-active-result-type`).trim(); this.elements[PROPERTY][`target element type`] = ELEMENT.getAttribute(`data-active-result-type`).trim();
ELEMENT.removeAttribute(`data-active-result-type`); ELEMENT.removeAttribute(`data-active-result-type`);
}; };
// Remove the construction data active result. // Remove the construction data active result.
ELEMENT.removeAttribute(`data-active-result`); ELEMENT.removeAttribute(`data-active-result`);
}); });
} }
await this.update(); await this.update();
this.fill(); this.fill();
} }
/* /*
Resize the window to fit the content. Resize the window to fit the content.
*/ */
async resize() { async resize() {
} }
/* /*
Populate the contents. Populate the contents.
*/ */
async fill() { async fill() {
@ -87,7 +87,7 @@ class Page_Results extends Page {
? (Object.keys(this.elements)).forEach(async (SOURCE) => { ? (Object.keys(this.elements)).forEach(async (SOURCE) => {
if (SOURCE.indexOf(`*`) < SOURCE.length - 1) { if (SOURCE.indexOf(`*`) < SOURCE.length - 1) {
let DATA = (nested.dictionary.get(this[`data`][`analysis`], SOURCE)); let DATA = (nested.dictionary.get(this[`data`][`analysis`], SOURCE));
this.elements[SOURCE][(this.elements[SOURCE].nodeName.toLowerCase().includes(`input`) || this.elements[SOURCE].nodeName.toLowerCase().includes(`progress`)) ? `value` : `innerHTML`] = (DATA) this.elements[SOURCE][(this.elements[SOURCE].nodeName.toLowerCase().includes(`input`) || this.elements[SOURCE].nodeName.toLowerCase().includes(`progress`)) ? `value` : `innerHTML`] = (DATA)
? (((typeof DATA).includes(`obj`) && !Array.isArray(DATA)) ? (((typeof DATA).includes(`obj`) && !Array.isArray(DATA))
? JSON.stringify(DATA) ? JSON.stringify(DATA)
@ -95,36 +95,36 @@ class Page_Results extends Page {
: null; : null;
} else if (SOURCE.indexOf(`*`) >= SOURCE.length - 1) { } else if (SOURCE.indexOf(`*`) >= SOURCE.length - 1) {
let DATA = (nested.dictionary.get(this[`data`][`analysis`], SOURCE.split(`,`).slice(0, -1))); let DATA = (nested.dictionary.get(this[`data`][`analysis`], SOURCE.split(`,`).slice(0, -1)));
(!Array.isArray(DATA) && (typeof DATA).includes(`obj`) && DATA != null) (!Array.isArray(DATA) && (typeof DATA).includes(`obj`) && DATA != null)
let ELEMENT_TYPES = { let ELEMENT_TYPES = {
"container": "section", "container": "section",
"content": "article", "content": "article",
"title": "p", "title": "p",
"body": "p" "body": "p"
}; };
(DATA) (DATA)
? (Object.keys(DATA)).forEach((ITEM) => { ? (Object.keys(DATA)).forEach((ITEM) => {
let ELEMENTS = {}; let ELEMENTS = {};
// Create the elements. // Create the elements.
(Object.keys(ELEMENT_TYPES)).forEach((TYPE) => { (Object.keys(ELEMENT_TYPES)).forEach((TYPE) => {
ELEMENTS[TYPE] = document.createElement(ELEMENT_TYPES[TYPE]); ELEMENTS[TYPE] = document.createElement(ELEMENT_TYPES[TYPE]);
(([`content`, `action`, `title`].includes(TYPE) || TYPE.includes(`container`)) && this.elements[SOURCE][`target element type`]) (([`content`, `action`, `title`].includes(TYPE) || TYPE.includes(`container`)) && this.elements[SOURCE][`target element type`])
? ELEMENTS[TYPE].classList.add(this.elements[SOURCE][`target element type`].concat((!TYPE.includes(`container`)) ? ELEMENTS[TYPE].classList.add(this.elements[SOURCE][`target element type`].concat((!TYPE.includes(`container`))
? `-${TYPE}` ? `-${TYPE}`
: ``)) : ``))
: false; : false;
}); });
ELEMENTS[`title`].innerText = String(ITEM).trim(); ELEMENTS[`title`].innerText = String(ITEM).trim();
ELEMENTS[`title`].classList.add(`flow-text`); ELEMENTS[`title`].classList.add(`flow-text`);
ELEMENTS[`body`].innerText = String(DATA[ITEM]).trim(); ELEMENTS[`body`].innerText = String(DATA[ITEM]).trim();
// Inject the elements. // Inject the elements.
[`title`, `body`].forEach((CONTENT) => { [`title`, `body`].forEach((CONTENT) => {
ELEMENTS[`content`].appendChild(ELEMENTS[CONTENT]); ELEMENTS[`content`].appendChild(ELEMENTS[CONTENT]);
}); });
@ -135,11 +135,11 @@ class Page_Results extends Page {
} }
}) })
: false; : false;
// Set the color. // Set the color.
(nested.dictionary.get(this[`data`][`analysis`], [`Rating`, `Trust`]) && document.querySelector(`summary`)) ? document.querySelector(`summary`).setAttribute(`result`, (nested.dictionary.get(this[`data`][`analysis`], [`Rating`, `Trust`]))) : false; (nested.dictionary.get(this[`data`][`analysis`], [`Rating`, `Trust`]) && document.querySelector(`summary`)) ? document.querySelector(`summary`).setAttribute(`result`, (nested.dictionary.get(this[`data`][`analysis`], [`Rating`, `Trust`]))) : false;
} }
}; };
} }
new Page_Results(); new Page_Results();