Reputation: 31
I've recently ported my chrome extension to safari using the safari web extension converter: https://developer.apple.com/documentation/safariservices/safari_web_extensions/converting_a_web_extension_for_safari.
However, I'm finding that my setInterval calls only executed a set number of times in the background page. When I open the background page for my web extension, I've tried running a simple interval to see the result:
Safari Web Extension Background Page with interval stuck after running set number of times
When I try the same thing for my chrome web extension, the interval runs indefinitely:
Chrome Web Extension Background Page with interval running continuously
If I try to run the interval after opening the dev console for any random web page in Safari, the interval works fine:
Safari content page with interval running continuously
Similarly, a recursive setTimeout function fails in the background page for the web extension:
Recursive setTimeout function in my background page Result of recursive set timeout function in my background page (stops afterwards)
I'm not sure why timers might be failing in background page for Safari. My extension works perfectly fine in chrome/brave/firefox. Thanks!
Upvotes: 3
Views: 1073
Reputation: 9
Since MV3, setTimeout() or setInterval() can fail in service workers because the timers are canceled whenever the service worker is terminated.
You should convert timers to alarms. As with other listeners, alarm listeners should be registered in the top level of your script.
Upvotes: 0
Reputation: 151
I saw the same issue. setInterval()
was throttled after 6 iterations, no matter the interval. Calls were made, but sporadically, like once a minute at first and even further apart later.
I fixed this by switching to alarms API instead of setInterval()
. Seems to work fine.
Safari 16.3, macOS 12.6 Monterey.
Upvotes: 1
Reputation: 31
When the extension is not active safari browser stops the background process of that specific extension.
Alternative approach: You can create a timeout in the content script and message background from the content script periodically to check your requirement.
Upvotes: 1