How can I stop a video with Javascript in Youtube?

前端 未结 3 1390
离开以前
离开以前 2020-11-29 03:52

Situation: here, where I pressed some video.

Problem: I try to stop the video by Javascript in the console of Firebug:

<         


        
相关标签:
3条回答
  • 2020-11-29 03:58

    The following works well, tested on wamp server. Just replace the 11-digit ID in the following line with that of the video you want to play.

    http://www.youtube.com/v/***LpbzbyGjJGE***?enablejsapi=1&version=3&playerapiid=ytplayer
    

    Good luck.

     <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Untitled Document</title>
    </head>
    
    <body>
    
    <a href="#" onclick="var myPlayer = document.getElementById('playerid'); myPlayer.pauseVideo();">Pause</a>
    <a href="#" onclick="var myPlayer = document.getElementById('playerid'); myPlayer.playVideo();">Play</a>
    <embed id="playerid" width="500px" height="400px" allowfullscreen="true" allowscriptaccess="always" quality="high" bgcolor="#000000" name="playerid" style="" src="http://www.youtube.com/v/LpbzbyGjJGE?enablejsapi=1&version=3&playerapiid=ytplayer" type="application/x-shockwave-flash">
    </body>
    </html>
    
    0 讨论(0)
  • 2020-11-29 03:59

    Your video is requesting w/ the JSAPI enabled, so you are very close! All you need is a valid reference to the embedded player. Inspecting your page revealed that you are using the HTML DOM element id of "playerid" to identify your player.

    Example:

    <embed id="playerid" width="100%" height="100%" allowfullscreen="true" allowscriptaccess="always" quality="high" bgcolor="#000000" name="playerid" style="" src="http://www.youtube.com/apiplayerbeta?enablejsapi=1&playerapiid=normalplayer" type="application/x-shockwave-flash">
    

    To obtain a reference to the player and then stop the video use the following code:

    var myPlayer = document.getElementById('playerid');
    myPlayer.stopVideo();
    
    0 讨论(0)
  • 2020-11-29 04:21

    So this is quickest way to do it!

    Open Developer Options

    Hover over YouTube play button.

    Press Control/Command + Shift + C

    Select the Button

    Add id to it.

    Go to console and run the following

    var x = document.getElementById('id'); x.click();

    That's it!

    0 讨论(0)
提交回复
热议问题