登录
首页 >  Golang >  Go问答

zserge/lorca HTML 提交到 go func

来源:stackoverflow

时间:2024-04-05 14:45:34 205浏览 收藏

IT行业相对于一般传统行业,发展更新速度更快,一旦停止了学习,很快就会被行业所淘汰。所以我们需要踏踏实实的不断学习,精进自己的技术,尤其是初学者。今天golang学习网给大家整理了《zserge/lorca HTML 提交到 go func》,聊聊,我们一起来看看吧!

问题内容

github.com/zserge/lorca 库允许您通过 chrome 的 dev 协议将 go funcs 绑定到 javascript。这样,您就可以从浏览器将参数传递到 go func 中。

由于开发要求,我尝试将 html 直接提交到 go func,而不是使用嵌入式 http 服务器。 (请不要偏离主题并询问为什么不使用服务器。)

以下是我可以执行的操作的示例:

var inputform string = `
<html>
    <body>
        <form action="/action_page.php">
            &lt;input type=&quot;text&quot; name=&quot;userinput&quot;&gt;
            &lt;input type=&quot;submit&quot; onclick=&quot;golangfunc(userinput.value)&quot;&gt;
        </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">
            &lt;input type=&quot;text&quot; name=&quot;username&quot; id=&quot;username&quot;&gt;
            &lt;input type=&quot;text&quot; name=&quot;address&quot; id=&quot;address&quot;&gt;
            &lt;input type=&quot;submit&quot; onclick=&quot;golangfunc()&quot;&gt;
        </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()
}

今天关于《zserge/lorca HTML 提交到 go func》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于的内容请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>