Trigger click on file upload after pausing the video

时间:2016-04-25 09:31:59

标签: javascript jquery html youtube-api

I am trying to pause the video on the 3rd second and trigger click on the choose file right after that. It is currently not working, even though I have tried creating another element, and trigger click on THAT element (which would click on the file upload) like a chain.

Although that, everything works fine with YouTube and pausing.

Here is my code:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<script type="text/javascript">
(function() {
  var stopPlayAt = 3, // Stop play at time in seconds
      stopPlayTimer;   // Reference to settimeout call

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

  var player;
  window.onYouTubeIframeAPIReady = function() {
    player = new YT.Player("player", {
        "height": "315",
        "width": "560",
        "videoId": "m0hZZTjz-24",
        "events": {
            "onReady": onPlayerReady,
            "onStateChange": onPlayerStateChange
        }
    });
  }


  function onPlayerReady(event) {
    event.target.playVideo();
  }

  function onPlayerStateChange(event) {
    var time, rate, remainingTime;
    clearTimeout(stopPlayTimer);
    if (event.data == YT.PlayerState.PLAYING) {
        time = player.getCurrentTime();
      if (time + .4 < stopPlayAt) {
        rate = player.getPlaybackRate();
        remainingTime = (stopPlayAt - time) / rate;
        stopPlayTimer = setTimeout(fileOpener, remainingTime * 1000); 
        //file opener called here
      }
  }
}



function fileOpener() {
    player.pauseVideo();
    $('#fileToUpload').trigger('click'); 
  //here to trigger the file upload button
}
})();


</script>

<div id="player"></div>

<form action="upload.php" method="post" enctype="multipart/form-data">
    <p>Upload here:</p>
    <input type="file" name="fileToUpload" id="fileToUpload">
    <input type="submit" value="Upload Image" name="submit">
</form>

What is the issue, why isn't click or trigger('click') not working.

1 个答案:

答案 0 :(得分:2)

你不能这样做。这是由于安全限制,这是有目的和设计的。