Skip to content

Commit

Permalink
Clean up clipboard lookup a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
Jacalz committed Feb 24, 2025
1 parent b8d3b79 commit 7471bca
Showing 1 changed file with 11 additions and 11 deletions.
22 changes: 11 additions & 11 deletions clipboard_wasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,24 @@ import (
//
// This function may only be called from the main thread.
func GetClipboardString() string {
clipboard := js.Global().Get("navigator").Get("clipboard")
clipboardChan := make(chan js.Value)
text := make(chan string)

clipboard := js.Global().Get("navigator").Get("clipboard")
clipboard.Call("readText").Call("then", js.FuncOf(func(this js.Value, p []js.Value) interface{} {
clipboardContent := p[0]
clipboardChan <- clipboardContent
content := p[0]
if !content.Truthy() {
text <- ""
return nil
}

text <- content.String()
return nil
})).Call("catch", js.FuncOf(func(this js.Value, args []js.Value) interface{} {
clipboardChan <- js.ValueOf(nil)
text <- ""
return nil
}))

result := <-clipboardChan
if !result.Truthy() {
return ""
}

return result.String()
return <-text
}

// SetClipboardString sets the system clipboard to the specified UTF-8 encoded string.
Expand Down

0 comments on commit 7471bca

Please sign in to comment.