move background scripts to a dedicated folder, but move actions outside
This commit is contained in:
		
							parent
							
								
									0df989512c
								
							
						
					
					
						commit
						ea6dd9be93
					
				
					 8 changed files with 133 additions and 136 deletions
				
			
		|  | @ -16,7 +16,7 @@ | ||||||
| 	}, | 	}, | ||||||
| 
 | 
 | ||||||
| 	"background": { | 	"background": { | ||||||
| 		"service_worker": "scripts/shopAI.js", "type": "module" | 		"service_worker": "scripts/background/shopAI.js", "type": "module" | ||||||
| 	}, | 	}, | ||||||
| 
 | 
 | ||||||
| 	"content_scripts": [ | 	"content_scripts": [ | ||||||
|  |  | ||||||
							
								
								
									
										14
									
								
								scripts/actions.js
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										14
									
								
								scripts/actions.js
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,14 @@ | ||||||
|  | import Tabs from "/scripts/GUI/tabs.js"; | ||||||
|  | import EntryManager from "/scripts/external/entries/manager.js" | ||||||
|  | 
 | ||||||
|  | export default class user_actions { | ||||||
|  | 	static init() { | ||||||
|  | 		chrome.sidePanel.setPanelBehavior({ openPanelOnActionClick: true }).catch((error) => console.error(error)); | ||||||
|  | 
 | ||||||
|  | 		user_actions.tabs(); | ||||||
|  | 	}; | ||||||
|  | 
 | ||||||
|  | 	static tabs() { | ||||||
|  | 		new EntryManager(); | ||||||
|  | 	} | ||||||
|  | }; | ||||||
|  | @ -1,8 +0,0 @@ | ||||||
| import Tabs from "/scripts/GUI/tabs.js"; |  | ||||||
| import EntryManager from "/scripts/external/entries/manager.js" |  | ||||||
| 
 |  | ||||||
| export default class Watcher_Tabs { |  | ||||||
|      constructor () { |  | ||||||
|           new EntryManager(); |  | ||||||
|      }; |  | ||||||
| } |  | ||||||
|  | @ -1,9 +0,0 @@ | ||||||
| import Watcher_Tabs from "./tabs.js"; |  | ||||||
| 
 |  | ||||||
| export default class user_actions { |  | ||||||
| 	static init() { |  | ||||||
| 		chrome.sidePanel.setPanelBehavior({ openPanelOnActionClick: true }).catch((error) => console.error(error)); |  | ||||||
| 
 |  | ||||||
| 		new Watcher_Tabs(); |  | ||||||
| 	}; |  | ||||||
| }; |  | ||||||
							
								
								
									
										109
									
								
								scripts/background/fc.js
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										109
									
								
								scripts/background/fc.js
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,109 @@ | ||||||
|  | /* fc.js | ||||||
|  | This script provides installation run scripts. | ||||||
|  | */ | ||||||
|  | 
 | ||||||
|  | import { init, read, write, observe } from "../secretariat.js"; | ||||||
|  | import filters from "../filters.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`]; | ||||||
|  | 				init(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(); | ||||||
|  | 	} | ||||||
|  | 
 | ||||||
|  | 	static async every() { | ||||||
|  | 		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 write([`settings`, `sync`], DURATION_PREFERENCES, -1); | ||||||
|  | 			}; | ||||||
|  | 	 | ||||||
|  | 			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 read([`settings`, `sync`, `duration`])) ? (await read([`settings`, `sync`, `duration`] * (60 ** 2) * 1000 != DURATION_PREFERENCES[`duration`])) : false) { | ||||||
|  | 						DURATION_PREFERENCES[`duration`] = await read([`settings`, `sync`, `duration`]) * (60 ** 2) * 1000; | ||||||
|  | 
 | ||||||
|  | 						// Reset the updater. 
 | ||||||
|  | 						updater_cancel(UPDATER); | ||||||
|  | 						UPDATER = updater_set(); | ||||||
|  | 					} | ||||||
|  | 				}; | ||||||
|  | 
 | ||||||
|  | 				observe(updater_cancel); | ||||||
|  | 			}; | ||||||
|  | 		}) | ||||||
|  | 
 | ||||||
|  | 	}; | ||||||
|  | } | ||||||
							
								
								
									
										9
									
								
								scripts/background/shopAI.js
									
										
									
									
									
										Normal file
									
								
							
							
						
						
									
										9
									
								
								scripts/background/shopAI.js
									
										
									
									
									
										Normal file
									
								
							|  | @ -0,0 +1,9 @@ | ||||||
|  | /* ShopAI | ||||||
|  | Shop wisely with AI! | ||||||
|  | */ | ||||||
|  | 
 | ||||||
|  | import fc from './fc.js'; | ||||||
|  | import user_actions from "../actions.js"; | ||||||
|  | 
 | ||||||
|  | fc.run(); | ||||||
|  | user_actions.init(); | ||||||
							
								
								
									
										109
									
								
								scripts/fc.js
									
										
									
									
									
								
							
							
						
						
									
										109
									
								
								scripts/fc.js
									
										
									
									
									
								
							|  | @ -1,109 +0,0 @@ | ||||||
| /* fc.js |  | ||||||
| This does not stand for "FamiCom" but instead on Finalization and Completion. This script provides installation run scripts. |  | ||||||
| */ |  | ||||||
| 
 |  | ||||||
| import { init, read, write, observe } from "./secretariat.js"; |  | ||||||
| import filters from "./filters.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`]; |  | ||||||
|         init(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(); |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   static async every() { |  | ||||||
|     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 write([`settings`, `sync`], DURATION_PREFERENCES, -1); |  | ||||||
|       }; |  | ||||||
|    |  | ||||||
|       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 read([`settings`, `sync`, `duration`])) ? (await read([`settings`, `sync`, `duration`] * (60 ** 2) * 1000 != DURATION_PREFERENCES[`duration`])) : false) { |  | ||||||
|             DURATION_PREFERENCES[`duration`] = await read([`settings`, `sync`, `duration`]) * (60 ** 2) * 1000; |  | ||||||
| 
 |  | ||||||
|             // Reset the updater. 
 |  | ||||||
|             updater_cancel(UPDATER); |  | ||||||
|             UPDATER = updater_set(); |  | ||||||
|           } |  | ||||||
|         }; |  | ||||||
| 
 |  | ||||||
|         observe(updater_cancel); |  | ||||||
|       }; |  | ||||||
|     }) |  | ||||||
| 
 |  | ||||||
|   }; |  | ||||||
| } |  | ||||||
|  | @ -1,9 +0,0 @@ | ||||||
| /* ShopAI |  | ||||||
| Shop wisely with AI! |  | ||||||
| */ |  | ||||||
| 
 |  | ||||||
| import fc from './fc.js'; |  | ||||||
| import user_actions from "./actions/user_actions.js"; |  | ||||||
| 
 |  | ||||||
| fc.run(); |  | ||||||
| user_actions.init(); |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue