Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JS implementation of clipboard #48

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions clipboard_js.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package clipboard

import (
"errors"
"sync"
"syscall/js"
)

func readAll() (string, error) {
var wg sync.WaitGroup
wg.Add(1)
var result string
var err error
promise := js.Global().Get("navigator").Get("clipboard").Call("readText").Call("then", js.FuncOf(func(me js.Value, args []js.Value) interface{} {
result = args[0].String()
wg.Done()
return nil
}), js.FuncOf(func(me js.Value, args []js.Value) interface{} {
err = errors.New(args[0].String())
wg.Done()
return nil
}))

if !promise.Truthy() {
return "", errors.New("No promise received by JS")
}

// Wait for promise to resolve
wg.Wait()

// Return value
return result, err
}

func writeAll(text string) error {
var wg sync.WaitGroup
wg.Add(1)
var err error
promise := js.Global().Get("navigator").Get("clipboard").Call("writeText", text).Call("then", js.FuncOf(func(me js.Value, args []js.Value) interface{} {
wg.Done()
return nil
}), js.FuncOf(func(me js.Value, args []js.Value) interface{} {
err = errors.New(args[0].String())
wg.Done()
return nil
}))

if !promise.Truthy() {
return errors.New("No promise received by JS")
}

// Wait for promise to resolve
wg.Wait()

// Return value
return err
}