gpt4 book ai didi

swift - 无法在 Swift/Cocoa/NextStep 中将 JS 注入(inject) WKWebView/将 WKWebView 中网页上的用户选择推送到 Swift/Cocoa

转载 作者:行者123 更新时间:2023-11-28 11:37:19 29 4
gpt4 key购买 nike

我正在使用一个 MacOS 应用程序,它需要使用 WKUserScript 功能将消息从网页发送回 MacOS 应用程序。我正在处理文章 https://medium.com/capital-one-tech/javascript-manipulation-on-ios-using-webkit-2b1115e7e405这表明这在 iOS 中有效并且工作得很好。

然而,我已经努力了几个星期,试图让它在我的 MacOS 中运行。这是我的代码示例,它符合要求并运行但没有成功打印在处理程序 userContentController() 中找到的消息

import Cocoa
import WebKit

class ViewController: NSViewController, WKNavigationDelegate {

@IBOutlet weak var webView: WKWebView!

override func viewDidLoad() {
super.viewDidLoad()

let userContentController = WKUserContentController()

// Add script message handlers that, when run, will make the function
// window.webkit.messageHandlers.test.postMessage() available in all frames.

userContentController.add(self, name: "test")

// Inject JavaScript into the webpage. You can specify when your script will be injected and for
// which frames–all frames or the main frame only.
let scriptSource = "window.webkit.messageHandlers.test.postMessage(`Hello, world!`);"
let userScript = WKUserScript(source: scriptSource, injectionTime: .atDocumentEnd, forMainFrameOnly: true)
userContentController.addUserScript(userScript)

// let config = WKWebViewConfiguration()
// config.userContentController = userContentController
// let webView = WKWebView(frame: .zero, configuration: config)

webView.navigationDelegate = self
webView.configuration.userContentController = userContentController

// Make sure in Info.plist you set `NSAllowsArbitraryLoads` to `YES` to load
// URLs with an HTTP connection. You can run a local server easily with services
// such as MAMP.

let htmlStr = "<html><body>Hello world - nojs</body></html>"
webView.loadHTMLString(htmlStr, baseURL: nil)

}
}

extension ViewController: WKScriptMessageHandler {
// Capture postMessage() calls inside loaded JavaScript from the webpage. Note that a Boolean
// will be parsed as a 0 for false and 1 for true in the message's body. See WebKit documentation:
// https://developer.apple.com/documentation/webkit/wkscriptmessage/1417901-body.
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
if let messageBody = message.body as? String {
print(messageBody)
}
}
}

另一件奇怪的事情是,我似乎无法创建一个简单的 WKWebView 应用程序来加载页面并显示它。这些都只是简单的测试,我的主应用程序能够使用 AlamoFire/loadHTMLString() 来加载/显示网页,以显示页面,我只是无法注入(inject)所需的 JS。

我在转换中所做的一切都非常简单,除了 userContentController 的分配外几乎不需要任何更改 - 所以也许这就是问题所在?这个例子以他的原始样本为原型(prototype)在 iOS 中工作得很好。 https://github.com/rckim77/WKWebViewDemoApp/blob/master/WKWebViewDemoApp/ViewController.swift

我猜这里肯定缺少一些非常简单的东西。任何帮助将不胜感激!

最佳答案

下面是我如何在 Mac 上设置我的 WebView 尝试这样的事情

import Cocoa
import WebKit

class ViewController: NSViewController {
@IBOutlet weak var webView: WKWebView!

override func viewDidLoad() {
super.viewDidLoad()

let javascript = """
function printStatement() {
try {
window.webkit.messageHandlers
.callbackHandler.postMessage({'payload': 'Hello World!'})
} catch(err) {
console.log('The native context does yet exist')
}
}
"""

let script = WKUserScript(
source: javascript,
injectionTime: WKUserScriptInjectionTime.atDocumentEnd,
forMainFrameOnly: true
)

webView.configuration.userContentController.add(
name: "callbackHandler"
)
webView.configuration.userContentController
.addUserScript(script)

webView.navigationDelegate = self

let html = """
<div onClick='javascript:printStatement()'>Print Statement</div>
"""

webView.loadHTMLString(html, nil)
}

}

extension ViewController: WKScriptMessageHandler {
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {

if(message.name == "callbackHandler") {
guard let body = message.body as? [String: Any] else {
print("could not convert message body to dictionary: \(message.body)")
return
}

guard let payload = body["payload"] as? String else {
print("Could not locate payload param in callback request")
return
}

print(payload)
}
}

}

如果不让我知道,希望这能回答您的问题并且有效,我会尽力解决!

关于swift - 无法在 Swift/Cocoa/NextStep 中将 JS 注入(inject) WKWebView/将 WKWebView 中网页上的用户选择推送到 Swift/Cocoa,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54751886/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com