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