work on filter selection as OOP

Initializing the filters set its object to the filters stored in memory. To select one, first run select and then you can access it via "one" key.
This commit is contained in:
buzzcode2007 2024-04-02 14:22:04 +08:00
parent d8380020a4
commit 917266dfd2

View file

@ -3,120 +3,139 @@ Manage filters.
*/
export default class filters {
constructor() {
this.all = {};
}
constructor() {
this.all = {};
}
/* Select the most appropriate filter based on a URL.
/* Select the most appropriate filter based on a URL.
@param {string} URL the current URL
*/
static select(URL = window.location.href) {
this.one = {};
}
@param {string} URL the current URL
*/
static async select(URL = window.location.href) {
this.one = await (async () => {
// Import the secretariat.
const secretariat = await import(
chrome.runtime.getURL("scripts/secretariat.js")
);
/* Update all filters or just one.
// Get the filters.
let filter = (await secretariat.read(`filters`, -1, {"field": "URL", "test value": URL}));
@param {string} URL the URL to update
@return {boolean} the state
*/
static update(URL) {
(async () => {
// Import the updater.
const secretariat = await import(
chrome.runtime.getURL("scripts/secretariat.js")
);
const net = await import(chrome.runtime.getURL("scripts/net.js"));
const texts = (await import(chrome.runtime.getURL("gui/scripts/read.js")))
.default;
const alerts = (
await import(chrome.runtime.getURL("gui/scripts/alerts.js"))
).default;
// If there are filters, then filter the URL.
return (filter);
})();
// Apparently, JS doesn't have a native queueing system, but it might best work here.
class Queue {
constructor() {
this.elements = [];
}
return (this.one);
}
enqueue(element) {
this.elements.push(element);
}
/* Update all filters or just one.
dequeue() {
return this.elements.shift();
}
@param {string} URL the URL to update
@return {boolean} the state
*/
static update(URL) {
this.all = (async () => {
// Import the updater.
const secretariat = await import(
chrome.runtime.getURL("scripts/secretariat.js")
);
const net = await import(chrome.runtime.getURL("scripts/net.js"));
const texts = (await import(chrome.runtime.getURL("gui/scripts/read.js")))
.default;
const alerts = (
await import(chrome.runtime.getURL("gui/scripts/alerts.js"))
).default;
isEmpty() {
return this.elements.length <= 0;
}
}
// Apparently, JS doesn't have a native queueing system, but it might best work here.
class Queue {
constructor() {
this.elements = [];
}
// Create a queue of the filters.
let filters = new Queue();
enqueue(element) {
this.elements.push(element);
}
if (URL) {
// Check if the URL is in a valid protocol
if (URL.includes(`://`)) {
// Append that to the queue.
filters.enqueue(URL);
}
} else {
// Add every item to the queue based on what was loaded first.
if ((await Promise.all([secretariat.read(`filters`, -1)]))[0]) {
Object.keys(
(await Promise.all([secretariat.read(`filters`, -1)]))[0],
).every((filter_URL) => {
if (filter_URL.includes(`://`)) {
filters.enqueue(filter_URL);
}
});
}
}
dequeue() {
return this.elements.shift();
}
if (!filters.isEmpty()) {
while (!filters.isEmpty()) {
let filter_URL = filters.dequeue();
isEmpty() {
return this.elements.length <= 0;
}
}
// Inform the user of download state.
alerts.log(
texts.localized(`settings_filters_update_status`, null, [
filter_URL,
]),
);
// Create a queue of the filters.
let filters = new Queue();
// Create promise of downloading.
let filter_download = net.download(filter_URL);
filter_download
.then((result) => {
// Only work when the filter is valid.
if (result) {
// Write the filter to storage.
secretariat.write(["filters", filter_URL], result, -1);
console.log(
texts.localized(
`settings_filters_update_status_complete`,
null,
[filter_URL],
),
);
}
})
.catch((error) => {
// Inform the user of the download failure.
console.log(
texts.localized(
`settings_filters_update_status_failure`,
null,
[error, filter_URL],
),
);
});
}
} else {
// Inform the user of the download being unnecessary.
alerts.warn(texts.localized(`settings_filters_update_stop`));
}
})();
}
if (URL) {
// Check if the URL is in a valid protocol
if (URL.includes(`://`)) {
// Append that to the queue.
filters.enqueue(URL);
}
} else {
// Add every item to the queue based on what was loaded first.
if ((await Promise.all([secretariat.read(`filters`, -1)]))[0]) {
Object.keys(
(await Promise.all([secretariat.read(`filters`, -1)]))[0],
).every((filter_URL) => {
if (filter_URL.includes(`://`)) {
filters.enqueue(filter_URL);
}
});
}
}
if (!filters.isEmpty()) {
while (!filters.isEmpty()) {
let filter_URL = filters.dequeue();
// Inform the user of download state.
alerts.log(
texts.localized(`settings_filters_update_status`, null, [
filter_URL,
]),
);
// Create promise of downloading.
let filter_download = net.download(filter_URL);
filter_download
.then((result) => {
// Only work when the filter is valid.
if (result) {
// Write the filter to storage.
secretariat.write(["filters", filter_URL], result, -1);
console.log(
texts.localized(
`settings_filters_update_status_complete`,
null,
[filter_URL],
),
);
}
})
.catch((error) => {
// Inform the user of the download failure.
console.log(
texts.localized(
`settings_filters_update_status_failure`,
null,
[error, filter_URL],
),
);
});
}
} else {
// Inform the user of the download being unnecessary.
alerts.warn(texts.localized(`settings_filters_update_stop`));
}
// Regardless of the download result, update will also mean setting the filters object to whatever is in storage.
return(secretariat.read(`filters`, -1).then((filters) => {
return(filters)
}));
})();
}
}