move scripts for non-extension pages

This commit is contained in:
buzz-lightsnack-2007 2024-04-15 13:31:39 +08:00
parent 4d20cdec8d
commit 9dfdb3f44b
5 changed files with 5 additions and 6 deletions

0
scripts/external/inject.js vendored Normal file
View file

21
scripts/external/processor.js vendored Normal file
View file

@ -0,0 +1,21 @@
/* processor.js
Process the information on the website and display it on screen.
*/
// const inject = ((await import(chrome.runtime.getURL("scripts/external/inject.js"))).default);
const scraper = (await import(chrome.runtime.getURL("scripts/external/scraper.js"))).default;
export default class processor {
#filter;
async scrape (fields) {
this.data = new scraper ((fields) ? fields : this.targets);
}
constructor (filter) {
this.#filter = filter;
this.targets = this.#filter[`data`];
this.scrape();
}
}

57
scripts/external/scraper.js vendored Normal file
View file

@ -0,0 +1,57 @@
/* reader.js
Read the contents of the page.
*/
export default class scraper {
constructor(scraper_fields) {
let field_content;
if ((typeof scraper_fields).includes("object") && scraper_fields != null && scraper_fields) {
/* Read for the particular fields. */
function read(fields) {
let field_data = {};
console.log(Object.keys(fields));
(Object.keys(fields)).forEach((FIELD_NAME) => {
let FIELD = {"name": FIELD_NAME, "value": fields[FIELD_NAME]};
if (FIELD[`value`]) {
// Check if array.
if (Array.isArray(FIELD[`value`])) {
// Temporarily create an empty list.
field_data[FIELD[`name`]] = [];
if (typeof FIELD[`value`][0] == "object" && FIELD[`value`][0] != null && !Array.isArray(FIELD[`value`][0])) {
field_data[FIELD[`name`]].push(read(FIELD[`value`][0]));
} else {
let ELEMENTS = (document.querySelectorAll(FIELD[`value`][0]));
if (ELEMENTS.length > 0) {
(ELEMENTS).forEach((ELEMENT) => {
field_data[FIELD[`name`]].push(ELEMENT.innerText);
})
};
};
} else if ((typeof FIELD[`value`]).includes(`obj`) && FIELD[`value`] != null) {
field_data[FIELD[`name`]] = read(FIELD[`value`]);
} else if (document.querySelector(FIELD[`value`])) {
field_data[FIELD[`name`]] = document.querySelector(FIELD[`value`]).innerText;
};
};
});
return field_data;
};
field_content = read(scraper_fields);
}
if (Object.keys(field_content).length > 0) {
for (let field_name in Object.keys(field_content)) {
console.log(field_name);
this[field_name] = field_content[field_name];
}
}
}
}

65
scripts/external/watchman.js vendored Normal file
View file

@ -0,0 +1,65 @@
/* Watchman.js
Be sensitive to changes and update the state.
*/
let main = (async () => {
// Import modules.
let filters = new ((await import(chrome.runtime.getURL("scripts/filters.js"))).default);
const processor = (await import(chrome.runtime.getURL("scripts/external/processor.js"))).default;
class watchman {
/* Check the current URL.
@param {string} URL the page URL; default value is the current webpage
@return {dictionary} the filter to follow
*/
static async observe(URL = window.location.href) {
// Create the variable to determine the corresponding key.
let activity = await filters.select(URL);
return activity;
}
/* Act on the page.
@param {dictionary} filters the filter to work with
@return {boolean} the state
*/
static act(matches) {
console.log("ShopAI works here! Click on the button in the toolbar or website to start.");
// Show loading screen while the load is incomplete.
// Set the icon to indicate that it's active.
// Begin.
let PROC = new processor(matches);
window.addEventListener(`load`, (event) => {
// Remove the loading screen.
});
}
/* Set the program to standby utnil next load.
*/
static standby() {
// Set the icon to indicate that it's not active.
}
static async job() {
/* The main action. */
(watchman.observe()).then((RULES) => {
if (RULES && Object.keys(RULES).length !== 0) {
watchman.act(RULES);
} else {
watchman.standby();
}
});
}
}
watchman.job();
});
main();