move updater to check
This commit is contained in:
		
							parent
							
								
									750c46b59b
								
							
						
					
					
						commit
						fcee12d161
					
				
					 4 changed files with 185 additions and 120 deletions
				
			
		|  | @ -1,7 +0,0 @@ | |||
| import EntryManager from "/scripts/GUI/entrypoints/manager.js" | ||||
| 
 | ||||
| export default class BackgroundCheck { | ||||
| 	static init() { | ||||
| 		new EntryManager(); | ||||
| 	}; | ||||
| }; | ||||
							
								
								
									
										117
									
								
								scripts/background/check.js
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										117
									
								
								scripts/background/check.js
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,117 @@ | |||
| /* | ||||
| BackgroundCheck | ||||
| Check the tabs in the background, and check the filters. | ||||
| */ | ||||
| 
 | ||||
| // Filter importation
 | ||||
| import EntryManager from "/scripts/GUI/entrypoints/manager.js" | ||||
| import filters from "../filters.js"; | ||||
| import {background, global} from "/scripts/secretariat.js"; | ||||
| 
 | ||||
| export default class BackgroundCheck { | ||||
| 	update = {}; | ||||
| 
 | ||||
| 	constructor () { | ||||
| 		this.manager = new EntryManager(); | ||||
| 		this.updater(); | ||||
| 	}; | ||||
| 
 | ||||
| 	updater() { | ||||
| 		global.read([`settings`,`sync`]).then(async (DURATION_PREFERENCES) => { | ||||
| 			/* | ||||
| 			Set the default options if they don't exist yet.  | ||||
| 			*/ | ||||
| 			const setDefaults = async () => { | ||||
| 				// Forcibly create the preference if it doesn't exist. It's required! 
 | ||||
| 				if (!(typeof DURATION_PREFERENCES).includes(`obj`) || !DURATION_PREFERENCES || Array.isArray(DURATION_PREFERENCES)) { | ||||
| 					DURATION_PREFERENCES = {}; | ||||
| 					DURATION_PREFERENCES[`duration`] = 24; | ||||
| 		 | ||||
| 					// Write it. 
 | ||||
| 					return(await global.write([`settings`, `sync`], DURATION_PREFERENCES, -1, {"silent": true})); | ||||
| 				} else {return (true)}; | ||||
| 			}; | ||||
| 
 | ||||
| 			setDefaults().then((result) => { | ||||
| 				if (result) { | ||||
| 					/* | ||||
| 					Check if it's time to update the filters through comparing the difference of the current time and the last updated time to the expected duration.  | ||||
| 					*/ | ||||
| 					async function updater_check() { | ||||
| 						let TIME = {}; | ||||
| 						TIME[`now`] = Date.now(); | ||||
| 						TIME[`last`] = await global.read([`settings`,`sync`,`last`], -1); | ||||
| 
 | ||||
| 						// Run if the last time is not set or if the difference is greater than the expected duration.
 | ||||
| 						return (TIME[`last`] ? ((TIME[`now`] - TIME[`last`]) > DURATION_PREFERENCES[`duration`]) : true); | ||||
| 					}; | ||||
| 
 | ||||
| 					/* | ||||
| 					Run the update.  | ||||
| 
 | ||||
| 					@return {Promise} the promise that, once resolved, contains the last update status.  | ||||
| 					*/ | ||||
| 					const updater_run = async () => { | ||||
| 						filter.update(); | ||||
| 
 | ||||
| 						// Update the last time.
 | ||||
| 						return(await global.write([`settings`,`sync`,`last`], Date.now(), -1)); | ||||
| 					}; | ||||
| 
 | ||||
| 					// Set the interval. 
 | ||||
| 					let updater_set = () => { | ||||
| 						this.update[`checker`] = setInterval(async () => { | ||||
| 							// Update the filters. 
 | ||||
| 							updater_run(); | ||||
| 						}, DURATION_PREFERENCES[`duration`]); | ||||
| 					}; | ||||
| 					 | ||||
| 					/* | ||||
| 					Reset the interval.  | ||||
| 					*/ | ||||
| 					const updater_reset = () => { | ||||
| 						// Cancel the interval. 
 | ||||
| 						(this.update[`checker`]) ? clearInterval(this.update[`checker`]) : false; | ||||
| 
 | ||||
| 						// Run the updater, if necessary. 
 | ||||
| 						(updater_check()) | ||||
| 							? updater_run() | ||||
| 							: false; | ||||
| 						 | ||||
| 						// Start the new interval. 
 | ||||
| 						updater_set(); | ||||
| 					} | ||||
| 					 | ||||
| 					const updater_background = () => { | ||||
| 						this.update[`background`] = async () => { | ||||
| 							if ((await global.read([`settings`, `sync`, `duration`])) ? (await global.read([`settings`, `sync`, `duration`] * (60 ** 2) * 1000 != DURATION_PREFERENCES[`duration`])) : false) { | ||||
| 								// Get the new time. 
 | ||||
| 								DURATION_PREFERENCES[`duration`] = await global.global.read([`settings`, `sync`, `duration`]) * (60 ** 2) * 1000; | ||||
| 
 | ||||
| 								// Reset the updater.
 | ||||
| 								updater_reset();								 | ||||
| 							}; | ||||
| 						}; | ||||
| 
 | ||||
| 						// Set the background checker. 
 | ||||
| 						new background(() => {return(this.update.background())}); | ||||
| 					} | ||||
| 
 | ||||
| 					// Convert DURATION_PREFERENCES[`duration`]) from hrs to milliseconds.
 | ||||
| 					DURATION_PREFERENCES[`duration`] = DURATION_PREFERENCES[`duration`] * (60 ** 2) * 1000; | ||||
| 
 | ||||
| 					// Set the filter management. 
 | ||||
| 					let filter = new filters(); | ||||
| 					 | ||||
| 					// When the browser is started, run the updater immediately only when the filters are not updated yet.
 | ||||
| 					(updater_check()) | ||||
| 						? updater_run() | ||||
| 						: false; | ||||
| 
 | ||||
| 					// Run the background. 
 | ||||
| 					updater_background(); | ||||
| 				} | ||||
| 			}); | ||||
| 		}) | ||||
| 	} | ||||
| }; | ||||
|  | @ -1,113 +0,0 @@ | |||
| /* fc.js | ||||
| This script provides installation run scripts. | ||||
| */ | ||||
| 
 | ||||
