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"
}
}
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.
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:
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.
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.
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"
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With