parse download as JSON
Since filters are supposed to be in JSON
This commit is contained in:
		
							parent
							
								
									912a36a2f6
								
							
						
					
					
						commit
						fe41352afd
					
				
					 1 changed files with 118 additions and 119 deletions
				
			
		|  | @ -3,146 +3,145 @@ Manage filters. | ||||||
| */ | */ | ||||||
| 
 | 
 | ||||||
| export default class filters { | export default class filters { | ||||||
| 	constructor() { |   constructor() { | ||||||
| 		this.all = (async() => { |     this.all = async () => { | ||||||
| 			// 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") | ||||||
| 			); |       ); | ||||||
| 			 |  | ||||||
| 			return(secretariat.read(`filters`, -1).then((filters) => { |  | ||||||
| 				return(filters) |  | ||||||
| 			})); |  | ||||||
| 		}) |  | ||||||
| 	} |  | ||||||
| 
 | 
 | ||||||
| 	/* Select the most appropriate filter based on a URL. |       return secretariat.read(`filters`, -1).then((filters) => { | ||||||
|  |         return filters; | ||||||
|  |       }); | ||||||
|  |     }; | ||||||
|  |   } | ||||||
|  | 
 | ||||||
|  |   /* Select the most appropriate filter based on a URL. | ||||||
| 
 | 
 | ||||||
| 	@param {string} URL the current URL | 	@param {string} URL the current URL | ||||||
| 	*/ | 	*/ | ||||||
| 	async select(URL = window.location.href) { |   async select(URL = window.location.href) { | ||||||
| 		this.one = await (async () => { |     this.one = await (async () => { | ||||||
| 			// Import the secretariat.
 |       // Import the secretariat.
 | ||||||
| 			const secretariat = await import( |       const secretariat = await import( | ||||||
| 				chrome.runtime.getURL("scripts/secretariat.js") |         chrome.runtime.getURL("scripts/secretariat.js") | ||||||
| 			); |       ); | ||||||
| 
 | 
 | ||||||
| 			// Get the filters.
 |       // Get the filters.
 | ||||||
| 			let filter = (await secretariat.read(`filters`, -1, {"field": "URL", "test value": URL})); |       let filter = await secretariat.read(`filters`, -1, { | ||||||
|  |         field: "URL", | ||||||
|  |         "test value": URL, | ||||||
|  |       }); | ||||||
| 
 | 
 | ||||||
| 			// If there are filters, then filter the URL.
 |       // If there are filters, then filter the URL.
 | ||||||
| 			return (filter); |       return filter; | ||||||
| 		})(); |     })(); | ||||||
| 
 | 
 | ||||||
| 		return (this.one); |     return this.one; | ||||||
| 	} |   } | ||||||
| 
 | 
 | ||||||
| 	/* Update all filters or just one. |   /* Update all filters or just one. | ||||||
| 
 | 
 | ||||||
| 	@param {string} URL the URL to update | 	@param {string} URL the URL to update | ||||||
| 	@return {boolean} the state | 	@return {boolean} the state | ||||||
| 	*/ | 	*/ | ||||||
| 	async update(URL) { |   async update(URL) { | ||||||
| 		// 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"))) |     const texts = (await import(chrome.runtime.getURL("gui/scripts/read.js"))) | ||||||
| 			.default; |       .default; | ||||||
| 		const alerts = ( |     const alerts = ( | ||||||
| 			await import(chrome.runtime.getURL("gui/scripts/alerts.js")) |       await import(chrome.runtime.getURL("gui/scripts/alerts.js")) | ||||||
| 		).default; |     ).default; | ||||||
| 
 | 
 | ||||||
| 		// 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 { | ||||||
| 			constructor() { |       constructor() { | ||||||
| 				this.elements = []; |         this.elements = []; | ||||||
| 			} |       } | ||||||
| 
 | 
 | ||||||
| 			enqueue(element) { |       enqueue(element) { | ||||||
| 				this.elements.push(element); |         this.elements.push(element); | ||||||
| 			} |       } | ||||||
| 
 | 
 | ||||||
| 			dequeue() { |       dequeue() { | ||||||
| 				return this.elements.shift(); |         return this.elements.shift(); | ||||||
| 			} |       } | ||||||
| 
 | 
 | ||||||
| 			isEmpty() { |       isEmpty() { | ||||||
| 				return this.elements.length <= 0; |         return this.elements.length <= 0; | ||||||
| 			} |       } | ||||||
| 		} |     } | ||||||
| 
 | 
 | ||||||
| 		// Create a queue of the filters.
 |     // Create a queue of the filters.
 | ||||||
| 		let filters = new Queue(); |     let filters = new Queue(); | ||||||
| 
 | 
 | ||||||
| 		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(`://`)) { | ||||||
| 				// Append that to the queue.
 |         // Append that to the queue.
 | ||||||
| 				filters.enqueue(URL); |         filters.enqueue(URL); | ||||||
| 			} |       } | ||||||
| 		} else { |     } else { | ||||||
| 			// Add every item to the queue based on what was loaded first.
 |       // Add every item to the queue based on what was loaded first.
 | ||||||
| 			if ((await Promise.all([secretariat.read(`filters`, -1)]))[0]) { |       if ((await Promise.all([secretariat.read(`filters`, -1)]))[0]) { | ||||||
| 				Object.keys( |         Object.keys( | ||||||
| 					(await Promise.all([secretariat.read(`filters`, -1)]))[0], |           (await Promise.all([secretariat.read(`filters`, -1)]))[0], | ||||||
| 				).every((filter_URL) => { |         ).every((filter_URL) => { | ||||||
| 					if (filter_URL.includes(`://`)) { |           if (filter_URL.includes(`://`)) { | ||||||
| 						filters.enqueue(filter_URL); |             filters.enqueue(filter_URL); | ||||||
| 					} |           } | ||||||
| 				}); |         }); | ||||||
| 			} |       } | ||||||
| 		} |     } | ||||||
| 
 | 
 | ||||||
