Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/458.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 注册在Google Chrome中加载的DOMContentLoaded_Javascript_Google Chrome_Google Chrome Extension - Fatal编程技术网

Javascript 注册在Google Chrome中加载的DOMContentLoaded

Javascript 注册在Google Chrome中加载的DOMContentLoaded,javascript,google-chrome,google-chrome-extension,Javascript,Google Chrome,Google Chrome Extension,在Firefox和Safari中,我设法用window.addEventListener('DOMContentLoaded',PageShowHandler,false)注册了DOMContentLoaded事件通过将此语句插入到加载页面dom后插入的js脚本中,或者更清楚地说,在加载页面dom后执行,我的特定函数可以在每次加载此特定页面的dom时运行 我似乎不能用Chrome来做这件事。我用chrome.tabs.onUpdated等事件做了一些技巧,但并不是在所有情况下都有效;所有这些事件

在Firefox和Safari中,我设法用window.addEventListener('DOMContentLoaded',PageShowHandler,false)注册了DOMContentLoaded事件通过将此语句插入到加载页面dom后插入的js脚本中,或者更清楚地说,在加载页面dom后执行,我的特定函数可以在每次加载此特定页面的dom时运行

我似乎不能用Chrome来做这件事。我用chrome.tabs.onUpdated等事件做了一些技巧,但并不是在所有情况下都有效;所有这些事件加起来并不等于DOMContentLoaded实现的结果。例如,当我单击网页上的特定链接时,这不会像我的DOMContentLoaded事件那样插入代码

window.addEventListener('DOMContentLoaded', PageShowHandler, false);
引入inject.js似乎没有注册该事件

这是舱单:

{
"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"
}
}
如果向清单中的内容脚本添加
“run\u at”:“document\u start”
标志,则在构建DOM之前将注入这些脚本,因此每次都应触发
DOMContentLoaded

"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"
  }
],

(关于执行顺序的更多信息)

我在注入的脚本中使用了DOMFocusIn事件,成功地使事情运转起来。此事件正确模拟了DOMContentLoaded在Firefox和Safari中实现的触发行为

window.addEventListener('DOMFocusIn', PageShowHandler, false);
如果未将“内容脚本”中的“所有帧”字段设置为true,则无法正常工作:

{
"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"],
   "all_frames" : true
  }
],
"browser_action" : {
"default_icon" : "Quest Icon 11.png",
"default_popup": "dialog.html"
}
}

Chrome(默认情况下)在加载dom时注入内容脚本,所以这可能就是为什么不会触发DOMContentLoaded。你想用DOMContentLoaded实现什么?也许你根本不需要它?这就是我试图实现的目标,这就是为什么我需要从popup.html.*sign*-For
DOMContentLoaded
使用
“document\u end”
For
窗口。onload
使用
“document\u idle”
。查看此模板。考虑到webkit的引擎为您提供了一个不会使页面负担过重的事件处理程序,为文档生命周期中加载的DOMContentLoaded维护一个额外的(您自己的)事件处理程序是不好的做法。