zserge / lorca HTML提交即可使用

github.com/zserge/lorca library allows you to bind go funcs to javascript through chrome's dev protocol. With this, you can pass arguments into a go func from the browser.

I'm trying to submit HTML directly to a go func instead of using an embedded http server due to development requirements. (Please don't go off topic & ask why not use a server.)

Here's an example of what I can do:

var inputform string = `
<html>
    <body>
        <form action="/action_page.php">
            <input type="text" name="userinput">
            <input type="submit" onclick="golangfunc(userinput.value)">
        </form>
    </body>
</html>
`

func main(){
    ui, err := lorca.New("data:text/html,"+url.PathEscape(inputform), "", 480, 320)
    ui.Bind("golangfunc", golangfunc)
    defer ui.Close()
    <-ui.Done()
}

func golangfunc(input string){
    fmt.Println(input)
}

I have an arbitrary number of HTML input fields, and so I'd like to pass an HTML form instead of a single input value, but not sure how to do this.

github.com/zserge/lorca supported JS function from Go. Get HTML Form Elements using ui.eval.

package main

import (
    "fmt"
    "github.com/zserge/lorca"
    "net/url"
)

var inputform string = `
<html>
    <body>
        <form action="/action_page.php">
            <input type="text" name="username" id="username">
            <input type="text" name="address" id="address">
            <input type="submit" onclick="golangfunc()">
        </form>
    </body>
</html>
`

func main(){
    ui, _ := lorca.New("data:text/html,"+url.PathEscape(inputform), "", 480, 320)
    ui.Bind("golangfunc", func() {
        username := ui.Eval(`document.getElementById('username').value`)
        address := ui.Eval(`document.getElementById('address').value`)

        fmt.Println(username, address)
    })
    defer ui.Close()
    <-ui.Done()
}