admin管理员组文章数量:1336728
Making a Google Chrome extension and need to run a script after the head is loaded, because there are scripts in the head and I need them to run. And before the DOM is loaded, because there's an inlined script in there that I need to beat.
How would I do this? How do I detect when head loads?
Making a Google Chrome extension and need to run a script after the head is loaded, because there are scripts in the head and I need them to run. And before the DOM is loaded, because there's an inlined script in there that I need to beat.
How would I do this? How do I detect when head loads?
Share Improve this question asked Jun 5, 2010 at 3:37 fentfent 18.2k16 gold badges90 silver badges91 bronze badges2 Answers
Reset to default 4When you inject your content script, within the manifest you can state the "run_at" parameter to be "document_start", the files are injected after any files from css, but before any other DOM is constructed or any other script is run. More information can be found here.
{
"name": "My extension",
...
"content_scripts": [
{
"matches": ["http://www.google./*"],
"css": ["mystyles.css"],
"js": ["jquery.js", "myscript.js"],
"run_at": "document_start"
}
],
...
}
*Edited, added an example. One of the mutations event types could be used.
Example
manifest.json
{
"name": "Content Script test",
"version": "0.1",
"description": "Content Script test",
"content_scripts": [
{
"matches": ["http://*/*"],
"js": ["cs.js"],
"run_at": "document_start",
"all_frames": true
}
]
}
cs.js
document.addEventListener('DOMSubtreeModified', OnSubtreeModified, false);
function OnSubtreeModified(event) {
console.log('Hello from extension!');
document.removeEventListener('DOMSubtreeModified', OnSubtreeModified, false);
}
test.html (on the web somewhere)
<html>
<head>
<script>
alert('Hello from Web!');
</script>
</head>
<body>
<h1>Hello World!</h1>
</body>
</html>
Results
You will two alerts, in the order:
- Hello from web!
- Hello from extension!
Just put it at the top of the <body>
tag. Scripts execute where they are placed and will block the rest of the page, so a script at the top of the body will have awareness of everything in the head, but no awareness of the rest of the DOM, which is still loading. If you place it at the bottom of the <body>
it will still be before DOMReady and before the page-load event, plus you should be able to access DOM elements that preceeded the script. One way this can be useful if you need to append scripts dynamically to the <head>
-- you can't do that with a script that's inside the <head>
, so you have to do it in the <body>
.
[Edit] My answer can be applied generally to any HTML document, but Mohamed's answer applies more directly to Google extensions. I'd be inclined to take his answer ahead of mine, though both are probably correct.
本文标签:
版权声明:本文标题:javascript - How do I run a script right after the head is loaded, but before the DOM? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742236576a2438191.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论