I realise that this is something that should probably not be so awkward but I'm still having trouble with the setters/getters.. It isn't so much the getters/setters programmatically, more getting the recursion multiple levels deep correct.
Long story short I want to define a bunch of default settings, then add getters/setters that retrieve/store these settings. Some of these settings contain objects of their own.
This is I'm currently attempting it:
function manipulatePrefs() {}
/**
* Sets a stored variable using whatever storage methods are avalaible
* NOTE: Currently only supports GM_setValue()
*
* @param _pref The name of the stored variable being set
* @param _value The value of the stored variable being set
*
* @return
*
*/
manipulatePrefs.setPref = function(_pref, _value)
{
// _pref = _pref || 'replacement_pref';
// _value = _value || 'replacement_value';
// console.info('manipulatePrefs.setPref:::\n');
// console.info(_pref,_value);
try
{
return GM_setValue(_pref, _value);
} catch(e)
{
console.info('setPref::\nERROR! When trying to set '+_pref+' to '+_value+' ('+(typeof _value)+')');
if('number' == typeof _value)
{
console.info('setPref::\nTesting whether is a float / int');
if(_value === parseInt(_value))
{
console.info('setPref::\nTest:\n(_value === parseInt(_value))\nResult: True\n\nCan be stored');
return GM_setValue(_pref, _value.toString());
}
else if(_value === parseFloat(_value))
{
console.info('setPref::\nTest:\n(_value === parseFloat(_value))\nResult: True\n\nCannot be stored as a float, will convert to string');
return GM_setValue(_pref, _value.toString());
}
}
else if('object' == typeof _value)
{
console.info("setPref::\nTest:\n('object' == typeof _value)\nResult: True\n\nCannot be stored as a object, will JSON.stringify");
return GM_setValue(_pref, JSON.stringify(_value));
}
}
};
/**
* Gets a stored variable using whatever storage methods are avalaible [currently only supports GM_getValue() ]
* Will create the stored variable with the default value if it does not already exist
*
* @param _pref The name of the preference being retrieved
* @param _defaultValue The default value of the preference being stored to fall back upon
*
* @return Returns either the preset value, or the default value if the stored variable did not previously exist
*
*/
manipulatePrefs.getPref = function(_pref, _defaultValue)
{
// console.info('manipulatePrefs.getPref:::\n');
// console.info(_pref,_defaultValue);
// Check if the _preference exists
// NOTE:: Must check against typeof GM_getValue(_pref) *NOT* typeof GM_getValue(_pref, _defaultValue)
if ('undefined' !== typeof GM_getValue(_pref))
{
// console.info('manipulate_prefs.getPref():\n' + _pref + ' was found. Returning the value of GM_getValue(_pref, _defaultValue)');
return GM_getValue(_pref, _defaultValue);
}
else
{
console.info('manipulate_prefs.getPref():\n' + _pref + ' was not found. Set as default value before fetching. Default value = ' + _defaultValue);
manipulatePrefs.setPref(_pref, _defaultValue);
return GM_getValue(_pref, _defaultValue);
}
};
function setterGetter_GM_Storage(storageVar, defaultPreferences)
{
function bindGettersSetters(_objToAddPrefsTo, _defaultPrefs, _property)
{
if('object' !== typeof _objToAddPrefsTo)
{
_objToAddPrefsTo.__defineGetter__(_property, function()
{
var valueToReturn = JSON.parse(manipulatePrefs.getPref(storageVar, JSON.stringify(_defaultPrefs)))[_property];
return valueToReturn;
});
_objToAddPrefsTo.__defineSetter__(_property, function(val)
{
var originalValues = JSON.parse(manipulatePrefs.getPref(storageVar, JSON.stringify(_defaultPrefs)));
originalValues[_property] = val;
manipulatePrefs.setPref(storageVar, JSON.stringify(originalValues));
// Return this so that methods can be strung foo.setA(10).setB(20).setC(30);
return this;
});
}
else
{
for(_property in _objToAddPrefsTo) {
//bindGettersSetters(_objToAddPrefsTo, _defaultPrefs, _property);
}
}
}
/* setterGetter_GM_Storage(storageVar, defaultPreferences) */
for (var property in defaultPreferences)
{
// console.group();
// console.info(JSON.stringify(this));
this[property] = JSON.parse(manipulatePrefs.getPref(storageVar, JSON.stringify(defaultPreferences)))[property] || {};
// console.info(JSON.stringify(this));
bindGettersSetters(this, defaultPreferences, property);
// console.groupEnd();
}
return this;
}
var defaultSettings = {
foo: {
a:1,
b:2,
c:3,
d: new Date(),
e: {
'blah':'foo',
'test':'ing'
}
},
columnPrefixes: {
flag: " | ",
refName: "",
refSince: "",
nextPayment: "",
lastClick: "",
totalClicks: "",
average: "",
clickText: "",
average_1: "",
average_2: "",
RSA: "",
SD: "",
profit: "$"
}
};
var blah = new setterGetter_GM_Storage('prefs', defaultSettings);
console.group();
console.info('blah');
console.info(blah);
console.info(JSON.stringify(blah));
console.groupEnd();
//blah.test = 'teonoihuoust';
console.group();
console.info('blah');
console.info(blah);
console.info(JSON.stringify(blah));
console.groupEnd();