Register DOMContentLoaded in Google Chrome

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

Answers


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)


I managed to get things working by using the DOMFocusIn event in my injected script. This event correctly mimics the trigger behavior that DOMContentLoaded achieves in Firefox and Safari.

window.addEventListener('DOMFocusIn', PageShowHandler, false);

This doesn't work properly if i don't set to true the "all_frames" field in "contents_scripts":

{
"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"
}
}

Need Your Help

JDK9: An illegal reflective access operation has occurred. org.python.core.PySystemState

java java-9 java-module

I'm trying to run DMelt programs (http://jwork.org/dmelt/) program using Java9 (JDK9), and it gives me errors such as:

Why use XML(SOAP) when JSON so simple and easy to handle?

json soap

Receiving and sending data with JSON is done with simple HTTP requests. Whereas in SOAP, we need to take care of a lot of things. Parsing XML is also, sometimes, hard. Even Facebook uses JSON in Gr...