Set, pick up and check a Cookie after a video (Youtube API) finishes - Javascript

Asked

Viewed 95 times

0

I need the browser to understand when a user has seen a Youtube video (Youtube API) until the end and record this, but all client-side. But even though I already have the function that recognizes the end of the video, I can’t use and fire other functions within it (nested Function). I am 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;
  });

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);
    
    

  }
}

  • thanks! And for example, if I need to use the function below inside onPlayerStateChange() and if, how would I do? Function checkCookie() { var cookieVideo = getCookie("seu_cname"); if (cookieVideo != "") { console.log('Cookie ON'); } Else { console.log('Cookie OFF'); cookieVideo = "seen" if (cookieVideo != "" && cookieVideo != null) { setcookie("video4", cookieVideo, 30); console.log('Cookie ON'); } }&##Xa; }

  • I tested the setcookie inside the function and the Console returns this: index.html:59 Uncaught Referenceerror: setcookie is not defined at onPlayerStateChange

Browser other questions tagged

You are not signed in. Login or sign up in order to post.