Skip to content

Instantly share code, notes, and snippets.

@FarisHijazi
Created May 12, 2019 19:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save FarisHijazi/67bbc569c3251d2134f163ea66d83385 to your computer and use it in GitHub Desktop.
Save FarisHijazi/67bbc569c3251d2134f163ea66d83385 to your computer and use it in GitHub Desktop.
JavaScript snoop properties of objects. Bind callbacks to getters and setters for all properties of an object. Useful for observing how a page element has its properties accessed
/**
* Snoops on an object, monitors all attempts to access and set properties
* @param {Object} obj - the object to monitor
* @param {string[]=} props - list of properties to watch, watches all properties by default
* @param {Function=} onset - callback when the property is set (newValue is passed)
* @param {Function=} onget - callback when the property is accessed
* @param verbose
*/
function snoopProperty(obj, props = [], onset = (val) => null, onget = () => null, verbose = true) {
if (typeof onset !== 'function') onset = (val) => null;
if (typeof onget !== 'function') onget = () => null;
var snooper = {
object: obj,
props: props,
records: {
sets: [],
gets: [],
},
/**
* @param props the properties to snoop
*/
snoop: function (props) {
if (props.length === 0) { // default to all properties
props = Object.keys(obj);
} else if (!!props && !props.length) {
props = [props];
}
for (const prop of props) {
obj['__' + prop] = obj[prop]; // actual property
snooper.object.__defineSetter__(prop, function (newValue) {
if(verbose) console.warn(
'Snooper: [', prop, '] was just changed from "', snooper.object['__' + prop], '" to -> "', newValue, '"',
'\non:', snooper.object
);
snooper.records.sets.push({timeStamp: Date.now(), from: snooper.object['__' + prop], to: newValue});
onset.call(snooper.object, newValue);
snooper.object['__' + prop] = newValue;
});
snooper.object.__defineGetter__(prop, function () {
if(verbose) console.warn(
'Snooper: [', prop, '] was just accessed "', snooper.object['__' + prop], '"',
'\non:', snooper.object
);
snooper.records.gets.push({timeStamp: Date.now(), value: snooper.object['__' + prop]});
onget.call(snooper.object);
return snooper.object['__' + prop];
});
}
},
abort: function (props) {
if (props.length === 0) { // default to all properties
props = Object.keys(obj);
} else if (!!props && !props.length) {
props = [props];
}
for (const prop of props) {
snooper.object.__defineSetter__(prop, newValue => {
snooper.object['__' + prop] = newValue;
});
snooper.object.__defineGetter__(prop, () => snooper.object['__' + prop]);
}
}
};
snooper.snoop(props);
return snooper;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment