getElementById无法在Google Chrome浏览器扩展程序中嵌入< embed> [英] getElementById not working in Google Chrome extension for <embed>

查看:874
本文介绍了getElementById无法在Google Chrome浏览器扩展程序中嵌入< embed>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Google Chrome扩展内容脚本中,我有以下内容:

  jQuery(document).ready(function(){ 
var player = document.getElementById('player');
console.log(player);
console.log(document);
});

我在任何Hulu视频上运行它,其中嵌入了id ='player'

 < embed id =playertype =application / x-shockwave-flashsrc =/ site-player /playerwrapper.swf?cb=e80c477cLwidth =100%height =428style =z-index:10; name =playerquality =highallowedcriptaccess =alwaysallowfullscreen =truebgcolor =#000000flashvars =bitrate = 700000&user_id = -1& amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; amp; &放大器;放大器;引荐= HTTP://www.hulu.com/watch/20338/saturday-night-live-steve-carell-monologue#continuous_play=on&安培; continuous_play_on =真放大器;放大器; sortBy =&放大器;放大器; initMode = 4&放大器;放大器;模式= 4&放大器;放大器; CONTENT_ID = 7751491&放大器;放大器; CB = 2011-3-8-8&放大器;放大器; v = 3\" > 

例如,以下链接 http://www.hulu.com/watch/20337/saturday-night-live-snl- digital-short-the-Japanese-office



但是当脚本运行时,console.log(player)返回null。但是,console.log(文档)返回适当的文档对象。
即使我尝试 var player = Document.prototype.getElementById.apply(document,['player']);
我仍​​然得到空值。



有趣的是,如果我从Chrome浏览器JavaScript控制台尝试,它工作正常,我得到正确的< embed> 打印到控制台。

任何想法为什么这不工作在我的内容脚本?

更新:
根据要求,这里是我的铬扩展文件夹的完整内容:
$ b

manifest.json:

  {
name:gebidtest,
version:1,
permissions:[
http:// * / *,
https:// * / *

content_scripts:[{
matches: [http:// * / *],
js:[jquery-1.5.1.min.js,app.js]
}]
}

app.js:

  jQuery(document).ready(function(){
var player = document.getElementById('player');
的console.log(播放器);
console.log(document);
});

jquery-1.5.1.min.js:我刚刚下载了它

我在Mac上使用 Chrome版本10.0.648.127 ,但我也在PC上试过,结果相同。

解决方案

原来问题出在时间上。即使它在一个jQuery(document).ready()中,getElementById失败,因为嵌入尚未加载。当我做一个setTimeout几秒钟,getElementById的作品。


In my Google Chrome extension content script I have the following:

jQuery(document).ready(function() {
    var player = document.getElementById('player');
    console.log(player);
    console.log(document);
});

I'm running it on any Hulu video, which has the following embed with id='player'

<embed id="player" type="application/x-shockwave-flash" src="/site-player/playerwrapper.swf?cb=e80c477cL" width="100%" height="428" style="z-index:10;" name="player" quality="high" allowscriptaccess="always" allowfullscreen="true" bgcolor="#000000" flashvars="bitrate=700000&amp;user_id=-1&amp;startLoadWrapperTime=1299572732904&amp;overrideBrand=&amp;referrer=http://www.hulu.com/watch/20338/saturday-night-live-steve-carell-monologue#continuous_play=on&amp;continuous_play_on=true&amp;sortBy=&amp;initMode=4&amp;modes=4&amp;content_id=7751491&amp;cb=2011-3-8-8&amp;v=3">

For example, the following link http://www.hulu.com/watch/20337/saturday-night-live-snl-digital-short-the-japanese-office

But when the script is run, console.log(player) returns null. However, console.log(document) returns the proper document object. Even if I try var player = Document.prototype.getElementById.apply(document, ['player']); I still get null.

The funny thing is, if I try it from the Chrome JavaScript Console, it works fine, I get the proper <embed> printed to console.

Any ideas why this isn't working in my content script?

UPDATE: As requested, here are the complete contents of my chrome extension folder:

manifest.json:

{
    "name": "gebidtest",
    "version": "1",
    "permissions": [
      "http://*/*",
      "https://*/*"
    ],
    "content_scripts": [{
      "matches": ["http://*/*"],
      "js": ["jquery-1.5.1.min.js","app.js"]
    }]
}

app.js:

jQuery(document).ready( function() {
    var player = document.getElementById('player');
    console.log(player);
    console.log(document);
});

jquery-1.5.1.min.js: I just downloaded it

I'm using Chrome version 10.0.648.127 on a Mac, but I also tried it on a PC with the same results

解决方案

Turns out the problem was with timing. Even though it was inside a jQuery(document).ready(), the getElementById failed because the embed wasn't loaded yet. When I do a setTimeout of a few seconds, the getElementById works.

这篇关于getElementById无法在Google Chrome浏览器扩展程序中嵌入&lt; embed&gt;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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