我有一个使用 window.open 方法的书签。
有时网站会修改此方法,因此我无法预测运行小书签的行为。
有没有办法获得“未触及”的窗口对象?
我尝试注入(inject)一个新的 iframe 并从中获取窗口对象,但是 chrome 阻止了弹出窗口(请参阅 https://bugs.chromium.org/p/chromium/issues/detail?id=932884 )。
window.open("http://example.com");
最佳答案
您可以检查 window.open
是否方法是否是 native 代码,通过使用 .toString()
并检查是否 [native code]
存在。
window.open.toString()
// returns: "function open() { [native code] }"
window.open.toString().includes('[native code]')
// returns: true
window.open = function() { console.log('ding!'); }
window.open.toString().includes('[native code]')
// returns: false
此外,您可以合理地确保 .toString()
尚未通过从原型(prototype)调用它来覆盖它。
Function.prototype.toString.call(window.open).includes('[native code]')
关于javascript - 如何获取 "untouched"窗口对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55645028/