Skip to content

Commit

Permalink
wails: add execute js on document load
Browse files Browse the repository at this point in the history
  • Loading branch information
freehelpdesk committed Mar 5, 2023
1 parent 9cd2422 commit a6d39ee
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 0 deletions.
6 changes: 6 additions & 0 deletions v2/internal/frontend/desktop/windows/frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -665,6 +665,12 @@ func (f *Frontend) ExecJS(js string) {
})
}

func (f *Frontend) ExecJSOnDocumentLoad(js string) {
f.mainWindow.Invoke(func() {
f.chromium.EvalOnDocumentCreate(js)
})
}

func (f *Frontend) navigationCompleted(sender *edge.ICoreWebView2, args *edge.ICoreWebView2NavigationCompletedEventArgs) {
if f.frontendOptions.OnDomReady != nil {
go f.frontendOptions.OnDomReady(f.ctx)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,21 @@ func (e *Chromium) Eval(script string) {
return
}

func (e *Chromium) EvalOnDocumentCreate(script string) {

_script, err := windows.UTF16PtrFromString(script)
if err != nil {
log.Fatal(err)
}

_, _, _ = e.webview.vtbl.AddScriptToExecuteOnDocumentCreated.Call(
uintptr(unsafe.Pointer(e.webview)),
uintptr(unsafe.Pointer(_script)),
0,
)
return
}

func (e *Chromium) Show() error {
return e.controller.PutIsVisible(true)
}
Expand Down
1 change: 1 addition & 0 deletions v2/internal/frontend/frontend.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ type Frontend interface {
Run(context.Context) error
RunMainLoop()
ExecJS(js string)
ExecJSOnDocumentLoad(js string)
Hide()
Show()
Quit()
Expand Down
6 changes: 6 additions & 0 deletions v2/pkg/runtime/window.go
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,12 @@ func WindowExecJS(ctx context.Context, js string) {
appFrontend.ExecJS(js)
}

// WindowExecJSOnDocumentLoad executes the given Js in the window when it is loaded
func WindowExecJSOnDocumentLoad(ctx context.Context, js string) {
appFrontend := getFrontend(ctx)
appFrontend.ExecJSOnDocumentLoad(js)
}

func WindowSetBackgroundColour(ctx context.Context, R, G, B, A uint8) {
appFrontend := getFrontend(ctx)
col := &options.RGBA{
Expand Down

0 comments on commit a6d39ee

Please sign in to comment.