本文介绍了YouTube视频的getCurrentTime()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我正在使用Javascript编写Chrome扩展程序,并且想要获取youtube.com上播放视频的当前时间。我尝试使用问题获取当前YouTube视频时间的答案,例如: ytplayer = document.getElementById(movie_player); ytplayer.getCurrentTime(); 但是我得到以下错误:Uncaught TypeError:无法读取属性'getCurrentTime'为null; 我做错了什么?我为ElementId - movie_player,player ....尝试了不同的值。 任何帮助表示赞赏。 干杯。 编辑: 这是我的清单: {manifest_version:2, name:,description:,version:1.0, browser_action:{default_icon :icon.png,default_popup:basic.html},权限:[标签,activeTab ,http:// * / *,https:// * / *] } 另一件事是:如果我执行这段代码: ytplayer =的document.getElementById( movie_player); ytplayer.getCurrentTime(); 在Youtube页面的Javascript控制台中,它工作正常并返回当前时间。 但是,如果我从扩展名或扩展名的控制台执行此值,则第一行返回值为null。因此,正如Ben所假设的那样,这个问题似乎是我的扩展程序甚至无法访问Youtube页面。 任何帮助表示感谢,所以在此先感谢。 干杯解决方案正如Ben正确认为的那样,您正在执行背景中的代码页面,这是错误的。 要与其他页面交互,您需要在其中注入一个内容脚本。请参阅此处的概述。您可能需要了解消息的工作方式,以便您可以收集内容脚本中的数据并进行通信它作为后台页面。 为了制作一个简单的例子,您可以使用 inject.js ytplayer = document.getElementById(movie_player); ytplayer.getCurrentTime(); 在你的后台页面脚本中,你可以按如下方式将它注入当前页面(当按钮例如) chrome.browserAction.onClicked.addListener(function(tab){ chrome.tabs .executeScript({ file:'inject.js'}); }); 然后,您会在当前打开的页面的控制台中看到执行的结果。要报告结果,您需要使用 chrome.runtime.sendMessage I'm writing a Chrome Extension in Javascript and I want to get the current time for the playing video on youtube.com. I tried using the answer from question Getting Current YouTube Video Time , e.g.: ytplayer = document.getElementById("movie_player"); ytplayer.getCurrentTime();However I do get following error: "Uncaught TypeError: Cannot read property 'getCurrentTime' of null";What I am doing wrong? I tried different values for the ElementId - movie_player, player....Any help is appreciated. Thanks in advance.Cheers.edit:Here is my manifest:{ "manifest_version": 2, "name": "", "description": "", "version": "1.0", "browser_action": { "default_icon": "icon.png", "default_popup": "basic.html" }, "permissions": [ "tabs", "activeTab", "http://*/*", "https://*/*" ]}Another thing is:If I execute this code:ytplayer = document.getElementById("movie_player");ytplayer.getCurrentTime();In the Javascript console on a Youtube Page it works fine and returns the current time.If, however I execute this value from the extension or the console of the extension, the first line return value null.So, as Ben assumed below, the issue seems to be that my extension doesn't even access the Youtube page.Any help is appreciated, so thanks in advance.Cheers 解决方案 As Ben correctly assumed, you're executing the code in the context of your background page, which is wrong.To interact with other pages, you need to inject a content script in them. See overview of that here. You'll probably need to learn how Messaging works so you can gather data in a content script and communicate it to the background page.To make a minimal example, you can have a file inject.jsytplayer = document.getElementById("movie_player");ytplayer.getCurrentTime();And in your background page script, you can inject that into the current page as follows (when the button is clicked, for instance)chrome.browserAction.onClicked.addListener(function(tab) { chrome.tabs.executeScript({ file: 'inject.js' });});Then, you'll see the result of the exeution in the console of the currently open page. To report the result back, you'll need to use chrome.runtime.sendMessage 这篇关于YouTube视频的getCurrentTime()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 09-18 18:44