How to play and pause a Youtube video in the console?
Asked Answered
H

4

3

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.

Hackneyed answered 8/9, 2015 at 18:25 Comment(2)
What are you trying to accomplish? Are you trying to make a browser extension or something?Cental
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.Ichthyoid
I
7

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

document.getElementsByClassName('ytp-play-button')[0].click();
Ichthyoid answered 8/9, 2015 at 18:37 Comment(1)
I think this would actually work! Thanks! Wow, I can't believe I didn't think of this.Hackneyed
P
12

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.

Praetor answered 22/1, 2016 at 9:58 Comment(0)
I
7

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

document.getElementsByClassName('ytp-play-button')[0].click();
Ichthyoid answered 8/9, 2015 at 18:37 Comment(1)
I think this would actually work! Thanks! Wow, I can't believe I didn't think of this.Hackneyed
C
1

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?

Clout answered 8/9, 2015 at 18:31 Comment(0)
F
0

Here is one!

$('video').playbackRate = 0.1-16

Freer answered 6/4 at 12:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.