How to run extension code 1x/day when user is active?

Viewed 41

I am working on a chrome extension. It has to open up a new window and interact with it once a day and as early in the day as possible. (This has to be done in the user's browser instead of a separate back-end so it can use their session info.) So for example, if not done so already on a given day, then the extension creates a new window, loads up a site, and interacts with it. Then as soon as the next day hits and the user's Chrome is open, the extension will create the new window again.

How can I ensure this window opens up once a day and as early in the day as possible?

I've seen this answer about using alarms to run once a day, but I worry that if I set the alarm to run at 12pm every day but the user doesn't have Chrome open at that time, then the alarm won't run. I've read another answer saying that I can add the background permission to run even when Chrome is closed, but I think that if 12pm passes when their computer is asleep then the process won't run. (However, if I'm mistaken then please let me know.)

1 Answers

I'm not sure if I understood your problem right but my solution would be to save the date (local storage, synced storage, indexedDB) on the browser startup using the "chrome.runtime.onStartup" event. On the next start of the browser check the current date and compare it to the stored date. If it's older, execute the function and store the date again.

Related