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

Example on how to postmessage or send message to AHK from the webview? #2

Closed
jonseppanen opened this issue Mar 11, 2022 · 2 comments
Closed

Comments

@jonseppanen
Copy link

I cant seem to get the postmessage function to work right.

Thanks

@thqby
Copy link
Owner

thqby commented Apr 22, 2022

#Include <WebView2\WebView2>

g := Gui()
g.Show('w600 h500')
wv := WebView2.create(g.Hwnd)
wv.CoreWebView2.add_WebMessageReceived(WebView2.Handler(WebMessageReceivedEventHandler))
wv.CoreWebView2.AddScriptToExecuteOnDocumentCreated('window.chrome.webview.addEventListener("message", event => alert(event.data));window.chrome.webview.postMessage(window.document.URL);', 0)
wv.CoreWebView2.Navigate('https://autohotkey.com')

WebMessageReceivedEventHandler(handler, ICoreWebView2, WebMessageReceivedEventArgs) {
	args := WebView2.WebMessageReceivedEventArgs(WebMessageReceivedEventArgs)
	msg := args.TryGetWebMessageAsString()
	WebView2.Core(ICoreWebView2).PostWebMessageAsString(msg)
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants