golang+mswebview2获取cookie

webview_go获取cookie

golang+webview获取cookies

package main
 
import (
    "fmt"
    "github.com/zserge/webview"
)
 
func main() {
    // 创建Web视图对象
    w := webview.New(true) // true表示以无头模式运行(不显示界面)
    defer w.Destroy()     // 程序退出时关闭WebView
 
    // 设置页面加载完成后的处理函数
    w.SetTitle("Get Cookie Example")
    w.Navigate("https://example.com")
    w.Bind("getCookies", func() string {
        cookies := make([]string, len(w.Eval(`document.cookie`).String()))
        for i, cookie := range strings.Split(w.Eval(`document.cookie`).String(), "; ") {
            if parts := strings.Split(cookie, "="); len(parts) == 2 {
                cookies[i] = fmt.Sprintf("%s=%s", parts[0], parts[1])
            } else if len(parts) > 2 {
                cookies[i] = fmt.Sprintf("%s=%s", parts[0], strings.Join(parts[1:], "="))
            }
        }
        return strings.Join(cookies, "\n")
    })
 
    // 等待WebView加载完成并触发事件
    select {}
}
### 集成Golang后端与Vue前端的方法 #### 服务器配置 为了使Golang作为API服务器支持Vue.js单页应用程序(SPA),需设置Golang web框架(如Gin)来处理静态文件并提供API路由。对于SPA而言,所有未知路径应重定向到`index.html`以便前端路由器接管[^2]。 ```go package main import ( "net/http" "github.com/gin-gonic/gin" ) func setupRouter() *gin.Engine { router := gin.Default() // Serve frontend static files staticFiles := http.Dir("./dist") // Vue build output directory fileServer := http.FileServer(staticFiles) router.StaticFS("/static", staticFiles) apiGroup := router.Group("/api") { apiGroup.GET("/example", func(c *gin.Context) { c.JSON(http.StatusOK, gin.H{"message": "Hello from Go backend"}) }) } // Catch-all route to serve index.html for SPA routing router.NoRoute(func(c *gin.Context) { http.ServeFile(c.Writer, c.Request, "./dist/index.html") }) return router } ``` #### 前端构建 Vue项目应当被编译为生产版本,并放置于Go项目的指定目录下(`./dist`)。这通常通过执行`npm run build`命令完成,在`vue.config.js`中可以自定义输出路径和其他选项[^1]。 #### API交互 前后端通信依赖HTTP请求发送JSON数据。在Vue组件内部利用Axios库发起GET/POST调用来获取或提交资源给Golang RESTful APIs[^3]。 ```javascript const axios = require('axios'); export default { methods: { async fetchData() { try { const response = await axios.get('/api/example'); console.log(response.data); } catch (error) { console.error(error); } }, submitData(data) { axios.post('/api/resource', data).then(/* handle success */).catch(/* handle error */); } } }; ```
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

轻蓝雨

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值