0

I'm making a youtube web extension. How can I get a reference to YT.Player from the content script on a youtube video page (www.youtube.com/watch?v=videoId)?

In my main content script I have this (from https://developers.google.com/youtube/iframe_api_reference), but the function onYouTubeIframeAPIReady never fires:

var tag = document.createElement('script');

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

var player;

function onYouTubeIframeAPIReady() {

  console.log("onYouTubeIframeAPIReady");

  ////normally I would create the player like below, but I need the existing one
  //player = new YT.Player('player', {
  //    modestbranding: 1,
  //    cc_load_policy: 1,
  //    wmode: "opaque",
  //    height: '390',
  //    width: '640',
  //    videoId: videoId,
  //    enablejsapi: 1,
  //    events: {
  //        'onReady': onPlayerReady,
  //        'onStateChange': onPlayerStateChange,
  //        'onApiChange': onApiChange
  //    }
  //});
}

Interestingly, I can get the player from the browser console:

var player = $("#movie_player");

But this call doesn't work from the content script (I get a different kind of object).

Erazihel
  • 7,295
  • 6
  • 30
  • 53
okkko
  • 1,010
  • 1
  • 13
  • 22
  • Related: https://stackoverflow.com/questions/12256382/youtube-iframe-api-not-triggering-onyoutubeiframeapiready – Shog9 Jul 19 '17 at 20:14
  • @Shog9 Yes, I've seen that and tried to put the function in the background script (I'm still learning and experimenting) but to no avail.. – okkko Jul 19 '17 at 20:16
  • 1
    I suspect the core problem here is that you're running in a sandbox - so your script isn't visible to the script running on the page, and vice-versa. Try injecting your logic directly into the page. – Shog9 Jul 19 '17 at 20:20

0 Answers0