假设我们运行以下代码行
Object.defineProperty(HTMLElement.prototype, 'click', {value: null});
有什么方法可以恢复/恢复原始的click
功能吗?
是的,我知道可以通过 dispatchEvent
触发点击事件,但是可以用类似的方式对其进行修补。我要问的是是否有可能恢复点击事件或以某种方式在它被覆盖后触发点击功能。请假设该行代码是正在运行的第一行代码。
最佳答案
恢复原始实现的一种方法是获取对另一个框架的命名空间的引用,并重新使用该框架的实现。如果页面在 sandbox 中运行,则此方法不起作用尽管没有 allow-same-origin
标志。
// Create a new execution context and get the implementation of "click".
var frame = document.createElement('iframe');
frame.sandbox = 'allow-same-origin';
document.body.appendChild(frame);
var click = frame.contentWindow.HTMLAnchorElement.prototype.click;
frame.remove();
var a = document.createElement('a');
a.href = 'https://example.com';
document.body.appendChild(a);
// Use the implementation.
click.call(a);
a.remove();
关于javascript - 被覆盖后检索/恢复 native javascript函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29884379/