getCurrentTime() for YouTube Video

独自空忆成欢 提交于 2019-12-03 15:11:21

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.js

ytplayer = 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

Use the following code works for chrome extension:

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

you can only use getElementById when you´r referencing to the correct page. You´r using the right id. if you´r trying to access the play form another page you can use the jquery .load() function

---------EDIT----------

in the sample they do it like so: function getCurrentTime() { var currentTime = player.getCurrentTime(); return roundNumber(currentTime, 3); }

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!