| import { template, global, background} from "../secretariat.js"; | ||||
| import filters from "../filters.js"; | ||||
| import pointer from "../data/pointer.js"; | ||||
| let config = chrome.runtime.getURL("config/config.json"); | ||||
| 
 | ||||
| export default class fc { | ||||
| 	// Start the out of the box experience.
 | ||||
| 	static hello() { | ||||
| 		// the OOBE must be in the config.
 | ||||
| 		fetch(config) | ||||
| 			.then((response) => response.json()) | ||||
| 			.then((jsonData) => { | ||||
| 				let configuration = jsonData[`OOBE`]; | ||||
| 
 | ||||
| 				if (configuration) { | ||||
| 					configuration.forEach((item) => { | ||||
| 						chrome.tabs.create({ url: item }, function (tab) {}); | ||||
| 					}); | ||||
| 				} | ||||
| 			}) | ||||
| 			.catch((error) => { | ||||
| 				console.error(error); | ||||
| 			}); | ||||
| 	} | ||||
| 
 | ||||
| 	// Initialize the configuration. 
 | ||||
| 	static setup() { | ||||
| 		// the OOBE must be in the config.
 | ||||
| 		fetch(config) | ||||
| 			.then((response) => response.json()) | ||||
| 			.then(async (jsonData) => { | ||||
| 				let configuration = jsonData; | ||||
| 				 | ||||
| 				// Run the storage initialization.
 | ||||
| 				delete configuration[`OOBE`]; | ||||
| 				template.set(configuration); | ||||
| 
 | ||||
| 				// Update the filters to sync with synchronized storage data. 
 | ||||
| 				(new filters).update(); | ||||
| 			}) | ||||
| 			.catch((error) => { | ||||
| 				console.error(error); | ||||
| 			}); | ||||
| 	} | ||||
| 
 | ||||
| 	static trigger() { | ||||
| 		chrome.runtime.onInstalled.addListener(function (details) { | ||||
| 			(details.reason == chrome.runtime.OnInstalledReason.INSTALL) ? fc.hello() : null; | ||||
| 			fc.setup(); | ||||
| 		}); | ||||
| 	} | ||||
| 
 | ||||
| 	// main function 
 | ||||
| 	static run() { | ||||
| 		fc.trigger(); | ||||
| 		fc.every(); | ||||
| 
 | ||||
| 		// Might as well set the preferences for storage. 
 | ||||
| 		template.configure(); | ||||
| 		pointer.clear(); | ||||
| 	} | ||||
| 
 | ||||
| 	static async every() { | ||||
| 		global.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; | ||||
| 	 | ||||
| 				// Write it. 
 | ||||
| 				await global.write([`settings`, `sync`], DURATION_PREFERENCES, -1, {"silent": true}); | ||||
| 			}; | ||||
| 	 | ||||
| 			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 ** 2) * 1000; | ||||
| 				let filter = new filters; | ||||
| 	 | ||||
| 				// Now, set the interval. 
 | ||||
