mirror of
https://github.com/toeverything/AFFiNE.git
synced 2026-07-11 23:26:30 +08:00
447b23f25f
Co-authored-by: EYHN <cneyhn@gmail.com>
48 lines
1.1 KiB
Swift
48 lines
1.1 KiB
Swift
//
|
|
// ApplicationBridgedWindowScript.swift
|
|
// App
|
|
//
|
|
// Created by 秋星桥 on 2025/1/8.
|
|
//
|
|
|
|
import Foundation
|
|
import WebKit
|
|
|
|
enum ApplicationBridgedWindowScript: String {
|
|
case getCurrentDocContentInMarkdown = "return await window.getCurrentDocContentInMarkdown();"
|
|
case getCurrentServerBaseUrl = "window.getCurrentServerBaseUrl()"
|
|
case getCurrentWorkspaceId = "window.getCurrentWorkspaceId();"
|
|
case getCurrentDocId = "window.getCurrentDocId();"
|
|
|
|
var requiresAsyncContext: Bool {
|
|
switch self {
|
|
case .getCurrentDocContentInMarkdown: return true
|
|
default: return false
|
|
}
|
|
}
|
|
}
|
|
|
|
extension WKWebView {
|
|
func evaluateScript(_ script: ApplicationBridgedWindowScript, callback: @escaping (Any?) -> ()) {
|
|
if script.requiresAsyncContext {
|
|
callAsyncJavaScript(
|
|
script.rawValue,
|
|
arguments: [:],
|
|
in: nil,
|
|
in: .page
|
|
) { result in
|
|
switch result {
|
|
case .success(let input):
|
|
callback(input)
|
|
case .failure:
|
|
callback(nil)
|
|
}
|
|
}
|
|
} else {
|
|
evaluateJavaScript(script.rawValue) { output, _ in callback(output) }
|
|
}
|
|
}
|
|
}
|
|
|
|
|