Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

YouTube API and cross origin requests

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.

like image 783
erebel55 Avatar asked May 10 '18 15:05

erebel55


1 Answers

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>
like image 133
relytmcd Avatar answered Sep 24 '22 07:09

relytmcd