Reputation: 2300
I have a chrome extension running various content and background scripts and I was curious if there is a "browser start" action I can listen for and act upon...
For instance in my application there are some basic scripts that I have run on every page load (within content scripts); but it would be much more efficient if there was some way I can run an action only when the browser first opens.
I've been reading around trying to find something but couldn't come up with anything similar. Any guidance or assistance would be greatly appreciated. Thank you in advance for your time.
Upvotes: 11
Views: 18774
Reputation: 419
David Glibertson's answer didn't work for me.
chrome.runtime.onStartup.addListener(function() {
console.log('open');
})
Upvotes: 14
Reputation: 4863
In your manifest file, the file you define in 'background' will run at the 'extension level'. Which is more-or-less the browser level (i.e. it isn't per-tab).
Your manifest file should include this:
"background": {
"scripts": ["extension.js"]
},
Now anything in extension.js will run once when the browser starts. You don't need to explicitly listen for an event in your code, the browser will run it as soon as it loads your extension. You can put a console.log('started')
or something in extensions.js and then inspect the 'background page' (from the extensions page) console to see that this does indeed only run once.
Upvotes: 5
Reputation: 267
I would recommend you to read up on background pages. https://developer.chrome.com/extensions/background_pages.html
In short, background pages exists only once per extension and as you create new tabs, there will still be only one instance of it running.
Upvotes: 4