在Google Chrome中注册DOMContentLoaded [英] Register DOMContentLoaded in Google Chrome

查看:147
本文介绍了在Google Chrome中注册DOMContentLoaded的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Firefox和Safari中,我通过将此语句插入插入的js脚本或者更清晰地设置为使用 window.addEventListener('DOMContentLoaded',PageShowHandler,false)注册DOMContentLoaded事件,在加载页面的dom后执行,我的特定函数在每次加载此特定页面的DOM时都运行。



我似乎无法在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
}
}


run_at:document_start标志,它们将在注入之前注入DOM被构造,因此每次触发 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屋!

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