3

I am developing a chrome extension. My scripts are called everytime I reload a page or open a new tab. How I do make it to load the extension only once, i.e., when chrome starts and reset all values to default on browser close.

I did go through most of the links available here in stackoverflow but was unable to focus it down to my situation.

manifest.json

"background":{
"scripts":["common.js","example.js"],
"persistent":true
},
"content_scripts": [
{
"matches": ["<all_urls>"],
"js": ["myscript.js"]
}
],
"content_security_policy": "script-src 'self' 'unsafe-eval'; object-src 'self'",
"permissions": ["http://*/*",
"https://*/*",
"contextMenus",
"tabs"]

And in one of my background javascripts, I am triggering an event:

example.js

var DOMContentLoaded_event = document.createEvent("Event");
DOMContentLoaded_event.initEvent("DOMContentLoaded", true, true);
window.document.dispatchEvent(DOMContentLoaded_event);

The above code is to trigger DONContent so that, the user need not click on the extension image everytime he boots the Chrome browser.

The trigger event is getting called each time my page loads, whether it is a reload of the same page or open another tab, the event is getting called. I know I am missing something major here. I did setting things in localStorage. Did NOT work(I mean, the event gets called on refresh of a webpage). I did try the "persistent": true option but in vain.

Can I know what I am missing?

Nikhil

4
  • It is not very clear what you are asking. Yo want to trigger a DOMContentLoaded event, but where do you want to trigger it (background-page, content-script) ? When do you want to trigger it (every time a new page is loaded) ? Why do you want to trigger it (why not listen for the "real" DOMContentLoaded event) ? Commented Jan 13, 2014 at 12:26
  • I am triggering the DOMContentLoaded event in my background javascript and I want it to load, only once when I load my chrome browser. I might be wrong in loading. How do I change to achieve that? Commented Jan 13, 2014 at 12:31
  • Why are you triggering the event manually ? The event is triggered by the browser. Note, though, that a non-persistent background-page gets loaded and unloaded frequently. Commented Jan 13, 2014 at 12:52
  • I have integrated an NACL example I found on pepper links. In that, the first event which happens is, the user has to click on the extension image. The click will trigger the entire NACL application. But I am bypassing the click and loading it when Chrome loads. Commented Jan 13, 2014 at 12:56

1 Answer 1

6

As per your comments, you are trying to register a listener for when Chrome starts and loads your extension. You can achieve this with the chrome.runtime.onStartup event:

Fired when a profile that has this extension installed first starts up. This event is not fired when an incognito profile is started, even if this extension is operating in 'split' incognito mode.

It is as simple as:

chrome.runtime.onStartup.addListener(function () {
    /* Do some initialization */
});
Sign up to request clarification or add additional context in comments.

5 Comments

Will update you regarding the progress soon. Thanks a lot though.
I must have overlooked the onStartup functionality. Thank you so much for that. But as you said, by by-passing the DOMContentLoaded functionality, I am not able to achieve what I wanted. It surely does not fit in this question. Thanks
You got me confused: Why aren't you able to achieve what you wanted ?
No. I am complicating things. I have broken it down. The chrome.runtime.Startup did the job for me :)
I got it now :) Glad to hear that !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.