这是我得到的错误信息:

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。

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


当前回答

当您在调用window.postMessage()时没有指定targetOrigin时,也会收到此消息。

在本例中,我们向第一个iFrame发送消息,并使用*作为目标,这应该允许与任何targetOrigin通信。

window.frames[0].postMessage({
                    message : "Hi there",
                    command :"hi-there-command",
                    data : "Some Data"
                }, '*')

其他回答

确保你从一个URL加载,比如:

https://www.youtube.com/embed/HIbAz29L-FA?modestbranding=1&playsinline=0&showinfo=0&enablejsapi=1&origin=https%3A%2F%2Fintercoin.org&widgetid=1

注意“origin”组件,以及“enablejsapi=1”。原点必须与您的域相匹配,然后它将被列入白名单并工作。

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

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

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

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

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

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

我相信这是一个问题与目标来源是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';

扩展上面@Hokascha的回答,对我来说也是惰性加载,被WordPress自动添加。这段代码将删除站点iframes上的所有延迟加载(添加到functions.php):

function disable_post_content_iframe_lazy_loading( $default, $tag_name, $context ) {
    if ( 'iframe' === $tag_name ) {
        return false;
    }
    return $default;
}
add_filter('wp_lazy_loading_enabled', 'disable_post_content_iframe_lazy_loading', 10, 3);

只需要在播放器的paramVars属性中添加参数“origin”和你网站的URL,就像这样:

this.player = new window['YT'].Player('player', {
    videoId: this.mediaid,
    width: '100%',
    playerVars: { 
        'autoplay': 1,
        'controls': 0,
        'autohide': 1,
        'wmode': 'opaque',
        'origin': 'http://localhost:8100' 
    },
}