某些网站在放置小部件的每个页面上禁用 Chrome 控制台(console.log 不起作用)。
这是重新启用它的方法吗?(例如黑客或其他什么)
附注据我了解,他们只是重写控制台功能。所以我的问题基本上是这样的:是否有任何解决方案可以将其重写回正常状态?(如果我对阻塞控制台的理解是正确的)
最佳答案
这是一个你可以使用的技巧。我看不到任何方法将其重置为 Chrome 使用的原始 Console
类,例如window.console = new Console()
,因为它似乎无法访问。
相反,您需要获取不同的 console
对象并将当前的 window.console
指向该对象。最简单的方法是创建一个隐藏的 iframe 并使用它。请注意,您必须将 iframe 保留在页面上。如果将其删除,控制台
将被清理并完全停止工作。
这里有一个 jQuery 1-liner 可以做到这一点,a working fiddle :
window.console =
$('<iframe src="about:blank" style="display:none;"/>')
.appendTo(document.body)[0].contentWindow.console;
如果你想要一个纯 JS 解决方案( fiddle ):
var theFrame = document.createElement('iframe');
theFrame.src = "about:blank";
theFrame.style.display = "none";
document.body.appendChild(theFrame);
window.console = theFrame.contentWindow.console;
唯一需要注意的是当前页面本身是否位于带有 sandbox
的 iframe 中ing,就像堆栈片段一样,那么您可能会遇到安全错误,并且对此您无能为力。
此代码段[当前]无法运行,因为它只有 sandbox="allow-scripts"
// break the console
window.console = {
log: function(x) { alert('haha I hijacked your console'); }
}
console.log('debug message');
var theFrame = document.createElement('iframe');
theFrame.src = "about:blank";
theFrame.style.display = "none";
document.body.appendChild(theFrame);
window.console = theFrame.contentWindow.console;
console.log('debug message');
关于javascript - 网站禁用 Chrome 开发者控制台后如何重新启用它?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27562663/