webview / webview

Tiny cross-platform webview library for C/C++. Uses WebKit (GTK/Cocoa) and Edge WebView2 (Windows).

Home Page:https://webview.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

how Inject JavaScript code?

hktalent opened this issue · comments

// golang code 
type JData struct {
	Data string `json:"data"`
	Name string `json:"name"`
}

func SaveMp4(s string) {
	// save mp4
}

....

w := webview.New(debug)
// Inject JavaScript code
	w.Eval(`
        window.goFunctions = {
            SaveMp4: function(s) {
                webview.ipc.invoke("SaveMp4", [s], function(result) {
                    console.log("SaveMp4 from Golang:", result);
                });
            }
        };
    `)

	// Define Golang function to handle JavaScript invocation
	w.SetIPCHandler(func(data interface{}, reply func(interface{})) {
		if msg, ok := data.([]interface{}); ok {
			if len(msg) >= 2 && msg[0] == "SaveMp4" {
				if name, ok := msg[1].(string); ok {
					result := SaveMp4(name)
					reply(result)
				}
			}
		}
	})