Set, fetch and check a Cookie after a video (YouTube API) finish - JavaScript

0

I need the browser to understand when a user viewed a YouTube video (YouTube API) to the end and records that, but all client-side. But even though I already have the function that recognizes the end of the video, I can not use and fire other functions inside it (nested function). I'm using Javascript.

Below the code so far:

    //[Interações]

    // criar player youtube 
    var player;
    function onYouTubePlayerAPIReady() {
      player = new YT.Player('player', {
        width: '796',
        height: '448',
        videoId: 'jyPOVbww6fU',
        playerVars: {
         'showinfo': 0,
         'rel': 0
       },
       events: {
        onReady: onPlayerReady,
        onStateChange: onPlayerStateChange
      }
    });
    }

    // autoplay video
    function onPlayerReady(event) {
      event.target.playVideo();
    }

    // função ao terminar o vídeo
    function onPlayerStateChange(event) {        
      if(event.data === 0) {

        //Setar, Pegar e Ler Cookie

        function setCookie(cname,cvalue,exdays) {
          var d = new Date();
          d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
          var expires = "expires=" + d.toGMTString();
          document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
        }

        function getCookie(cname) {
          var name = cname + "=";
          var decodedCookie = decodeURIComponent(document.cookie);
          var ca = decodedCookie.split(';');
          for(var i = 0; i < ca.length; i++) {
            var c = ca[i];
            while (c.charAt(0) == ' ') {
              c = c.substring(1);
            }
            if (c.indexOf(name) == 0) {
              return c.substring(name.length, c.length);
            }
          }
          return "";
        }

      }
    }

function checkCookie() {
  var cookieVideo = getCookie("video1");
  if (cookieVideo != "") {
    console.log('Cookie ON');
  } else {
    console.log('Cookie OFF');
    cookieVideo = "visto"
    if (cookieVideo != "" && cookieVideo != null) {
      setCookie("video1", cookieVideo, 30);
      console.log('Cookie ON');
    }
  }
}

    //[Desabilitar Menu]

  jQuery(document).on('contextmenu', function() {
    return false;
  });
    
asked by anonymous 05.10.2018 / 20:13

1 answer

0

//[Interações]

// criar player youtube 
var player;
function onYouTubePlayerAPIReady() {
  player = new YT.Player('player', {
    width: '796',
    height: '448',
    videoId: 'jyPOVbww6fU',
    playerVars: {
     'showinfo': 0,
     'rel': 0
   },
   events: {
    onReady: onPlayerReady,
    onStateChange: onPlayerStateChange
  }
});
}

//Setar, Pegar e Ler Cookie

function setCookie(cname,cvalue,exdays) {
  var d = new Date();
  d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
  var expires = "expires=" + d.toGMTString();
  document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}

function getCookie(cname) {
  var name = cname + "=";
  var decodedCookie = decodeURIComponent(document.cookie);
  var ca = decodedCookie.split(';');
  for(var i = 0; i < ca.length; i++) {
    var c = ca[i];
    while (c.charAt(0) == ' ') {
      c = c.substring(1);
    }
    if (c.indexOf(name) == 0) {
      return c.substring(name.length, c.length);
    }
  }
  return "";
}

// autoplay video
function onPlayerReady(event) {
  event.target.playVideo();
}

// função ao terminar o vídeo
function onPlayerStateChange(event) {        
  if(event.data === 0) {

    // usar assim:
    setCookie(seu_cname,seu_cvalue,seu_exdays);
    
    // ou assim
    getCookie(seu_cname);
    
    

  }
}
    
08.10.2018 / 19:42