Capturing push
method on all arrays on a webpage can easily be done by overriding the prototype method.
But, a big warning, doing this could potentially cause side effect. In fact a long time ago in Javascript land they used to be a lib called prototype that actually did this, and got a lot of flack for doing it.
Saying that, there is one area were overriding built-in is recommended, and that's pollyfilling new ES features.
If all your wanting to do is log
into the console, maybe for debugging purposes, then your maybe OK.
So with all the warnings out the way, here is an example.
//keep a reference to the old push method
const oldPush = Array.prototype.push;
//now implement our replacement.
Array.prototype.push = function (...args) {
console.log(...args);
return oldPush.call(this, ...args);
}
//lets test.
const a = []
a.push(1)
a.push(1,2,3);
a.push('hello','there');