GetCurrentTime () for a YouTube video

I am writing a Chrome extension in Javascript and I want to get the current time to play the video on youtube.com. I tried using the answer from the question Get the current time of a YouTube video , for example:

ytplayer = document.getElementById ("movie_player");

ytplayer.getCurrentTime ();

However, I get the following error: "Uncaught TypeError: cannot read the getCurrentTime property" null ";

What am I doing wrong? I tried different values ​​for ElementId - movie_player, player ....

Any help is appreciated. Thanks in advance.

Greetings.

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: If I execute this code:

ytplayer = document.getElementById("movie_player");

ytplayer.getCurrentTime();

Javascript Youtube . , , , null.

, , , Youtube.

, .

+4
2

, , .

, script. . . , , Messaging, script .

, inject.js

ytplayer = document.getElementById("movie_player");
ytplayer.getCurrentTime();

script (, )

chrome.browserAction.onClicked.addListener(function(tab) {
  chrome.tabs.executeScript({
    file: 'inject.js'
  });
});

. , chrome.runtime.sendMessage

0

getElementById, . . , jquery .load()

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

:   function getCurrentTime() { var currentTime = player.getCurrentTime(); return roundNumber(currentTime, 3); }

0

Source: https://habr.com/ru/post/1534885/


All Articles