【发布时间】:2019-02-21 10:05:09
【问题描述】:
我了解如何修改 console.log 函数来拦截日志并将其推送到数组中。但是,这似乎不适用于所有控制台日志。在我的 html 中,我加载了一个 html webapp (\creative\300x600 with video\index.html),它也执行了一个 console.log("click detected"),我可以在我的 chrome 开发工具中看到,但没有被控制台捕获。 ogs(代码示例中的第二个脚本)。我怀疑因为这是一个正在加载的外部文件,所以我无法拦截它。
是否有解决方案可以从任何来源获取所有 console.log 并保存?
<!DOCTYPE html>
<head>
<script>
//add index.html to div on click
function load_home() {
console.log("Loaded");
document.getElementById("content").src='creative\\300x600 with video\\index.html';
}
window.addEventListener("click",load_home);
</script>
<script>
//modify console.log
console.defaultLog = console.log.bind(console);
console.logs = [];
console.log = function() {
console.defaultLog.apply(console, arguments);
console.logs.push(Array.from(arguments));
};
</script>
</head>
<html>
<iframe id="content" style="width: 300px; height:600px; background-color:blue" ></iframe>
</body>
</html>
编辑:
尝试在 div 而不是 iframe 上制作它,同样的问题是无法记录外部 html 的 console.log。只有来自 load_home 函数的console.log("loaded") 被记录。
<!DOCTYPE html>
<head>
<script>
function load_home() {
console.log("Loaded");
document.getElementById("content").innerHTML='<object type="text/html" data="creative\\300x600 with video\\index.html" ></object>';
}
window.addEventListener("click",load_home);
</script>
<script>
console.defaultLog = console.log.bind(console);
console.logs = [];
console.log = function() {
console.defaultLog.apply(console, arguments);
console.logs.push(Array.from(arguments));
};
</script>
</head>
<html>
<div id="content" style="width: 300px; height:600px; z-index:-1;" ,"onclick=load_home();" </div>
</body>
</html>
【问题讨论】:
标签: javascript iframe console.log