update filter download
It also lets users know if download is successful or not.
This commit is contained in:
parent
93ae3df82f
commit
84947339ca
1 changed files with 38 additions and 13 deletions
|
@ -14,12 +14,12 @@ export async function select(URL = window.location.href) {}
|
||||||
@return {boolean} the state
|
@return {boolean} the state
|
||||||
*/
|
*/
|
||||||
export async function update(URL) {
|
export async function update(URL) {
|
||||||
console.log("Updating…");
|
|
||||||
// Import the updater.
|
// Import the updater.
|
||||||
const secretariat = await import(
|
const secretariat = await import(
|
||||||
chrome.runtime.getURL("scripts/secretariat.js")
|
chrome.runtime.getURL("scripts/secretariat.js")
|
||||||
);
|
);
|
||||||
const net = await import(chrome.runtime.getURL("scripts/net.js"));
|
const net = await import(chrome.runtime.getURL("scripts/net.js"));
|
||||||
|
const texts = await import(chrome.runtime.getURL("gui/scripts/read.js"));
|
||||||
|
|
||||||
// Apparently, JS doesn't have a native queueing system, but it might best work here.
|
// Apparently, JS doesn't have a native queueing system, but it might best work here.
|
||||||
class Queue {
|
class Queue {
|
||||||
|
@ -43,13 +43,6 @@ export async function update(URL) {
|
||||||
// Create a queue of the filters.
|
// Create a queue of the filters.
|
||||||
let filters = new Queue();
|
let filters = new Queue();
|
||||||
|
|
||||||
async function download(URL) {
|
|
||||||
let filter = await fetch(URL);
|
|
||||||
|
|
||||||
// Return the filter.
|
|
||||||
return filter;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (URL) {
|
if (URL) {
|
||||||
// Check if the URL is in a valid protocol
|
// Check if the URL is in a valid protocol
|
||||||
if (URL.includes(`://`)) {
|
if (URL.includes(`://`)) {
|
||||||
|
@ -69,11 +62,43 @@ export async function update(URL) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!filters.isEmpty()) {
|
||||||
while (!filters.isEmpty()) {
|
while (!filters.isEmpty()) {
|
||||||
let filter_URL = filters.dequeue();
|
let filter_URL = filters.dequeue();
|
||||||
let filter = await net.download(filter_URL);
|
|
||||||
|
|
||||||
|
// Inform the user of download state.
|
||||||
|
console.log(filter_URL);
|
||||||
|
console.log(
|
||||||
|
texts.read(`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.
|
// Write the filter to storage.
|
||||||
secretariat.write(["filters", filter_URL], filter, -1);
|
secretariat.write(["filters", filter_URL], result, -1);
|
||||||
|
console.log(
|
||||||
|
texts.read(`settings_filters_update_status_complete`, null, [
|
||||||
|
filter_URL,
|
||||||
|
]),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch((error) => {
|
||||||
|
// Inform the user of the download failure.
|
||||||
|
console.log(
|
||||||
|
texts.read(`settings_filters_update_status_failure`, null, [
|
||||||
|
error,
|
||||||
|
filter_URL,
|
||||||
|
]),
|
||||||
|
);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// Inform the user of the download being unnecessary.
|
||||||
|
console.log(texts.read(`settings_filters_update_stop`));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue