Safari extension

From moasq/nanowave · View interactive version

Safari extension is an AI agent skill from the GitHub repository moasq/nanowave. Safari Web Extension integration with content scripts, background service workers, native messaging via SFSafariExtensionHandler, and manifest v3 configuration. Use when adding a Safari browser extension target to an iOS or macOS app.

How to use this skill

Skill file

Safari Web Extension

Overview

Safari Web Extensions use web technologies (JS/HTML/CSS) plus a native Swift wrapper for browser integration. The extension runs as a separate target alongside the main app.

Setup

Requires a separate extension target configured as kind: "safari" in the plan extensions array.

Native Communication Handler

Implement SFSafariExtensionHandler for bidirectional messaging between Swift and JavaScript:

import SafariServices

class SafariExtensionHandler: SFSafariExtensionHandler {
    override func toolbarItemClicked(in window: SFSafariWindow) {
        window.getActiveTab { tab in
            tab?.getActivePage { page in
                page?.dispatchMessageToScript(withName: "buttonClicked", userInfo: [:])
            }
        }
    }

    override func messageReceived(withName messageName: String, from page: SFSafariPage, userInfo: [String: Any]?) {
        // Handle messages sent from JS via browser.runtime.sendNativeMessage()
    }
}

Info.plist Configuration

Set these keys in the extension target's Info.plist (via XcodeGen):

NSExtensionPrincipalClass: $(PRODUCT_MODULE_NAME).SafariExtensionHandler
NSExtensionAttributes:
  SFSafariWebsiteAccess: { Level: All }

Web Extension Resources

Place these files in the extension bundle:

User Activation

The user must manually enable the extension in Safari → Settings → Extensions before it becomes active.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.