0

I'm writing an extension to remember your spot in a youtube video and automatically resume it later. These two lines work perfectly well if I paste them in a youtube video page:

var player = document.getElementById("movie_player");
player.getDuration();

But not when they are in a Chrome extension. To account for the possibility of the video loading async, I have set a timeout of 5 seconds during which the whole page has loaded. The extension keeps on throwing an error on the second line even when it works on the console at the same time.

TypeError: document.getElementById(...).getDuration is not a function(…)

The type is shown as 'undefined' so for some reason the function is not present on the player when accessed through the extension.

Shane
  • 369
  • 1
  • 2
  • 13
  • Try longer delay as quick test. Could use an interval timer to continually check for element and then clear timer when found – charlietfl May 28 '16 at 01:40
  • The function call is in a timed loop that gets called every 5 seconds. I've ran it for several minutes and gotten the same result. So I'm already doing that but it's not working.. – Shane May 28 '16 at 01:48

1 Answers1

0

You need the activeTab permission in your manifest file.

{
  "manifest_version": 2,
  "name": "Youtube Resume",
  "description": "This extension resumes a Youtube video where you left it off",
  "version": "1.0",
  "content_scripts":[
    {
      "matches": ["*://www.youtube.com/*"],
      "js": ["inject.js"]
    }
  ],
  "permissions": [
    "storage",
    "activeTab"
   ]
}
StrangeOne101
  • 121
  • 2
  • 10
  • I still get the same error with the permission. – Shane May 28 '16 at 01:58
  • 1
    @Shane Looking on youtube, the `movie_player` element is a div so it won't contain any `getDuration()` method. Try using `document.getElementsByClassName("ytp-time-current")[0].innerHTML` and parsing it so you can get the actual time. Edit: I say parsing it because it returns something like "0:07" so you will need to make a function to get the real time from it. – StrangeOne101 May 28 '16 at 02:07
  • @StrangeOne101 you are wrong, it doesn't matter if it's div, it has all functions (try to call in your browser's console): `ytplayer = document.getElementById("movie_player");
    ​…​
    ​ ytplayer.getCurrentTime(); 3358.412652`
    – user924 Aug 20 '17 at 08:31