在Google Chrome中注册DOMContentLoaded [英] Register DOMContentLoaded in Google Chrome
问题描述
我似乎无法在Chrome中执行此操作。我使用 chrome.tabs.onUpdated 等事件做了一些小技巧,但它在任何情况下都不起作用;所有这些事件都不会添加到DOMContentLoaded实现的内容。例如,当我点击我的网页上的特定链接时,这不会注入我的代码,因为我的DOMContentLoaded事件可能已经完成。
window.addEventListener('DOMContentLoaded',PageShowHandler,false);引入到 inject.js 中的
似乎没有注册该事件。
这是清单:
{
名称:gMail Adder,
版本:1.0,
description:Google Chrome Gmail Adder,
options_page:options.html,
background_page:background.html,
run_at:document_start,
权限:[
标签,
历史,
http:// * / *,
https:// * / *
,
content_scripts:[
{
匹配:[*://*.google.mail.com/*,https://*.google.mail.com/*,http://mail.google.com/* ,https://mail.google.com/*,https://www.google.com/*,http://www.google.com/*,file:/// *],
css:[toggle.css],
js:[jquery-1.4.4.min.js,inject.js]
browser_action:{
default_icon:Quest Icon 11.png,
default_popup:popup.html
}
}
DOMContentLoaded
: content_scripts:[
{
匹配:[*://*.google.mail.com/*,https://*.google.mail.com/*, http://mail.google.com/*,https://mail.google.com/*,https://www.google.com/*,http://www.google .com / *,file:/// *],
css:[toggle.css],
js:[jquery-1.4.4.min。 js,inject.js],
run_at:document_start
}
],
(更多关于执行顺序此处)
In Firefox and Safari i managed to register the DOMContentLoaded event with window.addEventListener('DOMContentLoaded', PageShowHandler, false); by inserting this statement into the js script that gets inserted, or more clearly, get executed after the dom of the page is loaded, my specific functions managed to run at every time the DOM of this specific page was loaded.
I can't seem to do this in Chrome. I made some trick with the chrome.tabs.onUpdated et al events but it doesn't work in every instance; all these events don't add up to what the DOMContentLoaded achieves. For example when i click on specific links on my webpage this doesn't inject my code as my DOMContentLoaded event could have done.
window.addEventListener('DOMContentLoaded', PageShowHandler, false);
introduced into inject.js doesn't seem to register the event.
This is the manifest:
{
"name" : "gMail Adder ",
"version" : "1.0",
"description" : "Google Chrome Gmail Adder",
"options_page": "options.html",
"background_page": "background.html",
"run_at": "document_start",
"permissions": [
"tabs",
"history",
"http://*/*",
"https://*/*"
],
"content_scripts": [
{
"matches": ["*://*.google.mail.com/*", "https://*.google.mail.com/*" ,"http://mail.google.com/*" ,"https://mail.google.com/*", "https://www.google.com/*", "http://www.google.com/*", "file:///*"],
"css": ["toggle.css"],
"js": ["jquery-1.4.4.min.js", "inject.js"]
}
],
"browser_action" : {
"default_icon" : "Quest Icon 11.png",
"default_popup": "popup.html"
}
}
If you add "run_at":"document_start"
flag to content scripts in the manifest they will be injected before DOM is constructed, so DOMContentLoaded
should be triggered every time:
"content_scripts": [
{
"matches": ["*://*.google.mail.com/*", "https://*.google.mail.com/*" ,"http://mail.google.com/*" ,"https://mail.google.com/*", "https://www.google.com/*", "http://www.google.com/*", "file:///*"],
"css": ["toggle.css"],
"js": ["jquery-1.4.4.min.js", "inject.js"],
"run_at": "document_start"
}
],
(more about execution order here)
这篇关于在Google Chrome中注册DOMContentLoaded的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!