how Inject JavaScript code?
hktalent opened this issue · comments
51pwn commented
// 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)
}
}
}
})