When some API is called and I want to know who is calling it.
For example, a web page is navigate to another url somehow, I want to find out why. There must be a function called the location.href=xxx API, but I don't know which is the function. (There are too many function used the location.href so I cannot find it by searching the code.)
My idea is to proxy/intercept the location.href API and add a debugger in it. Just like this: Breaking JavaScript execution when cookie is set.
function debugAccess(obj, prop, debugGet){
var origValue = obj[prop];
Object.defineProperty(obj, prop, {
get: function () {
if ( debugGet )
debugger;
return origValue;
},
set: function(val) {
debugger;
return origValue = val;
}
});
};
debugAccess(document, 'cookie');
But the code is not working for window.location.This API is not configurable, so we cannot use defineProperty to proxy it.
> Object.getOwnPropertyDescriptor(window.location, "href")
< {enumerable: true, configurable: false, get: ƒ, set: ƒ}
Is there other way to do this?