Extension not running after exiting popup window

345 views
Skip to first unread message

Leila Hudson

unread,
Apr 23, 2023, 8:53:58 AM4/23/23
to Chromium Extensions
Hi Chromium Extensions Group,

I am developing a chrome extension that is running functions every 10 seconds using setInterval(). This works fine as long as the popup window is open but once it is closed it stops running. I have added background in my permissions.

Thank you!

Patrick Kettner

unread,
Apr 23, 2023, 10:47:36 AM4/23/23
to Leila Hudson, Chromium Extensions
If your code is inside of a popup, it will only run when the popup is open. The background permission lets you use a background script, it doesn’t make a script run in the background. A background script is powered by a service worker, you can read more about it here - 

--
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/830ab4c4-22b4-41a0-ac09-1d45463380b7n%40chromium.org.

Stefan Van Damme

unread,
Apr 24, 2023, 12:10:07 PM4/24/23
to Chromium Extensions, Chromium Extensions, Leila Hudson
Hi Leila,

Why do you need to refresh it every 10 seconds?

Note:
If you use Manifest V3 background service worker, then you can not use the setTimeout, and you must use chrome.alarms.onAlarm.

Thanks,
Stefan vd

Stefan Van Damme

unread,
Apr 26, 2023, 2:14:35 PM4/26/23
to Leila Hudson, Chromium Extensions
Hi Leila,

If you are making frequent requests every 10 seconds, there is a possibility that you might run into issues. The Chrome extension may work in the Chrome extension developer mode, but not when you publish it in the Chrome Web Store.
As I said before, the setTimeout and setInterval do not work in the background service worker. So you must use the chrome.alarm API. However, please note that the minimum refresh interval using this API is 1 minute (-> no seconds).

Chrome extension developer documentation:

On Mon, Apr 24, 2023 at 7:20 PM Leila Hudson <lshu...@princeton.edu> wrote:
Hi Stefan,

I am running the function every 10 seconds since I'm collecting information each time and computing metrics from it. Would this be too often for the requests to be throttled? The code was contained in a React component. Now I have copied it into my main directory and named it background.js, also setting the service worker as background.js in my manifest.json.

Thank you!

Patrick Kettner

unread,
Apr 30, 2023, 9:27:23 AM4/30/23
to Stefan Van Damme, Leila Hudson, Chromium Extensions
Slight correction - setTimeout and setInterval do work in a background service worker. They can become unreliable if you have a delay that is greater than the lifetime of a serviceworker idle timeout (~30 seconds for idle, ~5 minutes for continuous execution). chrome.alarm lets you get around this issue by scheduling the browser to wakeup at the time defined by your alarm. But it has a minimum resolution of 1 minute, so it won't fire more often than that.

Reply all
Reply to author
Forward
0 new messages