在youtube.com上获取YouTube视频的当前时间(例如,document.getElementById('movie_player')。getCurrentTime()) [英] Get current time of Youtube Video on youtube.com (e.g. document.getElementById('movie_player').getCurrentTime())

查看:328
本文介绍了在youtube.com上获取YouTube视频的当前时间(例如,document.getElementById('movie_player')。getCurrentTime())的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个Google Chrome扩展程序,该扩展程序依赖于了解在youtube.com上播放视频的当前时间。原则上,我知道 document.getElementById('movie_player')。getCurrentTime()应该返回这个,但是在注入页​​面的内容脚本中调用它会产生:


未捕获的TypeError:
document.getElementById('movie_player')。getCurrentTime不是
函数(...) / p>

我可以在Chrome控制台中运行该命令并获得正确的返回值。



在此之前被标记为重复问题,我想指出,我正在讨论在'youtube.com'上这样做。我不知道我是否应该期待特殊行为,但这绝对是与我在stackoverflow上发现的其他问题不同的上下文。



为了提供帮助,下面是最简单的代码我可以拿出来重现这个问题。为了使其工作,您需要将其作为未打包的扩展名加载。然后导航到任何YouTube视频。点击扩展图标并点击开始测试按钮。此外,请确保您已打开控制台( F12 )。



在此先感谢您提供有关如何使此方法起作用的建议或其他方式我可以得到我需要的信息(例如,我看着获得时间价值的份额,但无法弄清楚这一点)。作为最后的手段,我正在考虑用我自己的iframe替换youtube播放器并尝试这种方式。

popup.html

 <!doctype html> 
< html>
< head>
< title>入门Extension的弹出式菜单< / title>
< script src ='popup.js'>< / script>

< / head>
< body>
< input type ='button'id ='start'value ='开始测试'>< / input>
< p id =messages>< / p>
< p id =result>< / p>
< / body>

< / html>



popup.js

  //添加启动按钮监听器
document.addEventListener('DOMContentLoaded',function(){
document.getElementById(start)。addEventListener('click',process_video) ;
});


函数process_video(){
chrome.tabs.executeScript(null,{file:'test.js'});
}

test.js

  player = document.getElementById('movie_player'); 
console.log(player);
console.log(player.getCurrentTime()); / / code>

manifest.json


//使用此命令生成问题中的错误

  {
manifest_version:2,

name:它是一个占位符,
描述:这只是一个测试,
版本:1.0,

browser_action:{
default_popup:popup.html,
default_title:我的测试


permissions:[
activeTab,
http:// localhost / *,
https://www.youtube.com/*


}

解决方案

感谢Charlie Dalsass(见我的问题下面的评论)

test.js

  video = document.getElementsByClassName('video-stream')[0]; 
console.log(video);
console.log(video.currentTime);


I'm creating a Google Chrome extension that relies on knowing the current time of a video being played on 'youtube.com'. I know, in principle, that document.getElementById('movie_player').getCurrentTime() should return this, but invoking this in a content script injected into the page produces:

Uncaught TypeError: document.getElementById('movie_player').getCurrentTime is not a function(…)

I am able to run the command in the Chrome console and get a proper return value.

And before this is flagged up as a duplicate question, I want to point out that I'm talking about doing this on 'youtube.com'. I dont know if I should expect special behavior, but this is definitely a different context than other questions I've found on stackoverflow.

To help, below is the simplest code I could come up with to reproduce this problem. To get it working, you'll need to load this as an unpacked extension. Then navigate to any youtube video. Click on the extension icon and click the "Start The Test" button. Also make sure you have the console open (F12).

Thanks in advance for suggestions for how to make this method work, or another way I can get the info I need (e.g. I looked into getting the share at time value but couldnt figure that one out). As a last resort, I'm considering replacing the youtube player with my own iframe and trying that way.

popup.html

<!doctype html>
<html>
  <head>
    <title>Getting Started Extension's Popup</title>
    <script src='popup.js'></script>

  </head>
  <body>
    <input type='button' id='start' value='Start The Test'></input>
    <p id="messages"></p>
    <p id="result"></p>
  </body>

</html>

popup.js

// Add listener for start button
document.addEventListener('DOMContentLoaded', function() {
    document.getElementById("start").addEventListener('click', process_video);
});


function process_video() {
    chrome.tabs.executeScript(null, {file: 'test.js'});
}

test.js

player = document.getElementById('movie_player');
console.log(player);
console.log(player.getCurrentTime()); // ERROR IN QUESTION IS PRODUCED WITH THIS COMMAND

manifest.json

{
  "manifest_version": 2,

  "name": "It's a placeholder",
  "description": "This is only a test",
  "version": "1.0",

  "browser_action": {
    "default_popup": "popup.html",
    "default_title": "My Test"
  },

  "permissions": [
    "activeTab",
    "http://localhost/*",
    "https://www.youtube.com/*"
  ]

}

解决方案

Thanks to Charlie Dalsass (see comments below my question), I think I have an answer!

test.js

video = document.getElementsByClassName('video-stream')[0];
console.log(video);
console.log(video.currentTime);

这篇关于在youtube.com上获取YouTube视频的当前时间(例如,document.getElementById('movie_player')。getCurrentTime())的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