The DOM function works in the console, but not in the extension, despite the expectation of its loading

I am writing an extension to remember your place in a YouTube video and automatically resume it later. These two lines work fine if I embed them on a YouTube video page:

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

But not when they are in the Chrome extension. To take into account the possibility of asynchronous video loading, I set a timeout for 5 seconds, during which the entire page loaded. The extension continues to throw an error in 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 displayed as "undefined", so for some reason the function is not available on the player when accessed through the extension.

0
source share
1 answer

You need permission activeTabin 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"
   ]
}
0
source

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


All Articles