Created
July 2, 2019 12:31
-
-
Save nuxodin/97ffa888ca137868208004f982f826eb to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function interceptObjectProperties(obj, options){ | |
if (!options) options = {}; | |
if (!options.ignore) options.ignore = {}; | |
for (var i in obj) { | |
const prop = i; | |
if (!obj.hasOwnProperty(prop)) continue; | |
if (options.ignore[prop]) continue; | |
var descriptor = Object.getOwnPropertyDescriptor(obj, prop); | |
if (descriptor.set) { | |
const original = descriptor.set; | |
descriptor.set = function(value){ | |
console.log('set', prop, value) | |
return original.call(this, value) | |
} | |
options.log_interceptable && console.log('intercepted setter '+prop); | |
} | |
if (descriptor.get) { | |
const original = descriptor.get; | |
descriptor.get = function(){ | |
var ret = original.apply(this, arguments) | |
console.log('get', prop, ret) | |
return ret; | |
} | |
options.log_interceptable && console.log('intercepted getter '+prop) | |
} | |
if (descriptor.value) { | |
if (typeof descriptor.value === 'function') { | |
const original = descriptor.value; | |
descriptor.value = function(){ | |
console.log('method ', prop, arguments) | |
return original.apply(this, arguments); | |
} | |
options.log_interceptable && console.log('intercepted function '+prop) | |
} | |
} | |
Object.defineProperty(obj, prop, descriptor); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment