I am creating a Google Chrome extension that relies on knowing the current playing time of a video on 'youtube.com'. I know, in principle, that document.getElementById('movie_player').getCurrentTime() should return this, but calling it in the contents of the script entered on the page produces:
Not Prepared TypeError: document.getElementById ('movie_player'). getCurrentTime is not a function (...)
I can run the command in the Chrome console and get the correct return value.
And before this is flagged as a duplicate question, I want to indicate that I am talking about it on "youtube.com". I don't know if special behavior should be expected, but this is definitely a different context than the other questions I found in stackoverflow.
To help, below is the simplest code I could reproduce in order to reproduce this problem. To make it work, you need to download this as an unpacked extension . Then go to any YouTube video. Click the extension icon and click the "Start Test" button. Also make sure the console is open ( F12 ).
Thank you in advance for suggestions on how to make this method work, or in some other way I can get the information I need (for example, I was looking for a fraction of the time, but could not display it). As a last resort, I am considering replacing the YouTube player with my own iframe and trying this.
popup.html
<!doctype html> <html> <head> <title>Getting Started Extension 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());
manifest.json
{ "manifest_version": 2, "name": "It 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/*" ] }