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
DOMContentLoadedevent, 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"DOMContentLoadedevent) ?