@safari-extension-expert

Build, port, and debug Safari Web Extensions (Manifest V3) and native Swift app bridges for macOS and iOS. Scope host permissions, handle non-persistent backgrounds, and test native messaging.

Safari Extension Expert

When to Use

  • Port an existing Chrome/Firefox WebExtension to Safari on macOS or iOS, or create a new Safari Web Extension (Manifest V3).
  • Wire native messaging between the extension JavaScript layer and the containing Swift/Objective-C macOS or iOS app (SafariWebExtensionHandler).
  • Audit manifest.json, host permissions, background execution lifetime, and Xcode target entitlements for Safari compatibility.

Prerequisites

Identify target platforms (macOS, iOS, iPadOS, visionOS), minimum Safari and OS versions, whether an existing WebExtension codebase is being converted via xcrun safari-web-extension-converter, and whether the extension requires native messaging or shared App Group storage. Inspect manifest.json, background/content scripts, and Xcode extension target Info.plist / entitlements before editing.

Design Safari-compatible Manifest V3 and permissions

  1. Target Manifest V3. background is optional (content-script-only or popup-only extensions do not need a background worker); when declared, it must be non-persistent ("background": { "service_worker": "background.js", "type": "module" }, or non-persistent "scripts" or "page"). Safari suspends idle background contexts aggressively, so persist state in browser.storage.local / browser.storage.session rather than module globals.
  2. Prefer Promise-based browser.* APIs (or a small runtime abstraction) and check platform-specific manifest compatibility carefully: Safari supports chrome_url_overrides.newtab on macOS, but does not support bookmarks or history URL overrides (nor newtab on iOS), and Chrome-only APIs such as offscreen or sidePanel behave differently or are unavailable in Safari.
  3. Scope host_permissions to the narrowest required origins or rely on activeTab. Safari prompts users explicitly per website (and per tab on iOS) and treats <all_urls> or *://*/* with high user friction and App Store review scrutiny.
  4. Keep content-script DOM access isolated, declare web_accessible_resources narrowly, and validate every message crossing the content-script ↔ background boundary.

Bridge cleanly to the native Swift host app

  • Use browser.runtime.sendNativeMessage(applicationId, payload) for request/response native calls (with "nativeMessaging" in permissions) and handle rejected promises explicitly when the containing app is unavailable, sandboxed, or missing permissions.
  • On the native side, inspect incoming messages in SafariWebExtensionHandler.beginRequest(with:), validate expected keys/types before running native work, and share persistent files or UserDefaults between the host app and extension target using a matching App Group entitlement on both targets.
  • Keep NSExtensionPrincipalClass and bundle identifiers aligned between the containing app (com.example.app) and the extension (com.example.app.Extension).

Verify locally and on macOS/iOS targets

  • Run cross-platform automated tests on manifest validation rules, permission guards, and message/native-bridge error handling in Node.js (with a mock browser.runtime) before building in Xcode.
  • On macOS with Xcode installed, run xcrun safari-web-extension-converter (for ports), verify signing and App Group entitlements on both targets, enable Unsigned Extensions only during authorized local Safari Develop-menu testing, and verify per-site permission prompts, background suspension/wake-up, and SafariWebExtensionHandler round-trips on the target Safari version.

Example

Validate a Safari MV3 manifest.json against portability mistakes (persistent: true, unsupported chrome_url_overrides keys or iOS newtab, <all_urls>) while allowing valid manifests without a background key or with macOS newtab, and test a sendNativeMessage bridge wrapper across success and unavailable-host paths.

Run the worked Safari WebExtension contract test with Node.js's built-in test runner (node --test).

Limitations

  • Automated Node.js tests verify manifest guardrails and JavaScript message/bridge contracts only; compiling Swift targets, running xcrun safari-web-extension-converter, code signing, App Group entitlement checks, and testing live Safari/iOS behavior require macOS and Xcode.
  • Legacy Safari App Extensions (pre-WebExtension SFSafariExtensionHandler without MV3 WebExtension APIs) follow an older native-only architecture; Apple recommends Safari Web Extensions for new cross-browser work.

Safety and Side Effects

Request the minimum permissions and host_permissions needed. Never embed API secrets or signing credentials in extension JS bundles or manifest.json. Obtain authorization before modifying Xcode signing identities, provisioning profiles, or App Store Connect submissions.