added initialization feature
This commit is contained in:
		
							parent
							
								
									ee14f52f3f
								
							
						
					
					
						commit
						b2ea655fd3
					
				
					 1 changed files with 88 additions and 17 deletions
				
			
		|  | @ -2,12 +2,12 @@ | |||
| Manage the local cache. | ||||
| */ | ||||
| 
 | ||||
| export function read(prefname, cloud = false) { | ||||
| export function read(prefname, cloud = 0) { | ||||
| 	/* Read all storeed data in the browser cache. | ||||
| 
 | ||||
| 	Parameters: | ||||
| 		prefname: (string) the preference name | ||||
| 		cloud: (bool) determine cloud reading, which is otherwise disabled | ||||
| 		cloud: (bool) determine cloud reading, which is otherwise set to automatic (0) | ||||
| 	Returns: (Object) the preferences | ||||
| 	*/ | ||||
| 
 | ||||
|  | @ -16,14 +16,18 @@ export function read(prefname, cloud = false) { | |||
| 
 | ||||
| 	if (prefname) { | ||||
| 		// Retrieve the data.
 | ||||
| 		if (cloud) { | ||||
| 			chrome.storage.sync.get(null, (database) => {pref_data = database[prefname]}); | ||||
| 		let data_readings = {'sync': null, 'local': null} | ||||
| 		chrome.storage.sync.get(null, (database) => {data_readings[`sync`] = database[prefname]}); | ||||
| 		chrome.storage.sync.get(null, (database) => {data_readings[`local`] = database[prefname]}); | ||||
| 
 | ||||
| 		if ((cloud == 0 && data_readings[`sync`]) || cloud == 1) { | ||||
| 			pref_data = data_readings[`sync`]; | ||||
| 		} else { | ||||
| 			chrome.storage.local.get(null, (database) => {pref_data = database[prefname];}); | ||||
| 		}; | ||||
| 			pref_data = data_readings[`local`]; | ||||
| 		} | ||||
| 	} else { | ||||
| 		// You can get everything if you'd like.
 | ||||
| 		if (cloud) { | ||||
| 		if (cloud > 0) { | ||||
| 			chrome.storage.sync.get(null, (database) => {pref_data = database}); | ||||
| 		} else { | ||||
| 			chrome.storage.local.get(null, (database) => {pref_data = database;}); | ||||
|  | @ -102,14 +106,14 @@ export function specifics(WHERE, domain) { | |||
| 	return(result); | ||||
| } | ||||
| 
 | ||||
| export function write(PREFERENCE, SUBPREFERENCE, DATA, CLOUD = false) { | ||||
| export function write(PREFERENCE, SUBPREFERENCE, DATA, CLOUD = 0) { | ||||
| 	/* Write the data on the selected prefname. | ||||
| 
 | ||||
| 	Parameters: | ||||
| 		PREFERENCE: the preference name | ||||
| 		DATA: the new data to be written | ||||
| 		SUBPREFERENCE: the intermediate data | ||||
| 		CLOUD: store in the cloud, which isn't recommended | ||||
| 		CLOUD: store in the cloud; otherwise set to automatic | ||||
| 	*/ | ||||
| 
 | ||||
| 	let DATA_INJECTED = DATA; | ||||
|  | @ -129,14 +133,15 @@ export function write(PREFERENCE, SUBPREFERENCE, DATA, CLOUD = false) { | |||
| 		DATA_INJECTED = DATA; | ||||
| 	}; | ||||
| 
 | ||||
| 	if (CLOUD) { | ||||
| 	// If CLOUD is set to 0, it should automatically determine where the previous source of data was taken from.
 | ||||
| 	if ((CLOUD == 1) || (CLOUD == 0 && read(PREFERENCE, 1))) { | ||||
| 		chrome.storage.sync.set({[`${PREFERENCE}`]: DATA_INJECTED}); | ||||
| 	} else { | ||||
| 		chrome.storage.local.set({[`${PREFERENCE}`]: DATA_INJECTED}); | ||||
| 	}; | ||||
| } | ||||
| 
 | ||||
| export function forget(preference, subpreference, CLOUD = false) { | ||||
| export function forget(preference, subpreference, CLOUD = 0) { | ||||
| 	/* Dangerous: Resets all data or a domain's data. | ||||
| 
 | ||||
| 	Parameters: | ||||
|  | @ -168,19 +173,85 @@ export function forget(preference, subpreference, CLOUD = false) { | |||
| 					} | ||||
| 				} else { | ||||
| 					// Remove that particular data.
 | ||||
| 					if (CLOUD <= 0) { | ||||
| 						chrome.storage.local.get(null, (data) => { | ||||
| 							delete data[preference]; | ||||
| 
 | ||||
| 							chrome.storage.local.set(data, (result) => {}); | ||||
| 						}); | ||||
| 					}; | ||||
| 					if (CLOUD >= 0) { | ||||
| 						chrome.storage.sync.get(null, (data) => { | ||||
| 							delete data[preference]; | ||||
| 
 | ||||
| 							chrome.storage.sync.set(data, (result) => {}); | ||||
| 						}); | ||||
| 					}; | ||||
| 				}; | ||||
| 			} else { | ||||
| 					// Clear the data storage.
 | ||||
| 					chrome.storage.local.clear(); | ||||
| 					chrome.storage.sync.clear(); | ||||
| 					if (CLOUD >= 0) {chrome.storage.sync.clear();}; | ||||
| 					if (CLOUD <= 0) {chrome.storage.local.clear();}; | ||||
| 			}; | ||||
| 		}; | ||||
| 	})(); | ||||
| 
 | ||||
| 	return (forget_action); | ||||
| } | ||||
| 
 | ||||
| export function init(data) { | ||||
| 	/* Initialize the storage. | ||||
| 
 | ||||
| 	Parameters: | ||||
| 		data: (dictionary) this build's managed data | ||||
| 	*/ | ||||
| 
 | ||||
| 	let PREFERENCES_ALL = {}; | ||||
| 	PREFERENCES_ALL[`build`] = data; | ||||
| 
 | ||||
| 	// Read all data.
 | ||||
| 	chrome.storage.managed.get(null, function(DATA_MANAGED){ | ||||
| 		PREFERENCES_ALL[`managed`] = DATA_MANAGED; | ||||
| 	}); | ||||
| 
 | ||||
| 	chrome.storage.sync.get(null, function(DATA_SYNC){ | ||||
| 		PREFERENCES_ALL[`sync`] = DATA_SYNC; | ||||
| 	}); | ||||
| 
 | ||||
| 	// Merge data.
 | ||||
| 	// Managed > Synchronized > Imported > Local
 | ||||
| 
 | ||||
| 	if (PREFERENCES_ALL[`managed`]) { | ||||
| 		(Object.keys(PREFERENCES_ALL[`managed`])).forEach((item) => { | ||||
| 			let PREFERENCE = {'name': item, 'existing': false}; | ||||
| 
 | ||||
| 			if (PREFERENCES_ALL[`sync`]) { | ||||
| 				PREFERENCE[`existing`] = (PREFERENCES_ALL[`sync`]).hasOwnProperty(PREFERENCE[`name`]); | ||||
| 			} | ||||
| 
 | ||||
| 			if (!PREFERENCE[`existing`]) { | ||||
| 				// Do not allow synchronized data to interfere with managed data.
 | ||||
| 				forget(PREFERENCE[`name`]); | ||||
| 				write(PREFERENCE[`name`], null, PREFERENCES_ALL[`managed`][PREFERENCE[`name`]]); | ||||
| 			} | ||||
| 
 | ||||
| 		}); | ||||
| 	} | ||||
| 
 | ||||
| 	// Import build data
 | ||||
| 	if (PREFERENCES_ALL[`build`]) { | ||||
| 		(Object.keys(PREFERENCES_ALL[`build`])).forEach((item) => { | ||||
| 			let PREFERENCE = {'name': item, 'existing': false}; | ||||
| 
 | ||||
| 			PREFERENCE[`existing`] = ( | ||||
| 				((PREFERENCES_ALL[`sync`]) ? (PREFERENCES_ALL[`sync`]).hasOwnProperty(PREFERENCE[`name`]) : false) || | ||||
| 					((PREFERENCES_ALL[`managed`]) ? (PREFERENCES_ALL[`managed`]).hasOwnProperty(PREFERENCE[`name`]) : false) | ||||
| 			); | ||||
| 
 | ||||
| 			if (!PREFERENCE[`existing`]) { | ||||
| 				write(PREFERENCE[`name`], null, PREFERENCES_ALL[`build`][PREFERENCE[`name`]], -1); | ||||
| 			} | ||||
| 
 | ||||
| 		}); | ||||
| 	} | ||||
| } | ||||
|  |  | |||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue