This is still a problem in the latest version of macOS monterey (v12.2). I created a JS solution which overrides the default pasting logic for macOS. Add this JS code as a userscript into your WKWebView user content controller.
Here's the gist link for my code.
I have also attached the code down below:
const inputs = document.querySelectorAll("input[type=text]")
let alreadyPasted = false
for (const input of inputs) {
input.addEventListener("paste", (event) => {
event.preventDefault()
// Don't call paste event two times in a single paste command
if (alreadyPasted) {
alreadyPasted = false
return
}
const paste = (event.clipboardData || window.clipboardData).getData("text")
const beginningString =
input.value.substring(0, input.selectionStart) + paste
input.value =
beginningString +
input.value.substring(input.selectionEnd, input.value.length)
alreadyPasted = true
input.setSelectionRange(beginningString.length, beginningString.length)
input.scrollLeft = input.scrollWidth
})
}
Here's a code example to reproduce the double paste bug with MacCatalyst (it doesn't matter if I use SwiftUI, I just like it more)
struct ContentView: View {
var body: some View {
WebView(url: URL(string: "https://google.com")!)
}
}
struct WebView: UIViewRepresentable {
let url: URL
func makeUIView(context: Context) -> WKWebView {
let webView = WKWebView()
return webView
}
func updateUIView(_ uiView: WKWebView, context: Context) {
}
}
I hope this helps someone out there :)
Topic:
App & System Services
SubTopic:
Core OS
Tags: