5

I want to fire a event just when my chrome extension starts. How do I achieve this? Is there a event listener that triggers when extension starts?

for example:-

chrome.runtime.onInstalled.addListener(function(){
        console.log('i will run once!');
});

Similar to this but not on installed but on start and it should only fire once during the extension's run time which is when it starts.

m1alesis
  • 670
  • 6
  • 16

3 Answers3

4

You could use an onload event in your background page and put the code that you want to be executed on extension "start-up" in there.

Example (inside background.js):

window.onload = function() {
  console.log("Extension has started...");
};

That message will be logged (once) when:

  • Extension is installed
  • Chrome is started (and extension is enabled)
  • Extension is enabled from disabled state
berrberr
  • 1,914
  • 11
  • 16
  • its not working for me D: so i added "background":{"scripts":["background.js"]} in my manifest and i created background.js and added ur window.onload function there. but I never get the console.log(). – m1alesis Jan 05 '15 at 21:03
  • Where are you looking for the console message? It will happen on the background page which you can inspect using the chrome://extensions page – berrberr Jan 05 '15 at 21:07
  • I right clicked on the default_popup page and did inspect element. Is that not where it should console.log() ? – m1alesis Jan 05 '15 at 21:14
  • There is a link on the chrome://extensions page to "Inspect views". See this answer: http://stackoverflow.com/a/10082021/845680 – berrberr Jan 05 '15 at 21:18
  • Oh i see now where it outputs the console.log(). This is not what i wanted though D: What it should do is once i launch the extension's default pop_up, it should run the function once. After user closes the extension's default pop_up and opens again, the function should run again once and so on. – m1alesis Jan 05 '15 at 21:20
  • 1
    That is quite different from the extension startup! I would call that the "popup startup". However, you can reuse the above idea and move it over to the popup file. Just move the code to your popup file (inside `` tags, or better yet a separate JS file) and it will execute each time the popup is opened. – berrberr Jan 05 '15 at 21:27
  • Whether it is `window.onload` or `document.addEventListener('DOMContentLoaded', ...)`, it is only a DOM event and only works when the background page is opened. But it can indeed replace the `chrome.runtime.onStartup` startup event, because the browser will trigger the load event when it starts. But more suggestion is to use `onStartup` because it is more reliable. – weiya ou Apr 25 '21 at 09:31
  • If the code inside background.js : ReferenceError: window is not defined – holden321 Jul 23 '22 at 08:51
1

Do you mean "onStartup" event?

chrome.runtime.onStartup.addListener(function callback)

See Google documentation here

Alexander Dayan
  • 2,846
  • 1
  • 17
  • 30
  • 2
    unfortunately that's not what i am looking for D: . I already tried this and it did not work and I found this answer on why it did not work. http://stackoverflow.com/questions/17621545/chrome-runtime-onstartup-never-fires . I want a event when extension starts not when chrome starts. Thanx for your help thought :D . – m1alesis Jan 05 '15 at 20:15
0

Just place any code to background.js and it will execute when extension starts

console.log("extension starts");
holden321
  • 1,166
  • 2
  • 17
  • 32