3
votes

Go to an url such as:

https://www.youtube.com/embed/zvCBSSwgtg4

and open the chrome console. I want to know what javascript command will play the youtube video and what javascript command will pause the youtube video.

I've tried using the profiler and inspector to find these commands but they are too well hidden. If someone is really good at javascript debugging, this would be a big help. Thanks!

Yes, I know youtube has API for iframes, but my use case is different.

3
What are you trying to accomplish? Are you trying to make a browser extension or something?Rocket Hazmat
I would like to point out that in most cases, playback of the embed link directly (which is what you have linked here) is blocked. It may work the first time or so, but once you refresh the page, it will not play anymore. It checks to see if it's in an iframe usually.Cameron

3 Answers

7
votes

Have you tried triggering a click on the actual play/pause button?

document.getElementsByClassName('ytp-play-button')[0].click();
8
votes

For HTML5 youtube player simply doing:

document.getElementsByTagName('video')[0].play()

document.getElementsByTagName('video')[0].pause()

I recently created a chrome extension for same: https://chrome.google.com/webstore/detail/youtube-playback-control/okbcoijdeebocmahlanbfemnckjonfnh

Hope it helps.

1
votes

This should be easy to do, just check for event attached to the button that you pushed and then trigger them manually using.

For example if the button has a jQuery event handler use:

http://api.jquery.com/trigger/

Or if it is a native JavaScript event you can use:

How to trigger event in JavaScript?