fix import method to support service worker
This commit is contained in:
		
							parent
							
								
									cd0384c3d1
								
							
						
					
					
						commit
						7eb0b86640
					
				
					 3 changed files with 77 additions and 60 deletions
				
			
		|  | @ -2,12 +2,11 @@ | ||||||
| Alert management system. | Alert management system. | ||||||
| */ | */ | ||||||
| 
 | 
 | ||||||
| const reader = (await import(chrome.runtime.getURL("gui/scripts/read.js"))).default; | import texts from "/gui/scripts/read.js"; | ||||||
| 
 |  | ||||||
| export default class alerts { | export default class alerts { | ||||||
| 	static async confirm(MESSAGE) { | 	static async confirm(MESSAGE) { | ||||||
| 		let user_response = confirm( | 		let user_response = confirm( | ||||||
| 			reader.localized((MESSAGE) ? MESSAGE : `GUI_alert_confirm_action_text`), | 			texts.localized((MESSAGE) ? MESSAGE : `GUI_alert_confirm_action_text`), | ||||||
| 		); | 		); | ||||||
| 
 | 
 | ||||||
| 		// Return the user response.
 | 		// Return the user response.
 | ||||||
|  | @ -46,13 +45,13 @@ export default class alerts { | ||||||
| 	*/ | 	*/ | ||||||
| 	static warn(message, critical = false) { | 	static warn(message, critical = false) { | ||||||
| 		console.warn(message); | 		console.warn(message); | ||||||
| 	if (critical) { | 		if (critical) { | ||||||
| 		alert(message); | 			alert(message); | ||||||
| 	} else { | 		} else { | ||||||
| 		try { | 			try { | ||||||
| 			M.toast({ text: message }); | 				M.toast({ text: message }); | ||||||
| 		} catch (err) {} | 			} catch (err) {} | ||||||
| 	} | 		} | ||||||
| 	} | 	} | ||||||
| 	 | 	 | ||||||
| 	/* | 	/* | ||||||
|  | @ -62,20 +61,18 @@ export default class alerts { | ||||||
| 	@param {number} ERROR_MESSAGE the custom error message | 	@param {number} ERROR_MESSAGE the custom error message | ||||||
| 	@param {boolean} critical the critical nature | 	@param {boolean} critical the critical nature | ||||||
| 	*/ | 	*/ | ||||||
| 	static error(ERROR_CODE, ERROR_MESSAGE, ERROR_STACK, critical = true) { | 	static async error(ERROR_CODE, ERROR_MESSAGE, ERROR_STACK, critical = true) { | ||||||
| 		(async () => { | 		// Import the templating.
 | ||||||
| 			// Import the templating.
 | 		const texts = (await import(chrome.runtime.getURL("gui/scripts/read.js"))).default; | ||||||
| 			const texts = (await import(chrome.runtime.getURL("gui/scripts/read.js"))).default; |  | ||||||
| 
 | 
 | ||||||
| 			// Display the error message.
 | 		// Display the error message.
 | ||||||
| 			console.error(texts.localized(`error_msg`, false, [ERROR_CODE, ERROR_MESSAGE, ERROR_STACK])); | 		console.error(texts.localized(`error_msg`, false, [ERROR_CODE, ERROR_MESSAGE, ERROR_STACK])); | ||||||
| 			if (critical) { | 		if (critical) { | ||||||
| 				alert(texts.localized(`error_msg_GUI`, false, [String(ERROR_CODE), ERROR_MESSAGE])); | 			alert(texts.localized(`error_msg_GUI`, false, [String(ERROR_CODE), ERROR_MESSAGE])); | ||||||
| 			} else { | 		} else { | ||||||
| 				try { | 			try { | ||||||
| 					M.toast({ text: ERROR_MESSAGE }); | 				M.toast({ text: ERROR_MESSAGE }); | ||||||
| 				} catch (err) {}; | 			} catch (err) {}; | ||||||
| 			}; | 		}; | ||||||
| 		})(); |  | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -3,11 +3,11 @@ This does not stand for "FamiCom" but instead on Finalization and Completion. Th | ||||||
| */ | */ | ||||||
| 
 | 
 | ||||||
| import { init, read, write } from "./secretariat.js"; | import { init, read, write } from "./secretariat.js"; | ||||||
| 
 | import filters from "./filters.js"; | ||||||
| let config = chrome.runtime.getURL("config/config.json"); | let config = chrome.runtime.getURL("config/config.json"); | ||||||
| 
 | 
 | ||||||
| export default class fc { | export default class fc { | ||||||
|   /* Start the out of the box experience. */ |   // Start the out of the box experience.
 | ||||||
|   static hello() { |   static hello() { | ||||||
|     // the OOBE must be in the config.
 |     // the OOBE must be in the config.
 | ||||||
|     fetch(config) |     fetch(config) | ||||||
|  | @ -26,16 +26,18 @@ export default class fc { | ||||||
|       }); |       }); | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   /* Initialize the configuration. */ |   // Initialize the configuration. 
 | ||||||
|   static setup() { |   static setup() { | ||||||
|     // the OOBE must be in the config.
 |     // the OOBE must be in the config.
 | ||||||
|     fetch(config) |     fetch(config) | ||||||
|       .then((response) => response.json()) |       .then((response) => response.json()) | ||||||
|       .then((jsonData) => { |       .then(async (jsonData) => { | ||||||
|  |         // const secretariat = await import(chrome.runtime.getURL("scripts/secretariat.js"));
 | ||||||
|  | 
 | ||||||
|         let configuration = jsonData; |         let configuration = jsonData; | ||||||
| 
 | 
 | ||||||
|         // Run the storage initialization.
 |         // Run the storage initialization.
 | ||||||
|         init(configuration); |         // secretariat.init(configuration);
 | ||||||
|       }) |       }) | ||||||
|       .catch((error) => { |       .catch((error) => { | ||||||
|         console.error(error); |         console.error(error); | ||||||
|  | @ -51,25 +53,42 @@ export default class fc { | ||||||
|     }); |     }); | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   /* main function */ |   // main function 
 | ||||||
|   static run() { |   static run() { | ||||||
|     fc.trigger(); |     fc.trigger(); | ||||||
|     fc.every(); |     fc.every(); | ||||||
|   } |   } | ||||||
| 
 | 
 | ||||||
|   static async every() { |   static async every() { | ||||||
|     let DURATION_PREFERENCES = await read([`settings`,`sync`]); |     read([`settings`,`sync`]).then(async (DURATION_PREFERENCES) => { | ||||||
|  |       // Forcibly create the preference if it doesn't exist. It's required! 
 | ||||||
|  |       if (!(typeof DURATION_PREFERENCES).includes(`obj`) || DURATION_PREFERENCES == null || Array.isArray(DURATION_PREFERENCES)) { | ||||||
|  |         DURATION_PREFERENCES = {}; | ||||||
|  |         DURATION_PREFERENCES[`duration`] = 24; | ||||||
|    |    | ||||||
|     if (((typeof DURATION_PREFERENCES).includes(`obj`) && DURATION_PREFERENCES != null && !Array.isArray(DURATION_PREFERENCES)) ? ((DURATION_PREFERENCES[`duration`]) ? (DURATION_PREFERENCES[`duration`] > 0) : false) : false) { |         // Write it. 
 | ||||||
|       // Convert DURATION_PREFERENCES[`duration`]) from hrs to milliseconds.
 |         write([`settings`, `sync`], DURATION_PREFERENCES, -1); | ||||||
|       DURATION_PREFERENCES[`duration`] = DURATION_PREFERENCES[`duration`] * 60 * 60 * 1000; |       }; | ||||||
|       let filters = new (await import(chrome.runtime.getURL(`scripts/filters.js`))).default(); |    | ||||||
|  |       if (((typeof DURATION_PREFERENCES).includes(`obj`) && DURATION_PREFERENCES != null && !Array.isArray(DURATION_PREFERENCES)) ? ((DURATION_PREFERENCES[`duration`]) ? (DURATION_PREFERENCES[`duration`] > 0) : false) : false) { | ||||||
|  |         // Convert DURATION_PREFERENCES[`duration`]) from hrs to milliseconds.
 | ||||||
|  |         DURATION_PREFERENCES[`duration`] = DURATION_PREFERENCES[`duration`] * 60 * 60 * 1000; | ||||||
|  |         let FILTERS = new filters; | ||||||
|  |    | ||||||
|  |         // Now, set the interval. 
 | ||||||
|  |         let updater_set = () => { | ||||||
|  |           setInterval(async () => { | ||||||
|  |             // Update the filters. 
 | ||||||
|  |             filters.update(); | ||||||
|  |           }, DURATION_PREFERENCES[`duration`]); | ||||||
|  |         }; | ||||||
|  |    | ||||||
|  |         // Provide a way to cancel the interval. 
 | ||||||
|  |         let updater_cancel = () => { | ||||||
|  |    | ||||||
|  |         } | ||||||
|  |       }; | ||||||
|  |     }) | ||||||
| 
 | 
 | ||||||
|       // Now, set the interval. 
 |  | ||||||
|       setInterval(async () => { |  | ||||||
|         // Update the filters. 
 |  | ||||||
|         filters.update(); |  | ||||||
|       }, DURATION_PREFERENCES[`duration`]); |  | ||||||
|     }; |  | ||||||
|   }; |   }; | ||||||
| } | } | ||||||
|  |  | ||||||
|  | @ -2,16 +2,17 @@ | ||||||
| Manage filters. | Manage filters. | ||||||
| */ | */ | ||||||
| 
 | 
 | ||||||
| const secretariat = await import(chrome.runtime.getURL("scripts/secretariat.js")); | import {read, write} from "./secretariat.js"; | ||||||
| const net = await import(chrome.runtime.getURL("scripts/net.js")); | import {download} from "./net.js"; | ||||||
| const texts = (await import(chrome.runtime.getURL("gui/scripts/read.js"))).default; | import texts from "/gui/scripts/read.js"; | ||||||
| const alerts = (await import(chrome.runtime.getURL("gui/scripts/alerts.js"))).default; | import {Queue} from "./common.js"; | ||||||
| const common = (await import(chrome.runtime.getURL("scripts/common.js"))); | import alerts from "/gui/scripts/alerts.js" | ||||||
|  | // const alerts = (await import(chrome.runtime.getURL("gui/scripts/alerts.js"))).default;
 | ||||||
| 
 | 
 | ||||||
| export default class filters { | export default class filters { | ||||||
| 	constructor() { | 	constructor() { | ||||||
| 		this.all = async () => { | 		this.all = async () => { | ||||||
| 			return secretariat.read(`filters`, -1).then((filters) => { | 			return read(`filters`, -1).then((filters) => { | ||||||
| 				return filters; | 				return filters; | ||||||
| 			}); | 			}); | ||||||
| 		}; | 		}; | ||||||
|  | @ -24,13 +25,13 @@ export default class filters { | ||||||
| 	async select(URL = window.location.href) { | 	async select(URL = window.location.href) { | ||||||
| 		this.one = await (async () => { | 		this.one = await (async () => { | ||||||
| 			// Get the filters.
 | 			// Get the filters.
 | ||||||
| 			let filter = await secretariat.search(`filters`, -1, [`URL`]); | 			let filter = await search(`filters`, -1, [`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. | ||||||
|  | @ -40,7 +41,7 @@ export default class filters { | ||||||
| 	*/ | 	*/ | ||||||
| 	async update(URL) { | 	async update(URL) { | ||||||
| 		// Create a queue of the filters.
 | 		// Create a queue of the filters.
 | ||||||
| 		let filters = new common.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
 | ||||||
|  | @ -50,9 +51,9 @@ export default class filters { | ||||||
| 			} | 			} | ||||||
| 		} 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 secretariat.read(`filters`, -1)) { | 			if (await read(`filters`, -1)) { | ||||||
| 				for (let FILTER_URL_INDEX = 0; FILTER_URL_INDEX < Object.keys(await secretariat.read(`filters`, -1)).length; FILTER_URL_INDEX++) { | 				for (let FILTER_URL_INDEX = 0; FILTER_URL_INDEX < Object.keys(await read(`filters`, -1)).length; FILTER_URL_INDEX++) { | ||||||
| 					let FILTER_URL = Object.keys(await secretariat.read([`settings`, `filters`], 1))[FILTER_URL_INDEX]; | 					let FILTER_URL = Object.keys(await read([`settings`, `filters`], 1))[FILTER_URL_INDEX]; | ||||||
| 					if (FILTER_URL.includes(`://`)) { | 					if (FILTER_URL.includes(`://`)) { | ||||||
| 						filters.enqueue(FILTER_URL); | 						filters.enqueue(FILTER_URL); | ||||||
| 					} | 					} | ||||||
|  | @ -68,18 +69,18 @@ export default class filters { | ||||||
| 				new alerts (texts.localized(`settings_filters_update_status`, null, [filter_URL])); | 				new alerts (texts.localized(`settings_filters_update_status`, null, [filter_URL])); | ||||||
| 
 | 
 | ||||||
| 				// Create promise of downloading.
 | 				// Create promise of downloading.
 | ||||||
| 				let filter_download = net.download(filter_URL, `JSON`, false, true); | 				let filter_download = download(filter_URL, `JSON`, false, true); | ||||||
| 				filter_download | 				filter_download | ||||||
| 					.then(async function (result) { | 					.then(async function (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); | 							write(["filters", filter_URL], result, -1); | ||||||
| 							alerts.log(texts.localized(`settings_filters_update_status_complete`,null,[filter_URL])); | 							alerts.log(texts.localized(`settings_filters_update_status_complete`,null,[filter_URL])); | ||||||
|                              |                              | ||||||
|                             // Add the filter to the sync list.
 |                             // Add the filter to the sync list.
 | ||||||
|                             if ((await secretariat.read(["settings", `filters`, filter_URL], 1)) == null) { |                             if ((await read(["settings", `filters`, filter_URL], 1)) == null) { | ||||||
|                                 secretariat.write(["settings", `filters`, filter_URL], true, 1); |                                 write(["settings", `filters`, filter_URL], true, 1); | ||||||
|                             } |                             } | ||||||
| 						} | 						} | ||||||
| 					}) | 					}) | ||||||
|  | @ -94,7 +95,7 @@ export default class filters { | ||||||
| 		} | 		} | ||||||
| 
 | 
 | ||||||
| 		// 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 read(`filters`, -1); | ||||||
| 
 | 
 | ||||||
| 		return this.all; | 		return this.all; | ||||||
| 	} | 	} | ||||||
|  | @ -105,7 +106,7 @@ export default class filters { | ||||||
| 	*/ | 	*/ | ||||||
| 	async remove(URL) { | 	async remove(URL) { | ||||||
| 		if (URL.includes(`://`)) { | 		if (URL.includes(`://`)) { | ||||||
| 			return (await secretariat.forget([`filters`, URL], -1, false)) ? await secretariat.forget([`settings`, `filters`, URL], 1, true) : false; | 			return (await forget([`filters`, URL], -1, false)) ? await forget([`settings`, `filters`, URL], 1, true) : false; | ||||||
| 		} else { | 		} else { | ||||||
| 			// Inform the user of the removal being unnecessary.
 | 			// Inform the user of the removal being unnecessary.
 | ||||||
| 			alerts.warn(texts.localized(`settings_filters_removal_stop`)); | 			alerts.warn(texts.localized(`settings_filters_removal_stop`)); | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue