Files
openclaw/apps/shared/OpenClawKit/Sources/OpenClawKit/WebViewJavaScriptSupport.swift
Peter Steinberger 54f45a950b feat(watchos): connect directly to Gateway as a node (#102893)
* feat(watchos): add direct gateway node

* docs: refresh watch node docs map

* chore: leave release notes to release workflow

* chore(ios): refresh native localization inventory

* fix(watchos): keep direct node policy bounded
2026-07-09 15:53:02 +01:00

60 lines
1.8 KiB
Swift

import Foundation
#if canImport(WebKit)
import WebKit
public enum WebViewJavaScriptSupport {
@MainActor
public static func applyDebugStatus(
webView: WKWebView,
enabled: Bool,
title: String?,
subtitle: String?)
{
let js = """
(() => {
try {
const api = globalThis.__openclaw;
if (!api) return;
if (typeof api.setDebugStatusEnabled === 'function') {
api.setDebugStatusEnabled(\(enabled ? "true" : "false"));
}
if (!\(enabled ? "true" : "false")) return;
if (typeof api.setStatus === 'function') {
api.setStatus(\(self.jsValue(title)), \(self.jsValue(subtitle)));
}
} catch (_) {}
})()
"""
webView.evaluateJavaScript(js) { _, _ in }
}
@MainActor
public static func evaluateToString(webView: WKWebView, javaScript: String) async throws -> String {
try await withCheckedThrowingContinuation { cont in
webView.evaluateJavaScript(javaScript) { result, error in
if let error {
cont.resume(throwing: error)
return
}
if let result {
cont.resume(returning: String(describing: result))
} else {
cont.resume(returning: "")
}
}
}
}
public static func jsValue(_ value: String?) -> String {
guard let value else { return "null" }
if let data = try? JSONSerialization.data(withJSONObject: [value]),
let encoded = String(data: data, encoding: .utf8),
encoded.count >= 2
{
return String(encoded.dropFirst().dropLast())
}
return "null"
}
}
#endif