正在尝试从YouTube捕获console.log消息<iframe>

Trying to Capture console.log Message from YouTube <iframe>

本文关键字:消息 gt lt log iframe console 捕获 YouTube      更新时间:2023-09-26

YouTube HTML5视频<iframe>为各种事件(如加载广告横幅)触发console.log消息。我正在尝试使用JavaScript以编程方式捕获console.log消息,以触发如下函数:

console['log'] = function(msg){
    // Operate on msg
}

要将console.log消息发送到YouTube <iframe>,以下方法有效(为了便于说明,请使用手写):

document.getElementsByTagName('iframe')[youTubeIframe].contentWindow.console.log(msg);

但是,以下代码不起作用:

document.getElementsByTagName('iframe')[youTubeIframe].contentWindow.console['log'] = function(msg){
    // Operate on msg from YouTube <iframe>
}

我也试过:

window.console = document.getElementsByTagName('iframe')[youTubeIframe].contentWindow.console;
console['log'] = function(msg){
    // Operate on msg
}

我不明白的是;如果我能够向YouTube <iframe>调用console.log消息,那么我如何捕获控制台日志消息?如果有这样的方法,正确的方法是什么?

在最新版本的浏览器上,您不能从保存iFrame的页面所在的不同域访问iFrame内的任何元素,即Chrome v31、Firefox v26、IE11。

它适用于某些特定浏览器的旧版本,但这肯定不会跨浏览器工作。


当前符合W3C HTML5嵌入式内容(包括iframe)规范的浏览器不仅执行同源策略,而且接受新的sandbox属性来指定对iframe的更多限制。

您可以在此页面中看到支持新标准的浏览器:http://html5test.com/compare/feature/security-sandbox.html

接受新属性的浏览器也必须遵守W3C同源策略,因此不会允许您访问console对象(或iframe中的任何其他对象)。