【发布时间】:2019-08-09 22:24:58
【问题描述】:
我正在制作一个包含各种垫片和 旧浏览器页面中的 polyfills。 问题是它们似乎都没有包含任何东西来制作 'addEventListener' 的第三个参数可选的,因为网页 通常省略它,它会给出“没有足够的论据”。到处都是错误。
我试图用这种方式覆盖它:
Element.prototype.oldaddEventListener = Element.prototype.addEventListener;
Element.prototype.addEventListener = function(event,handler, placeholder) {
if (!arguments[2]) {
this.oldaddEventListener(event,handler, false);
}
else{
this.oldaddEventListener(event,handler, placeholder);
}
}
现在检查它是否替换了元素上的功能:
alert(document.querySelector("div").addEventListener)
提醒原文:
function addEventListener() {
[native code]
}
检查 Element.prototype.addEventListener:
alert(Element.prototype.addEventListener)
显示被覆盖的版本。 所以我尝试覆盖这些:
HTMLElement.prototype.addEventListener,
EventTarget.prototype.addEventListener,
HTMLDivElement.prototype.addEventListener,
Object.prototype.addEventListener,
Node.prototype.addEventListener,
这些都不起作用。它们中的每一个都显示在何时被覆盖 警报,但都不会影响元素的功能。 我在 firebug DOM 检查器中搜索了它的任何实例 仍然显示原件,并覆盖它 'applicationCache'、'content'、'document'、'frames'、'parent'、'self' 和“顶部”。显然这也不起作用。
用户脚本在“文档开始”处运行,我检查它是否运行 在任何其他脚本之前,甚至在元素存在之前。 在页面加载后手动运行脚本也不会执行任何操作。 我发现覆盖元素上的函数的唯一方法是 直接选择该元素并在每个元素上覆盖它 一个一个的元素。
所以我的问题是:如何覆盖“addEventListener”函数 全局的每个元素,在 Firefox 3.x 中?
【问题讨论】:
-
Firefox 3 已有十多年的历史了。获得安全更新的希望已经过去了很长时间。你真的不应该使用它。
-
我专门为旧浏览器上的 polyfill/shim 制作了一个用户脚本
标签: javascript firefox prototype addeventlistener