execute background scripts only once in my chrome extension
google-chrome-extension
Solution
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 */
});
Problem
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