Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

how Inject JavaScript code? #1107

Open
hktalent opened this issue Apr 25, 2024 · 0 comments
Open

how Inject JavaScript code? #1107

hktalent opened this issue Apr 25, 2024 · 0 comments

Comments

@hktalent
Copy link

hktalent commented Apr 25, 2024

// 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)
				}
			}
		}
	})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

1 participant