chrome.runtime.onInstalled.addListener not fired

3,615 views
Skip to first unread message

asmaa salem

unread,
Jun 27, 2022, 9:26:51 AM6/27/22
to Chromium Extensions
Hello, 

I want to detect the first time of extension installation to open it, and I'm using 
"chrome.runtime.onInstalled.addListener" in the background.js  but it's not fired.

Could you support me?

chrome.runtime.onInstalled.addListener(async function(details){
  alert(details, "install time");
  if(details.reason == "install"){
      //call a function to handle a first install

      alert(details, "first time");
  }else if(details.reason == "update"){
      //call a function to handle an update
      alert(details, "second time");

  }
});


Regards,
Asmaa

Juraj M.

unread,
Jun 27, 2022, 9:54:49 AM6/27/22
to Chromium Extensions, a.asma...@gmail.com
You can't use `alert` in the background page / service worker.
Use console.log instead.

asmaa salem

unread,
Jun 27, 2022, 10:34:28 AM6/27/22
to Chromium Extensions, juraj....@gmail.com, asmaa salem
even I change it, still not fired

Juraj M.

unread,
Jun 27, 2022, 10:43:45 AM6/27/22
to Chromium Extensions, a.asma...@gmail.com, Juraj M.
Is the listener registered when the file is executed? If it's being registered asynchronously, it probably won't fire. Other than that I really can't say why it wouldn't run. I use this all over my extensions and it works fine for updates / installs.

馮韋元

unread,
Jun 27, 2022, 11:13:24 PM6/27/22
to Chromium Extensions, juraj....@gmail.com, a.asma...@gmail.com
This piece of code should be put at the highest level in the code. Not nested inside other functions.
In my code I have the following, which works as expected:

chrome.runtime.onInstalled.addListener(function(details) {
    if (details.reason === "install") {
    ...
    } if (details.reason === "update") {
    ...    
    }
});

Robbi

unread,
Jun 28, 2022, 8:57:43 AM6/28/22
to Chromium Extensions, 馮韋元, juraj....@gmail.com, a.asma...@gmail.com
@juraj
can you confirm this assertion?
"If it's being registered asynchronously, it probably won't fire."

so no hope for:
chrome.runtime.onInstalled.addListener (async function foobar (details) {...}
?

Juraj M.

unread,
Jun 28, 2022, 9:01:02 AM6/28/22
to Chromium Extensions, Robbi, 馮韋元, Juraj M., a.asma...@gmail.com
I mean the `addListener` must be called synchronously in the first iteration of the event loop.
But the handler function can be async of course.

Robbi

unread,
Jun 28, 2022, 9:11:58 AM6/28/22
to Chromium Extensions, juraj....@gmail.com, Robbi, 馮韋元, a.asma...@gmail.com
ok, I was beginning to worry 'cause I have same issue when I test the upgrading process from mv2 to mv3.
I'm using two .crx files and a browser which still permits to drag that files in extensions page...
...but the event does not fire :-(

Robbi

unread,
Jun 28, 2022, 1:46:46 PM6/28/22
to Chromium Extensions, Robbi, juraj....@gmail.com, 馮韋元, a.asma...@gmail.com
I've undetstand where is my problem.
From mv2 to mv3:
In mv2 version I have a not persistent background script
In that script I've also registered a service worker.
Now with the updating process the onInstalled event don't fire.
If I remove from previous (mv2) version the SW and I retry the updating process the onInstalled event fire.
I have no idea how to overcame this issue.
Any suggestions?

Simeon Vincent

unread,
Jun 29, 2022, 12:47:59 PM6/29/22
to Robbi, Chromium Extensions, juraj....@gmail.com, 馮韋元, a.asma...@gmail.com
For a functional demo of the onInstalled event working as expected in Manifest V3, check out the Action API Demo extension (source). This extension will open a new tab to demonstrate ways that you can use the Action API immediately after installation.

Simeon - @dotproto
Chrome Extensions DevRel


--
You received this message because you are subscribed to the Google Groups "Chromium Extensions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/2208e799-ccff-4b47-a49a-3e63d2105c56n%40chromium.org.

Robbi

unread,
Jul 1, 2022, 6:02:02 AM7/1/22
to Chromium Extensions, Simeon Vincent, Chromium Extensions, juraj....@gmail.com, 馮韋元, a.asma...@gmail.com, Robbi
Thanks to the great  @wOxxOm I solve my problem.
Link to stackOverflow thread LINK
Link to the bug report: LINK
Reply all
Reply to author
Forward
0 new messages