Post

Replies

Boosts

Views

Activity

Reply to WKWebview paste with keyboard paste text twice.
I tried something like @kingbri's JavaScript hack but the fact the event is only received once when invoked from the menu or other-than-Ctrl-V but twice for Ctrl-V caused problems. I ended up doing the equivalent on the Swift side, which is to override the UIResponderStandardEditActions paste action in my subclass of WKWebView and then avoid calling evaluateJavaScript if there is already a call in progress. When the double paste happens, the 2nd one is bypassed, but when there is only one (or if Apple fixes the bug), only the one is processed. This is what the code would look like if you only want to paste text from the clipboard... var pastedAsync = false // A property in the WKWebView subclass public override func paste(_ sender: Any?) { guard !pastedAsync, let text = UIPasteboard.general.text else { return } pastedAsync = true evaluateJavaScript("YourJSPasteFunction('\(text)')") { result, error in self.pastedAsync = false         // handle the result or error     } }
Mar ’22