Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

getCurrentTime() for YouTube Video

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

like image 343
jmt Avatar asked Apr 03 '14 17:04

jmt


People also ask

How do I pause a YouTube video in Javascript?

Using the contentWindow. The contentWindow. postMessage() sends a message to the particular element. In our case, we will give the command to stop the YouTube video and invoke the stopVideo() function using this method. We can either stop or pause the YouTube video using this method.

How do I allow play embedded YouTube videos on one page?

enablejsapi=1' to the end of the embed URL. Essentially this manager keeps track of the registered videos. If it detects that a registered video begins to play it will pause any other registered video that is currently playing.

How do I unMute an iFrame video?

Until and unless you have allow="autoplay;" in your iframe tag you wont be able to unmute the video. Add this attribute and further unMute function will work.


3 Answers

In 2020, it seems we should use: player.playerInfo.currentTime.

full code on codepen

like image 188
yaya Avatar answered Oct 23 '22 06:10

yaya


Use the following code works for chrome extension:

video = document.getElementsByClassName('video-stream')[0];
console.log(video);
console.log(video.currentTime);
like image 14
Qazi Hassam Tariq Avatar answered Oct 23 '22 06:10

Qazi Hassam Tariq


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

// WARNING: This example is out of date
// For the current HTML5 player, see other answers' code snippets
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 execution in the console of the currently open page. To report the result back, you'll need to use chrome.runtime.sendMessage

like image 1
Xan Avatar answered Oct 23 '22 05:10

Xan