| 				let updater_set = () => { | ||||
| 					setInterval(async () => { | ||||
| 						// Update the filters. 
 | ||||
| 						filter.update(); | ||||
| 					}, DURATION_PREFERENCES[`duration`]); | ||||
| 				}; | ||||
| 	 | ||||
| 				// Provide a way to cancel the interval. 
 | ||||
| 				let updater_cancel = (updater) => { | ||||
| 					clearInterval(updater); | ||||
| 				}; | ||||
| 
 | ||||
| 				let UPDATER = updater_set(); | ||||
| 
 | ||||
| 				let updater_interval = async () => { | ||||
| 					if ((await global.read([`settings`, `sync`, `duration`])) ? (await global.read([`settings`, `sync`, `duration`] * (60 ** 2) * 1000 != DURATION_PREFERENCES[`duration`])) : false) { | ||||
| 						DURATION_PREFERENCES[`duration`] = await global.global.read([`settings`, `sync`, `duration`]) * (60 ** 2) * 1000; | ||||
| 
 | ||||
| 						// Reset the updater. 
 | ||||
| 						updater_cancel(UPDATER); | ||||
| 						UPDATER = updater_set(); | ||||
| 					} | ||||
| 				}; | ||||
| 
 | ||||
| 				new background(updater_cancel); | ||||
| 			}; | ||||
| 		}) | ||||
| 
 | ||||
| 	}; | ||||
| } | ||||
							
								
								
									
										68
									
								
								scripts/background/importer.js
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										68
									
								
								scripts/background/importer.js
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,68 @@ | |||
| /*  | ||||
| BackgroundImporter | ||||
| This script provides installation run scripts. | ||||
| */ | ||||
| 
 | ||||
| // File importation
 | ||||
| import {template, global} from "../secretariat.js"; | ||||
| import pointer from "../data/pointer.js"; | ||||
| 
 | ||||
| // The URL for the configuration file
 | ||||
| const config = chrome.runtime.getURL("config/config.json"); | ||||
| 
 | ||||
| export default class BackgroundImporter { | ||||
| 	// Start the out of the box experience.
 | ||||
| 	async hello() { | ||||
| 		if (!(await global.read([`init`])) || !(await global.read([`settings`,`analysis`,`api`,`key`]))) { | ||||
| 			let SOURCE = fetch(config);  | ||||
| 			let SITES = [`popup/hello.htm`]; | ||||
| 	 | ||||
| 			if (SOURCE.ok) { | ||||
| 				try { | ||||
| 					let CONFIGURATION = await SOURCE.json(); | ||||
| 					if (CONFIGURATION[`OOBE`]) { | ||||
| 						SITES = [...SITES, ...(Array.isArray(CONFIGURATION[`OOBE`]) ? CONFIGURATION[`OOBE`] : [CONFIGURATION[`OOBE`]])]; | ||||
| 					}; | ||||
| 				} catch(err) {} | ||||
| 			}; | ||||
| 			 | ||||
| 			SITES.forEach((item) => { | ||||
| 				// Get local URL. 
 | ||||
| 				chrome.tabs.create({ url: chrome.runtime.getURL('pages/'.concat(item)) }, function (tab) {}); | ||||
| 			}); | ||||
| 		}; | ||||
| 	}; | ||||
| 
 | ||||
| 	// Initialize the configuration. 
 | ||||
| 	setup() { | ||||
| 		// the OOBE must be in the config.
 | ||||
| 		fetch(config) | ||||
| 			.then((response) => response.json()) | ||||
| 			.then(async (jsonData) => { | ||||
| 				let configuration = jsonData; | ||||
| 				 | ||||
| 				// Run the storage initialization.
 | ||||
| 				delete configuration[`OOBE`]; | ||||
| 				template.set(configuration); | ||||
| 			}) | ||||
| 			.catch((error) => { | ||||
| 				console.error(error); | ||||
| 			}); | ||||
| 	} | ||||
| 
 | ||||
| 	trigger() { | ||||
| 		chrome.runtime.onInstalled.addListener((details) => { | ||||
| 			(details.reason == chrome.runtime.OnInstalledReason.INSTALL) ? this.hello() : null; | ||||
| 			this.setup(); | ||||
| 		}); | ||||
| 	} | ||||
| 
 | ||||
| 	// main function 
 | ||||
| 	constructor () { | ||||
| 		this.trigger(); | ||||
| 
 | ||||
| 		// Might as well set the preferences for storage. 
 | ||||
| 		template.configure(); | ||||
| 		pointer.clear(); | ||||
| 	} | ||||
| } | ||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue