这是我得到的错误信息:

Failed to execute 'postMessage' on 'DOMWindow': The target origin provided
('https://www.youtube.com') does not match the recipient window's origin 
('http://localhost:9000').

我见过其他类似的问题,其中目标源是http://www.youtube.com,收件人源是https://www.youtube.com,但没有一个像我的问题一样,目标是https://www.youtube.com,源是http://localhost:9000。

我不明白这个问题。有什么问题吗? 我该怎么解决呢?


当前回答

它看起来只是一个Chrome安全系统,阻止重复请求,使用CORB。

https://www.chromestatus.com/feature/5629709824032768

在我的案例中,YouTube在第一次加载相同的网页后阻止访问,该网页有许多视频API数据请求,高负载。

对于负载较低的页面,则不会出现此问题。

在Safari和其他非基于Chronuim的浏览器中,不会出现此问题。

如果我在新的浏览器中加载网页,问题不会发生,当我重新加载同一页面时,问题就出现了。

其他回答

添加= $ {window.location起源。Host}或“*”是不够的。

在它之前添加https://,它就可以工作了。

另外,确保您使用的是一个可以嵌入的URL:去掉视频ID,并连接一个包含YouTube视频前缀和视频ID +嵌入定义的字符串。

我相信这是一个问题与目标来源是https。我怀疑这是因为你的iFrame url使用的是http而不是https。尝试将您要嵌入的文件的url更改为https。

例如:

'//www.youtube.com/embed/' + id + '?showinfo=0&enablejsapi=1&origin=http://localhost:9000';

是:

'https://www.youtube.com/embed/' + id + '?showinfo=0&enablejsapi=1&origin=http://localhost:9000';

您可以将JavaScript保存到本地文件:

https://www.youtube.com/player_api https://s.ytimg.com/yts/jsbin/www-widgetapi-vfluxKqfs/www-widgetapi.js

在第一个文件中,player_api放入以下代码:

if(!window.YT)var YT={loading:0,loaded:0};if(!window.YTConfig)var YTConfig={host:"https://www.youtube.com"};YT.loading||(YT.loading=1,function(){var o=[];YT.ready=function(n){YT.loaded?n():o.push(n)},window.onYTReady=function(){YT.loaded=1;for(var n=0;n<o.length;n++)try{o[n]()}catch(i){}},YT.setConfig=function(o){for(var n in o)o.hasOwnProperty(n)&&(YTConfig[n]=o[n])}}());

在第二个文件中,找到代码:this.a.contentWindow.postMessage(a,b[c]);

将其替换为:

if(this._skiped){
    this.a.contentWindow.postMessage(a,b[c]); 
}
this._skiped = true;

当然,你也可以将其串联成一个文件——这样会更有效率。 这不是一个完美的解决方案,但它是有效的!

My Source: yt_api-concat

这一错误与Youtube在“在某些网站或应用程序上播放”时屏蔽内容有关。更确切地说,是WMG(华纳音乐集团)。

然而,错误消息确实表明,将https iframe导入到http站点是问题所在,在这种情况下并不是这样。

为了避免控制台错误,我使用了类似Artur之前回答的方法来解决这个问题,以下步骤:

Downloaded the YouTube Iframe API (from https://www.youtube.com/iframe_api) to a local yt-api.js file. Removed the code which inserted the www-widgetapi.js script. Downloaded the www-widgetapi.js script (from https://s.ytimg.com/yts/jsbin/www-widgetapi-vfl7VfO1r/www-widgetapi.js) to a local www-widgetapi.js file. Replaced the targetOrigin argument in the postMessage call which was causing the error in the console, with a "*" (indicating no preference - see https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage). Appended the modified www-widgetapi.js script to the end of the yt-api.js script.

这不是最好的解决方案(修补本地脚本来维护,失去对消息发送位置的控制),但它解决了我的问题。

在使用此解决方案之前,请参阅此处关于删除targetOrigin URI的安全警告- https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage

修复了yt-api.js的例子