html - zserge/lorca HTML 提交到 go func

标签 html go

github.com/zserge/lorca 库允许您通过 chrome 的开发协议(protocol)将 go funcs 绑定(bind)到 javascript。有了这个,您可以将参数从浏览器传递到 go func。

由于开发需要,我正在尝试将 HTML 直接提交到 go func,而不是使用嵌入式 http 服务器。 (请不要跑题问为什么不使用服务器。)

这是我可以做的一个例子:

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)
}

我有任意数量的 HTML 输入字段,所以我想传递 HTML 表单而不是单个输入值,但不确定如何执行此操作。

最佳答案

github.com/zserge/lorca 支持 Go 的 JS 函数。使用 ui.eval 获取 HTML 表单元素。

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()
}

关于html - zserge/lorca HTML 提交到 go func,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55506212/

相关文章:

mysql - Golang 加入数组接口(interface)

postgresql - 戈朗 : gorm use Find(&model) for non gorm migrate table

go - VS Code,如何显示堆栈跟踪

javascript - 如何从不同的 html/php 文件记录.getElementById().innerHTML

c# - 服务器发送的事件如何与 ASP.NET MVC 一起工作?

javascript - 兑换金额错误

python - Flask 不加载带有查询字符串的 css 文件

javascript - Dojo Toolkit 中的代码重用

mysql - 如何使用golang ping远程mysql

sqlite - 有没有办法查看将参数应用于 sqlite 中的查询的结果?