Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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"
}
}
like image 868
alex Avatar asked Feb 22 '11 18:02

alex


People also ask

What is the JavaScript DOMContentLoaded event?

Summary: in this tutorial, you will learn about the JavaScript DOMContentLoaded event. The DOMContentLoaded fires when the DOM content is loaded, without waiting for images and stylesheets to finish loading.

How do content scripts access chrome APIs?

Content scripts can access Chrome APIs used by their parent extension by exchanging messages with the extension. They can also access the URL of an extension's file with chrome.runtime.getURL () and use the result the same as other URLs. Additionally, content scripts can access the following chrome APIs directly:

When are scripts injected into the Dom?

Scripts are injected after any files from css, but before any other DOM is constructed or any other script is run. Scripts are injected immediately after the DOM is complete, but before subresources like images and frames have loaded.

What is manage chrome policies with Windows Registry?

Manage Chrome policies with Windows registry Applies to Windows users who sign in to a managed account on Chrome browser. As an administrator, you can configure Chrome Browser settings on Microsoft ® Windows ® computers by modifying the Windows registry on each computer where you want a new setting.


2 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)

like image 79
serg Avatar answered Oct 01 '22 12:10

serg


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"
}
}
like image 21
alex Avatar answered Oct 01 '22 13:10

alex