YouTube API 和跨源请求 [英] YouTube API and cross origin requests

查看:24
本文介绍了YouTube API 和跨源请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

YouTube 是否开始锁定跨源请求?

我在我的网站上使用全屏自动播放英雄视频,并且它已经正常运行了很长时间.在过去几周内,它停止工作,我在日志中出现以下错误.

<块引用>

未能在 'DOMWindow' 上执行 'postMessage':提供的目标来源 ('https://www.youtube.com') 与收件人窗口的来源不匹配 ('https://tbrogames.github.io').

根据这个问题的答案

我尝试在 http 和 https 之间更改主机,看看是否能解决它,但没有.

我的网站抛出错误:https://tbrogames.github.io/

我找到了一个更大的游戏网站,也有这个问题.https://playbattlegrounds.com/main.pu

他们还使用 YouTube 视频作为英雄/启动视频;它不再起作用,抛出同样的错误.

相关的javascript可以在这里找到https://github.com/tbrogames/tbrogames.github.io/blob/master/js/defer.js

然而,这已经工作了很长时间,我没有更改任何代码.这就是为什么我认为这是 YouTube 做出的改变.

解决方案

我认为该错误实际上具有误导性.我遇到了同样的问题,但我相信实际上是 chrome 不再自动播放英雄.我收到此错误:Uncaught (in promise) DOMException: play() failed because the user didn't have to interact with the document first. https://developers.google.com/web/updates/2017/09/autoplay-policy-changes>

我的解决方法是在 Javascript 之前 播放视频时调用视频静音.具有相同属性的嵌入的 iframe 版本不会自动播放

Has YouTube started locking down cross origin requests?

I am using a fullscreen autoplay hero video on my website and it has been functioning correctly for a long time. Within the last couple weeks it stopped working and I have the following error in the logs.

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

Per the answer on this question

I tried changing the host between http and https to see if that would fix it and it didn't.

My website that throws the error: https://tbrogames.github.io/

I was able to find a bigger games website that also has this exact issue. https://playbattlegrounds.com/main.pu

They are also using a youtube video as the hero/splash video; and it no longer functions, throwing the same error.

The relevant javascript can be found here https://github.com/tbrogames/tbrogames.github.io/blob/master/js/defer.js

However, this was working for a long time and I didn't change any of the code. Which is why I'm thinking that it is a change that YouTube has made.

解决方案

I think that error is actually misleading. I had the same issue, but I believe that it is actually chrome that is no longer autoplaying the hero. I get this error: Uncaught (in promise) DOMException: play() failed because the user didn't interact with the document first. https://developers.google.com/web/updates/2017/09/autoplay-policy-changes

The fix for me was calling mute on the video in Javascript Before playing the video. The iframe version of the embed with the same properties would not autoplay

<script>
      // 2. This code loads the IFrame Player API code asynchronously.
      var tag = document.createElement('script');

      tag.src = "https://www.youtube.com/iframe_api";
      var firstScriptTag = document.getElementsByTagName('script')[0];
      firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);

      // 3. This function creates an <iframe> (and YouTube player)
      //    after the API code downloads.
      var player;
      function onYouTubeIframeAPIReady() {
        player = new YT.Player('ythero', {
          videoId: '3FjTef9gn3Q',
          height: '100%',
          width: '100%',
          playerVars: {
            rel: 0,
            controls: 0,
            showinfo: 0,
            autoplay: 1,
            loop: 1,
            playlist: '3FjTef9gn3Q',
            modestbranding: 1
          },
          events: {
            'onReady': onPlayerReady,
          }
        });
      }

      // 4. The API will call this function when the video player is ready.
      function onPlayerReady(event) {
        event.target.mute();
        event.target.playVideo();
      }
    </script>

这篇关于YouTube API 和跨源请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