Error: Invalid call to runtime.connect(). No runtime.onConnect listeners found.

Hi, I am developing the Click & Read web add-on for Chromium, Firefox and Safari. We use xcrun safari-web-extension-converter tool to generate the Safari add-on. The build on Xcode had no meaning errors/warnings, until we had to switch to a more up-to-date MacBook (required as deprecated Mac had no support for latest macOS and thus for Xcode compatibility !).

So now I build my add-on on an Apple M1 MB Air on Sequoia 15.0 with latest Xcode, but I encounter an error ? or warning ? :

NSBundle file:///System/Library/PrivateFrameworks/MetalTools.framework/ principal class is nil because all fallbacks have failed
Unable to create bundle at URL ((null)): normalized URL null
Unable to create bundle at URL ((null)): normalized URL null
Unable to create bundle at URL ((null)): normalized URL null

But it doesn't prevent the add-on build to complete as I am able to open it onto Safari. Then, when loading the add-on on a test web page, I get this error on safari console:

Error: Invalid call to runtime.connect(). No runtime.onConnect listeners found.

BTW, the same add-on Dist works as expected on Chrome or Firefox (on macOS).

So it seems the runtime listener runtime.onConnect.addListener isn't initiated on Background script...

Thanks for your feedback and help

Answered by Frameworks Engineer in 808236022

The source code does differ from the App Store version, and the attached code fails to parse background.js due to the added diacritics mapping and how the file is being minified. The \u... escape sequences are being converted into literal UTF-8 characters when minified, but the script itself is not being loaded with the correct UTF-8 encoding, leading to a parse error. (You can see this error in the Develop > Web Extension Background Content menu for your extension.)

This issue is not specific to Safari 18. We have observed the same parse error with Safari 17, so it’s not a regression in Safari 18. Chrome and Firefox appear to have different auto-detection rules for text encodings compared to Safari.

Here are a few workarounds you can try:

  1. Add "type": "module" to the background field in your manifest. This will force the scripts to load as UTF-8, as modules require UTF-8 encoding.
  2. Use a page background and provide your own HTML file. Include the script using <script src="background.js" charset="utf8"></script>, ensuring it’s loaded with the correct encoding.
  3. Add a UTF-8 BOM to the beginning of the background.js file to force UTF-8 encoding.

It's hard to know what is going on here without a bit more information.

Is it possible to file feedback on https://feedbackassistant.apple.com with a sample project that reproduces this issue and steps to how you did reproduce it?

Thanks!

Can you test in the latest version of Safari Technology Preview (https://developer.apple.com/safari/technology-preview/) - I expect it to be fixed there.

Hi, Thanks for your support.

in order to be clearer, I'll give more accurate info:

  • Our add-on project web site (only usage and install doc): https://clickandread.inist.fr
  • The command I use to trigger a build with Xcrun:

xcrun safari-web-extension-converter --app-name "Click and Read" --bundle-identifier fr.inist.Click-and-read --swift --force --macos-only dist

  • Once the safari converter having launched Xcode with the generated project, I click on play to build this project. Then I get the add-on app built (with some errors ?) ready to be opened and activated in Safari.

  • Once my add-on enabled and set, I browse on a dedicated testing page, which should be injected with add-on content script (to display some C&R green buttons on the page).

  • Unfortunately, either on LTS or Preview Safari, I get the same error in console...

Please see attached screenshots.

I don't see why the runtime.onConnect event listener cannot be found. According to my code in background script, it should be initialised:

// Listener on sent parsed ID list from content script to be resolved
currentBrowser.runtime.onConnect.addListener((port) => {
  port?.onMessage.addListener(async ({ parsedIdList }) => {
    if (parsedIdList?.length > 0) {
      // some logic here
    }
  });
});

Thank you for all of this updated information! If it's not too much trouble, can you put it in a bug report for us at https://feedbackassistant.apple.com and include the feedback ID in here after filing?

Thanks!

Accepted Answer

The source code does differ from the App Store version, and the attached code fails to parse background.js due to the added diacritics mapping and how the file is being minified. The \u... escape sequences are being converted into literal UTF-8 characters when minified, but the script itself is not being loaded with the correct UTF-8 encoding, leading to a parse error. (You can see this error in the Develop > Web Extension Background Content menu for your extension.)

This issue is not specific to Safari 18. We have observed the same parse error with Safari 17, so it’s not a regression in Safari 18. Chrome and Firefox appear to have different auto-detection rules for text encodings compared to Safari.

Here are a few workarounds you can try:

  1. Add "type": "module" to the background field in your manifest. This will force the scripts to load as UTF-8, as modules require UTF-8 encoding.
  2. Use a page background and provide your own HTML file. Include the script using <script src="background.js" charset="utf8"></script>, ensuring it’s loaded with the correct encoding.
  3. Add a UTF-8 BOM to the beginning of the background.js file to force UTF-8 encoding.
Error: Invalid call to runtime.connect(). No runtime.onConnect listeners found.
 
 
Q