I'm developping a Google Chrome extension, using content script. I want to interact with pages embedding a YouTube video player. I have include the www-widgetapi-vfljlXsRD.js
as a JavaScript file, and YouTube namespace is correctly initialize inside the extension sandbox.
I'm trying to retrieve a reference to an existing iFrame player. To achieve that, I tried this:
var ytplayer = new YT.Player('ytplayer');
ytplayer.pauseVideo();
where div#ytplayer
is the iFrame embedding the actual player.
I'm getting this error, telling that the method does not exist:
TypeError: Object # has no method 'pauseVideo'
What is the correct way to retrieve a reference to an existing player?
I was using YouTube player API before and it was working properly. Today I have issues like you, and I did not change anything in my code. It might mean that the www-widgetapi-vfljlXsRD.js
has been changed and encounters bugs... I cannot help any further.
Ok, I found my way to talk to an existing player. I have not actually managed to get a working reference to it.
I was inspired by the amazing work of Rob (cf. YouTube iframe API: how do I control a iframe player that's already in the HTML?), I remarked that the Youtube player is sending message to the main window, like this:
{"event":"infoDelivery","info":{"currentTime":3.3950459957122803,"videoBytesLoaded":244,"videoLoadedFraction":0.24482703466872344},"id":1}
You can observe it by listening to the "message" event window.addEventListener('message', function (event) {console.log(event.data)}, false);
in a window containing a playing player.
This infoDelivery
event contains the current player's time and is sent while the video is playing. Listening to this event I am now able to know the current player time.
I have not found a proper solution so far so this one will make the job for the moment.
来源:https://stackoverflow.com/questions/17073962/youtube-player-api-retrieving-a-reference-to-an-existing-player