te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - getElementById not working in Google Chrome extension for <embed> - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - getElementById not working in Google Chrome extension for <embed> - Stack Overflow

programmeradmin3浏览0评论

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=;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

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 plete 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

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./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./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 plete 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

Share Improve this question edited Mar 8, 2011 at 20:35 mark asked Mar 8, 2011 at 8:34 markmark 5,0708 gold badges38 silver badges50 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 11

Direct executing DOM related operation in JS is not correct. For example:

default.html

<!DOCTYPE HTML>
<html>
    <head>
    <script type="text/javascript" src="default.js"></script>
    </head>
    <body>
         <a id="Bar" href="#"></a>
    </body>
</html>

default.js

const bar = document.getElementById("Bar");
alert(bar.id);

At this time the DOM content may not be pletely loaded yet, and that is why the "bar" may be "NULL".

The correct way to do this is:

default.js

document.addEventListener('DOMContentLoaded', function() {
  const bar = document.getElementById("Bar");
  alert(bar.id);
  });

That is: executing the DOM related operation when the DOMContentLoaded event happens.

"setTimeout" is not the right way because you cannot make sure the DOM content is pletely loaded by setting some time delay!

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.

Works fine for me in chrome 9.

Try this fiddle: http://jsfiddle/Ee7mq/

If the console doesn't show the player tag something is wrong with your browser. If it does, something is wrong with your code. To figure out what i'd need to see more of it.

Use defer.

<script defer>...</script>
发布评论

评论列表(0)

  1. 暂无评论