how do I override appendChild()?

后端 未结 2 747
暗喜
暗喜 2021-02-08 10:36
appendChild = function(message) {
    console.log(\"intercepted!\");
}

using the code above does not seem to work.

Anyone knows?

2条回答
  •  再見小時候
    2021-02-08 11:09

    What you might want to replace is Element.prototype.appendChild but it's probably a bad idea.

    This example adds the text intercepted in the inserted element :

    var f = Element.prototype.appendChild;
    Element.prototype.appendChild = function(){f.apply(this, arguments);arguments[0].innerHTML="!Intercepted!"; };
    document.body.appendChild(document.createElement("div"));
    

    Demonstration

提交回复
热议问题