| 		if (!filters.isEmpty()) { |     if (!filters.isEmpty()) { | ||||||
| 			while (!filters.isEmpty()) { |       while (!filters.isEmpty()) { | ||||||
| 				let filter_URL = filters.dequeue(); |         let filter_URL = filters.dequeue(); | ||||||
| 
 | 
 | ||||||
| 				// Inform the user of download state.
 |         // Inform the user of download state.
 | ||||||
| 				alerts.log( |         alerts.log( | ||||||
| 					texts.localized(`settings_filters_update_status`, null, [ |           texts.localized(`settings_filters_update_status`, null, [filter_URL]), | ||||||
| 						filter_URL, |         ); | ||||||
| 					]), |  | ||||||
| 				); |  | ||||||
| 
 | 
 | ||||||
| 				// Create promise of downloading.
 |         // Create promise of downloading.
 | ||||||
| 				let filter_download = net.download(filter_URL); |         let filter_download = net.download(filter_URL, `JSON`); | ||||||
| 				filter_download |         filter_download | ||||||
| 					.then((result) => { |           .then((result) => { | ||||||
| 						// Only work when the filter is valid.
 |             // Only work when the filter is valid.
 | ||||||
| 						if (result) { |             if (result) { | ||||||
| 							// Write the filter to storage.
 |               // Write the filter to storage.
 | ||||||
| 							secretariat.write(["filters", filter_URL], result, -1); |               secretariat.write(["filters", filter_URL], result, -1); | ||||||
| 							alerts.log( |               alerts.log( | ||||||
| 								texts.localized( |                 texts.localized( | ||||||
| 									`settings_filters_update_status_complete`, |                   `settings_filters_update_status_complete`, | ||||||
| 									null, |                   null, | ||||||
| 									[filter_URL], |                   [filter_URL], | ||||||
| 								), |                 ), | ||||||
| 							); |               ); | ||||||
| 						} |             } | ||||||
| 						 |           }) | ||||||
| 					}) |           .catch((error) => { | ||||||
| 					.catch((error) => { |             // Inform the user of the download failure.
 | ||||||
| 						// Inform the user of the download failure.
 |             alerts.error( | ||||||
| 						alerts.error( |               texts.localized(`settings_filters_update_status_failure`, null, [ | ||||||
| 							texts.localized( |                 error, | ||||||
| 								`settings_filters_update_status_failure`, |                 filter_URL, | ||||||
| 								null, |               ]), | ||||||
| 								[error, filter_URL], |             ); | ||||||
| 							), |           }); | ||||||
| 						); |       } | ||||||
| 					}); |     } else { | ||||||
| 			} |       // Inform the user of the download being unnecessary.
 | ||||||
| 		} else { |       alerts.warn(texts.localized(`settings_filters_update_stop`)); | ||||||
| 			// 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. 
 |     // Regardless of the download result, update will also mean setting the filters object to whatever is in storage.
 | ||||||
| 		this.all = (await secretariat.read(`filters`, -1));	 |     this.all = await secretariat.read(`filters`, -1); | ||||||
| 
 | 
 | ||||||
| 		return(this.all); |     return this.all; | ||||||
| 	} |   } | ||||||
| } | } | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue