65 lines
1.4 KiB
Go
65 lines
1.4 KiB
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"fmt"
|
||
|
"syscall/js"
|
||
|
|
||
|
"go.jolheiser.com/go-spectre"
|
||
|
)
|
||
|
|
||
|
const defaultScoper = "com.lyndir.masterpassword"
|
||
|
|
||
|
var Scoper string
|
||
|
|
||
|
//go:generate go build -ldflags "-X 'main.Scoper=$PWA_SPECTRE_SCOPER'" -o ../assets/spectre.wasm
|
||
|
func main() {
|
||
|
if Scoper == "" {
|
||
|
Scoper = defaultScoper
|
||
|
}
|
||
|
scoper := spectre.SimpleScoper{
|
||
|
Key: Scoper,
|
||
|
}
|
||
|
js.Global().Set("spectre", spectreWrapper(scoper))
|
||
|
js.Global().Set("defaultScoper", Scoper == defaultScoper)
|
||
|
fmt.Println("Spectre Init")
|
||
|
<-make(chan struct{})
|
||
|
}
|
||
|
|
||
|
func spectreWrapper(scoper spectre.Scoper) js.Func {
|
||
|
spectreFunc := js.FuncOf(func(this js.Value, args []js.Value) interface{} {
|
||
|
if len(args) != 2 {
|
||
|
return "Invalid number of arguments"
|
||
|
}
|
||
|
|
||
|
s, err := spectre.New(args[0].String(), args[1].String(), spectre.WithScoper(scoper))
|
||
|
if err != nil {
|
||
|
return err.Error()
|
||
|
}
|
||
|
return map[string]interface{}{
|
||
|
// Site(site, counter, scope, template)
|
||
|
"Site": js.FuncOf(func(this js.Value, args []js.Value) interface{} {
|
||
|
if len(args) != 4 {
|
||
|
return "Invalid number of arguments"
|
||
|
}
|
||
|
|
||
|
scope, err := spectre.ParseScope(args[2].String())
|
||
|
if err != nil {
|
||
|
return err.Error()
|
||
|
}
|
||
|
|
||
|
template, err := spectre.ParseTemplate(args[3].String())
|
||
|
if err != nil {
|
||
|
return err.Error()
|
||
|
}
|
||
|
|
||
|
return s.Site(args[0].String(),
|
||
|
spectre.WithCounter(args[1].Int()),
|
||
|
spectre.WithScope(scope),
|
||
|
spectre.WithTemplate(template),
|
||
|
)
|
||
|
}),
|
||
|
}
|
||
|
})
|
||
|
return spectreFunc
|
||
|
}
|