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 to Pass a Callback Function When Calling ExecuteScript? #14

Open
lanyeeee opened this issue Aug 15, 2024 · 1 comment
Open

How to Pass a Callback Function When Calling ExecuteScript? #14

lanyeeee opened this issue Aug 15, 2024 · 1 comment
Labels
enhancement New feature or request question Further information is requested

Comments

@lanyeeee
Copy link
Contributor

lanyeeee commented Aug 15, 2024

Is there any way to pass a callback function when calling ExecuteScript?

For example, consider the following code snippet.
This code, of course, will panic during runtime.
Could you please provide the correct way to do this?

callback := func(errorCode uintptr, resultObjectAsJson string) {
    // print error and result
}
_, _, err = e.webview.vtbl.ExecuteScript.Call(
    uintptr(unsafe.Pointer(e.webview)),
    uintptr(unsafe.Pointer(_script)),
    uintptr(unsafe.Pointer(&callback)),
)

In C++, similar code looks like this:

webview->ExecuteScript(script.c_str(), Callback<ICoreWebView2ExecuteScriptCompletedHandler>([](HRESULT errorCode, LPCWSTR result) -> HRESULT {
    // print error and result
    return S_OK;
    }).Get());

However, I found that go-webview2 cannot even create ICoreWebView2ExecuteScriptCompletedHandler.
Here's the complete code:

package main

import (
    "fmt"
    "github.com/wailsapp/go-webview2/pkg/webview2"
)

func main() {
    var handler webview2.ICoreWebView2ExecuteScriptCompletedHandler
    fmt.Printf("%+v\n", handler)
}

panic massage:

panic: compileCallback: argument size is larger than uintptr

goroutine 1 [running]:
syscall.compileCallback({0x6163a0?, 0x64ae70?}, 0xe0?)
	C:/Program Files/Go/src/runtime/syscall_windows.go:280 +0xca
syscall.NewCallback(...)
	C:/Program Files/Go/src/syscall/syscall_windows.go:214
golang.org/x/sys/windows.NewCallback(...)
	C:/Users/lanye/go/pkg/mod/golang.org/x/[email protected]/windows/syscall_windows.go:140
github.com/wailsapp/go-webview2/pkg/webview2.NewComProc(...)
	C:/Users/lanye/go/pkg/mod/github.com/wailsapp/[email protected]/pkg/webview2/com.go:17
github.com/wailsapp/go-webview2/pkg/webview2.init()
	C:/Users/lanye/go/pkg/mod/github.com/wailsapp/[email protected]/pkg/webview2/ICoreWebView2AddScriptToExecuteOnDocumentCreatedCompletedHandler.go:51 +0x113

The reason seems to be the id parameter of the function in the following code is of type string, and the error disappeared after I replaced it with uintptr, however, the similar situation is common in go-webview2.

func ICoreWebView2AddScriptToExecuteOnDocumentCreatedCompletedHandlerInvoke(this *ICoreWebView2AddScriptToExecuteOnDocumentCreatedCompletedHandler, errorCode uintptr, id string) uintptr {
return this.impl.AddScriptToExecuteOnDocumentCreatedCompleted(errorCode, id)
}
type ICoreWebView2AddScriptToExecuteOnDocumentCreatedCompletedHandlerImpl interface {
IUnknownImpl
AddScriptToExecuteOnDocumentCreatedCompleted(errorCode uintptr, id string) uintptr
}

In conclusion, I would like to know if there is any way to pass a callback function when calling ExecuteScript.

@leaanthony
Copy link
Member

There is this method but it isn't supported just yet. Would happily accept a PR for it though...

@leaanthony leaanthony added enhancement New feature or request question Further information is requested labels Nov 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants