div要素を作成するたびに、フックが「foo」属性をdivにアタッチするように、document.createElement関数にフックしたいと思います。これは私が現在持っているものです:
<script>
window.onload = function () {
console.log("document loaded");
document.prototype.createElement = function (input) {
var div = document.createElement(input);
console.log("createElement hook attached!");
if (input == "div")div.foo = "bar";
return div;
}
document.body.addEventListener('onready', function () {
var div = document.createElement("div");
console.log(div.foo);
});
}
</script>
これをChromeで実行すると、次のようなエラーが発生します
Uncaught TypeError: Cannot set property 'createElement' of undefined test.html:4 window.onload
(上記のエラーメッセージの行番号をコードと一致するように変更しました)
私はここで何が間違っていますか?どうすればこれを修正できますか?