DOM功能在控制台中工作,但不能扩展,尽管等待加载 [英] DOM function works in console but not in extension, despite waiting for it to load

查看:119
本文介绍了DOM功能在控制台中工作,但不能扩展,尽管等待加载的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在撰写扩展程序,以便在YouTube视频中记住您的位置,并在稍后自动恢复。如果我将它们粘贴到YouTube视频页面中,这两行很好:

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

不过,当它们位于Chrome扩展程序中时并非如此。为了说明视频加载异步的可能性,我已经设置了整个页面加载期间的5秒超时。即使它在同一时间在控制台上工作,该扩展仍然会在第二行上抛出错误。



TypeError:document.getElementById(...)。getDuration不是函数(...)



该类型显示为'undefined',因此某些原因,当通过扩展程序访问时,该函数不存在于播放器中。您需要清单文件中的 activeTab 权限。



  {
manifest_version:2,
名称:Youtube简历,
说明:此扩展程序继续播放Youtube视频,
版本:1.0,
content_scripts:[
{
匹配: [*://www.youtube.com/*],
js:[inject.js]
}
],
权限: [
storage,
activeTab
]
}


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.

解决方案

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"
   ]
}

这篇关于DOM功能在控制台中工作,但不能扩展,尽管等待加载的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