webviewbridge-guide
Guide for using WebViewBridge Swift package to build WebPage (macOS 26.0+) bridges with JavaScript injection and bidirectional communication. Use when integrating WebPage with Swift, injecting JavaScript, or handling JS↔Swift messaging.
What this skill does
# WebViewBridge Guide
This skill helps use the WebViewBridge Swift package for WebPage (macOS 26.0+) bidirectional communication.
## Package Overview
**WebViewBridge** is a Swift framework for WebPage API with JavaScript injection and bidirectional communication.
- **Repository**: https://github.com/Sunalamye/WebViewBridge
- **Platforms**: macOS 26.0+ (WebPage API)
- **License**: MIT
## Installation
```swift
// Package.swift
dependencies: [
.package(url: "https://github.com/Sunalamye/WebViewBridge.git", from: "1.0.0"),
]
targets: [
.target(
name: "YourApp",
dependencies: ["WebViewBridge"]
),
]
```
## Architecture
```
WebViewBridge/
├── Core/
│ └── WebViewBridge.swift - Main bridge class
├── JavaScript/
│ └── bridge-core.js - Core JS utilities
└── WebViewBridgeKit.swift - Version info
```
## Quick Start
### 1. Create Bridge & Register Modules
```swift
import WebViewBridge
import WebKit
@available(macOS 26.0, *)
@MainActor
class MyViewController: NSViewController {
let bridge = WebViewBridge(handlerName: "myBridge")
var webPage: WebPage?
override func viewDidLoad() {
super.viewDidLoad()
// Register core modules
bridge.registerCoreModules()
// Register custom module
bridge.registerModule(JavaScriptModule(
name: "my-module",
source: """
window.myApp = {
sendMessage: function(msg) {
window.__bridgeCore.sendToSwift('custom_message', { message: msg });
}
};
"""
))
// Handle messages
bridge.onMessage = { type, data in
print("Received: \(type) - \(data)")
}
// Configure WebPage
var configuration = WebPage.Configuration()
let userContentController = WKUserContentController()
bridge.configure(contentController: userContentController)
configuration.userContentController = userContentController
// Create WebPage
webPage = WebPage(configuration: configuration)
bridge.configure(webPage: webPage!)
}
}
```
### 2. JavaScript → Swift
```javascript
// Using bridge-core
window.__bridgeCore.sendToSwift('my_event', { key: 'value' });
// Using custom API
window.myApp.sendMessage('Hello!');
```
### 3. Swift → JavaScript
```swift
Task {
// ⚠️ MUST use `return` statement!
let title = try await bridge.callJavaScript("return document.title")
}
```
## ⚠️ Critical: Return Statement
**WebPage.callJavaScript() requires `return` statement!**
```swift
// ❌ WRONG - returns null
try await bridge.callJavaScript("document.title")
// ✅ CORRECT
try await bridge.callJavaScript("return document.title")
try await bridge.callJavaScript("return JSON.stringify({a: 1})")
```
## JavaScriptModule
```swift
let module = JavaScriptModule(
name: "my-module",
source: "window.myAPI = { ... };",
injectAtStart: true, // default: true
mainFrameOnly: false // default: false
)
// Load from bundle
let module = JavaScriptModule.fromBundle(named: "my-script", bundle: .main)
```
## WebViewBridgeDelegate
```swift
@available(macOS 26.0, *)
public protocol WebViewBridgeDelegate: AnyObject {
func bridge(_ bridge: WebViewBridge, didReceiveMessage type: String, data: [String: Any])
func bridge(_ bridge: WebViewBridge, webSocketStatusChanged connected: Bool) // optional
func bridge(_ bridge: WebViewBridge, didEncounterError error: Error) // optional
}
```
## bridge-core.js API
```javascript
// Send to Swift
window.__bridgeCore.sendToSwift(type, data)
// Log
window.__bridgeCore.log(message)
// Base64
window.__bridgeCore.arrayBufferToBase64(buffer)
window.__bridgeCore.base64ToArrayBuffer(base64)
window.__bridgeCore.blobToBase64(blob, callback)
// WebSocket interceptor
window.__bridgeCore.installWebSocketInterceptor({
shouldIntercept: (url) => url.includes('api.example.com')
})
```
## Message Types
| Type | Direction | Description |
|------|-----------|-------------|
| `websocket_open` | JS → Swift | WebSocket connecting |
| `websocket_connected` | JS → Swift | WebSocket connected |
| `websocket_message` | JS → Swift | WebSocket message |
| `websocket_closed` | JS → Swift | WebSocket closed |
| `console_log` | JS → Swift | Log message |
## Complete Example
```swift
@available(macOS 26.0, *)
@MainActor
class WebManager {
let bridge = WebViewBridge(handlerName: "app")
var webPage: WebPage?
func setup() {
bridge.registerCoreModules()
bridge.registerModule(JavaScriptModule(
name: "app-api",
source: """
window.App = {
notify: (msg) => __bridgeCore.sendToSwift('notify', {msg})
};
"""
))
bridge.onMessage = { type, data in
if type == "notify", let msg = data["msg"] as? String {
print("Notification: \(msg)")
}
}
var config = WebPage.Configuration()
let ucc = WKUserContentController()
bridge.configure(contentController: ucc)
config.userContentController = ucc
webPage = WebPage(configuration: config)
bridge.configure(webPage: webPage!)
}
func getTitle() async -> String? {
try? await bridge.callJavaScript("return document.title") as? String
}
}
```
## Checklist
- [ ] Using `@available(macOS 26.0, *)`
- [ ] Called `registerCoreModules()`
- [ ] Configured both `contentController` and `webPage`
- [ ] Using `return` in `callJavaScript()`
- [ ] Set `onMessage` or delegate
## Reference Documentation
- [WebViewBridge Reference](references/reference.md) - Full API and examples
Related in General
modeling-omnistudio-epc-catalog
IncludedSalesforce Industries CME EPC product-modeling skill for Product2-based catalog creation. Use when creating EPC products, configuring product attributes, building offer bundles with Product Child Items, or reviewing EPC DataPack JSON metadata for product catalog changes. TRIGGER when: user creates or updates Product2 EPC records, AttributeAssignment payloads, AttributeMetadata/AttributeDefaultValues, Offer bundles, or ProductChildItem relationships. DO NOT TRIGGER when: designing OmniScripts/FlexCards/Integration Procedures (use building-omnistudio-omniscript, building-omnistudio-flexcard, or building-omnistudio-integration-procedure), implementing Apex business logic (use generating-apex), or troubleshooting deployment pipelines (use deploying-metadata).
relationship-science-coach
IncludedUse this skill for direct, practical adult relationship coaching: couples conflict, repair, trust, marriage, dating, flirting, attachment patterns, emotional connection, sex, desire differences, eroticism, kink negotiation, affection, love languages, breakups, and long-term passion. Draw on Gottman, EFT and Hold Me Tight, attachment science, modern sex research, Perel, Nagoski, Kerner, Schnarch, Love and Stosny, and flexible love-language tools. Be concrete and low-hedge. Redirect only for imminent danger, abuse, coercive control, minors, non-consent, self-harm, stalking, or medical/legal/psychiatric decisions.
building-sf-integrations
IncludedSalesforce integration architecture and runtime plumbing with 120-point scoring. Use this skill to set up Named Credentials, External Credentials, External Services, REST/SOAP callout patterns, Platform Events, and Change Data Capture. TRIGGER when: user sets up Named Credentials, External Services, REST/SOAP callouts, Platform Events, CDC, or touches .namedCredential-meta.xml files. DO NOT TRIGGER when: Connected App/OAuth config (use configuring-connected-apps), Apex-only logic (use generating-apex), or data import/export (use handling-sf-data).
venue-templates
IncludedAccess comprehensive LaTeX templates, formatting requirements, and submission guidelines for major scientific publication venues (Nature, Science, PLOS, IEEE, ACM), academic conferences (NeurIPS, ICML, CVPR, CHI), research posters, and grant proposals (NSF, NIH, DOE, DARPA). This skill should be used when preparing manuscripts for journal submission, conference papers, research posters, or grant proposals and need venue-specific formatting requirements and templates.
let-fate-decide
IncludedDraws the 12 Houses of the Zodiac Tarot spread to inject entropy into planning when prompts are vague, ambiguous, or casually delegated. Interprets the spread to guide next steps. Use when the user says 'let fate decide', 'YOLO', 'whatever', 'idk', or other nonchalant phrases, makes Yu-Gi-Oh references, or when you are about to arbitrarily pick between multiple reasonable approaches. Prefer over ask-questions-if-underspecified when the user's tone is casual or playful rather than precision-seeking.
net-ops
IncludedCross-platform network troubleshooting (Windows, macOS, Linux) via local or remote shell. Use for: DNS broken, can't resolve hostnames, nslookup/dig works but apps fail, NRPT, WFP, scutil, /etc/resolver, systemd-resolved, /etc/resolv.conf, NetworkManager, VPN DNS leak residue (ProtonVPN/Mullvad/WireGuard/AnyConnect), AV/firewall blocking DNS or DoH, Tailscale DNS interaction, intermittent connectivity, remote diagnostics over SSH.