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/