read filters
can identify an appropriate filter for a website
This commit is contained in:
		
							parent
							
								
									a55b0af151
								
							
						
					
					
						commit
						1505ca7fb2
					
				
					 2 changed files with 120 additions and 65 deletions
				
			
		|  | @ -1,25 +1,25 @@ | |||
| /* secretriat.js  | ||||
| Manage the local cache.  | ||||
| /* secretriat.js | ||||
| Manage the local cache. | ||||
| */ | ||||
| 
 | ||||
| export default class secretariat { | ||||
| 	static read(prefname, cloud = false) { | ||||
| 		/* Read all storeed data in the browser cache.  | ||||
| 		 | ||||
| 		/* Read all storeed data in the browser cache. | ||||
| 
 | ||||
| 		Parameters: | ||||
| 			prefname: (string) the preference name | ||||
| 			cloud: (bool) determine cloud reading, which is otherwise disabled | ||||
| 		Returns: (Object) the preferences | ||||
| 		*/ | ||||
| 		 | ||||
| 		// Initialize the selected pref data. 
 | ||||
| 
 | ||||
| 		// Initialize the selected pref data.
 | ||||
| 		let pref_data; | ||||
| 
 | ||||
| 		function pref_data_set(database) { | ||||
| 			pref_data = database[prefname]; | ||||
| 		}; | ||||
| 
 | ||||
| 		// Retrieve the data. 
 | ||||
| 		// Retrieve the data.
 | ||||
| 		if (cloud) { | ||||
| 			chrome.storage.sync.get(prefname, (items) => { | ||||
| 				pref_data_set(items); | ||||
|  | @ -33,34 +33,63 @@ export default class secretariat { | |||
| 		return(pref_data); | ||||
| 	}; | ||||
| 
 | ||||
| 	static rules(domain = ``) { | ||||
| 		/* Load all of the rules or a rule from a particular domain.  | ||||
| 		 | ||||
| 		Parameters:  | ||||
| 			domain: the RegEx of the domain | ||||
| 	static rules(domain = window.location.href) { | ||||
| 		/* List the matching rule for a particular domain. | ||||
| 
 | ||||
| 		Parameters: | ||||
| 			domain: the website to check, which --- by default --- is the current website | ||||
| 		Returns: (dictionary) the rules | ||||
| 		*/ | ||||
| 		 | ||||
| 
 | ||||
| 		let result; | ||||
| 
 | ||||
| 		// Read the filters.
 | ||||
| 		let filters = read(`filters`); | ||||
| 		if (filters) { | ||||
| 				// Must only run when there stored value.
 | ||||
| 				if (domain.trim()) { | ||||
| 					// Function to loop through each object defined by their URL
 | ||||
| 					function reference(article) { | ||||
| 						/* Skim through each one and set a matching find. | ||||
| 
 | ||||
| 		 | ||||
| 		// Load the data. 
 | ||||
| 		 | ||||
| 		/*  | ||||
| 		if (domain) { | ||||
| 			 | ||||
| 		} else { | ||||
| 			 | ||||
| 		}*/ | ||||
| 		 | ||||
| 		 | ||||
| 		console.log(`Rules are being loaded...`); | ||||
| 						Parameters: | ||||
| 							section: the URL to check | ||||
| 						*/ | ||||
| 
 | ||||
| 						// Set the section in focus
 | ||||
| 						let section = filters[article]; | ||||
| 						let qualified = false; | ||||
| 
 | ||||
| 						// Determine validity
 | ||||
| 						if (section) { | ||||
| 								// The filter must have a matching URL
 | ||||
| 								if (section[`URL`]) { | ||||
| 									// Now it's time to test it.
 | ||||
| 									qualified = (new RegExp(section[`URL`])).test(domain); | ||||
| 									if (qualified && section[`filters`]) { | ||||
| 										// Read that out.
 | ||||
| 										result = section; | ||||
| 									}; | ||||
| 								}; | ||||
| 						}; | ||||
| 
 | ||||
| 					}; | ||||
| 					// The keys
 | ||||
| 					(Object.keys(filters)).forEach(reference); | ||||
| 
 | ||||
| 				} else { | ||||
| 					// Get everything as instructed.
 | ||||
| 					result = filters; | ||||
| 				} | ||||
| 		}; | ||||
| 
 | ||||
| 		// Return the result.
 | ||||
| 		return(result); | ||||
| 	} | ||||
| 
 | ||||
| 	static amend(website, rules) { | ||||
| 		/* Update the rules.  | ||||
| 		 | ||||
| 		/* Update the rules. | ||||
| 
 | ||||
| 		Parameters: | ||||
| 			website: RegEx pattern of the website or the domain | ||||
| 			rules: the rules in JSON | ||||
|  | @ -68,52 +97,59 @@ export default class secretariat { | |||
| 		*/ | ||||
| 
 | ||||
| 
 | ||||
| 
 | ||||
| 	}; | ||||
| 
 | ||||
| 	static write(prefname, data) { | ||||
| 		/* Write the data on the selected prefname.  | ||||
| 		 | ||||
| 		Parameters:  | ||||
| 		/* Write the data on the selected prefname. | ||||
| 
 | ||||
| 		Parameters: | ||||
| 			prefname: the preference name | ||||
| 			data: the new data to be written | ||||
| 		*/ | ||||
| 
 | ||||
| 		 | ||||
| 
 | ||||
| 	}; | ||||
| 
 | ||||
| 	static forget(domain) { | ||||
| 		/* Dangerous: Resets all data or a domain's data.  | ||||
| 		 | ||||
| 		/* Dangerous: Resets all data or a domain's data. | ||||
| 
 | ||||
| 		Parameters: | ||||
| 			domain: the external source of the filter | ||||
| 		Returns: the user's confirmation | ||||
| 		*/ | ||||
| 		 | ||||
| 		let forget_action = false;  | ||||
| 		 | ||||
| 
 | ||||
| 		let forget_action = false; | ||||
| 
 | ||||
| 		(async () => { | ||||
| 			// Import alerts module. 
 | ||||
| 			// Import alerts module.
 | ||||
| 			let alerts = await import(chrome.runtime.getURL("gui/scripts/alerts.js")); | ||||
| 		 | ||||
| 			// Confirm the action. 
 | ||||
| 
 | ||||
| 			// Confirm the action.
 | ||||
| 			let forget_action = alerts.confirm_action(); | ||||
| 		 | ||||
| 
 | ||||
| 			if (forget_action) { | ||||
| 				// Clear the data storage. 
 | ||||
| 				chrome.storage.local.clear(); | ||||
| 				if (domain) { | ||||
| 
 | ||||
| 				} else { | ||||
| 						// Clear the data storage.
 | ||||
| 						chrome.storage.local.clear(); | ||||
| 						chrome.storage.sync.clear(); | ||||
| 				}; | ||||
| 			}; | ||||
| 		})(); | ||||
| 		 | ||||
| 
 | ||||
| 		return (forget_action); | ||||
| 	}; | ||||
| }; | ||||
| 
 | ||||
| export function read(prefname) { | ||||
| 	return(secretariat.read(prefname)); | ||||
| };	 | ||||
| }; | ||||
| 
 | ||||
| export function rules(domain = ``) { | ||||
| export function rules(domain = window.location.href) { | ||||
| 	return(secretariat.rules(domain)); | ||||
| };	 | ||||
| 
 | ||||
| } | ||||
| 
 | ||||
| export function amend(website, rules) { | ||||
| 	return (secretariat.amend(website, rules)); | ||||
|  | @ -122,4 +158,3 @@ export function amend(website, rules) { | |||
| export function forget(domain) { | ||||
| 	return (secretariat.forget(domain)); | ||||
| }; | ||||
| 
 | ||||
|  |  | |||
|  | @ -1,34 +1,54 @@ | |||
| /* Watchman.js  | ||||
| Be sensitive to changes and update the state.  | ||||
| /* Watchman.js | ||||
| Be sensitive to changes and update the state. | ||||
| */ | ||||
| 
 | ||||
| (async () => { | ||||
|     // Import modules. 
 | ||||
|     // Import modules.
 | ||||
|     let secretariat = await import(chrome.runtime.getURL("scripts/secretariat.js")); | ||||
|     let reader = await import(chrome.runtime.getURL("scripts/reader.js")); | ||||
| 
 | ||||
|     class watchman { | ||||
|         static observe(URL = window.location.href) { | ||||
|             /* Check the current URL.  | ||||
|              | ||||
|             Parameters:  | ||||
|             /* Check the current URL. | ||||
| 
 | ||||
|             Parameters: | ||||
|                 URL: the page URL; default value is the current webpage | ||||
|             Returns: (boolean) status | ||||
|             Returns: (dictionary) the filter to follow | ||||
|             */ | ||||
| 
 | ||||
|             // Create the variable to determine the corresponding key. 
 | ||||
|             let key; | ||||
|             // Create the variable to determine the corresponding key.
 | ||||
|             let activity = false; | ||||
|             let filters = secretariat.rules(URL); | ||||
| 
 | ||||
|             secretariat.rules(URL); | ||||
|             // Check if the filters exist.
 | ||||
|             activity = (filters); | ||||
| 
 | ||||
|             return (key); | ||||
|             return (activity); | ||||
|         }; | ||||
| 
 | ||||
|         static act() { | ||||
|         static act(filters) { | ||||
|             /* Act on the page. | ||||
| 
 | ||||
|             Parameters: | ||||
|               filters: the filter to work with | ||||
|             Returns: the state | ||||
|             */ | ||||
| 
 | ||||
|             console.log("ShopAI works here! Click on the button in the toolbar or website to start."); | ||||
|             // TODO
 | ||||
|         }; | ||||
|     }; | ||||
|      | ||||
|     watchman.observe(); | ||||
|     watchman.act(); | ||||
|         } | ||||
| 
 | ||||
|         static job() { | ||||
|           /* The main action. */ | ||||
|           let job_task = watchman.observe(); | ||||
|           if (job_task) { | ||||
|             watchman.act(job_task); | ||||
|           } else { | ||||
|             console.log("ShopAI doesn't work here (yet). Expecting something? Try checking your filters. If you know what you're doing, feel free to create a filter yourself."); | ||||
|           } | ||||
|         } | ||||
|     } | ||||
| 
 | ||||
|     watchman.job(); | ||||
| 
 | ||||
| })(); | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue