我有一个使用window.open方法的书签。
有时网站会修改此方法,因此我无法预测运行书签的行为。
有没有办法获得“未触动”的窗口对象?
我尝试注入一个新的iframe并从中获取窗口对象,但随后chrome阻止了弹出窗口(请参阅https://bugs.chromium.org/p/chromium/issues/detail?id=932884)。
window.open("http://example.com");
您可以使用window.open
检查.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()
没有被原型中的调用覆盖。
Function.prototype.toString.call(window.open).includes('[native code]')