SFSafariApplication.dispatchMessage() switches Safari to foreground?

I'm developing a Safari web extension for macOS that calls SFSafariApplication.dispatchMessage() (Swift) to communicate with background.js and the popup. However, I noticed that the method forcefully switches Safari to foreground. It seems it happens after calling browser.runtime.sendNativeMessage(''); in JavaScript.

Is there a way to avoid that behavior? Or perhaps I'm misunderstanding something?

This is a sample project and its video (Safari is switched to foreground every 3 seconds.):

  • Sample project: https://www.dropbox.com/s/vs7h128ef1ji34q/ForcefulForegroundIssuePlayground.zip?dl=0
  • Video: https://www.dropbox.com/s/7pjbh1rihnh068l/Screen%20Recording%202021-12-23%20at%205.42.44%20pm.mov?dl=0

By the way, I already submitted a feedback for this as FB9804951 about 2 weeks ago but no response from Apple so far and I don't know if Apple will take any action for that or not even after the new year so I decided to ask here as well.

Thanks for filing this!

What are you trying to use this messaging for so that you don't want Safari to come to the foreground?

Just an update; I've just chatted with an Apple developer (Timothy Hatcher) in the WWDC's lab. According to him, it's not solved yet, but at least Apple recognizes it's a bug. A certain famous company also sent the same feedback before mine, so my FB9804951 is internally marked as duplicated. I hope it'll be fixed soon. Thanks Timothy. 🙂

SFSafariApplication.dispatchMessage() switches Safari to foreground?
 
 
Q