[v3 windows] Add Webview2NavigationCompleted event. Support CSS + JS injection

This commit is contained in:
Lea Anthony 2023-06-04 19:01:53 +10:00
commit 1a12890556
No known key found for this signature in database
GPG key ID: 33DAF7BB90A58405
5 changed files with 37 additions and 18 deletions

View file

@ -19,7 +19,7 @@ require (
github.com/pterm/pterm v0.12.51
github.com/samber/lo v1.37.0
github.com/tc-hib/winres v0.1.6
github.com/wailsapp/go-webview2 v1.0.1
github.com/wailsapp/go-webview2 v1.0.2-0.20230604075323-d593c659ca7b
github.com/wailsapp/wails/v2 v2.3.2-0.20230117193915-45c3a501d9e6
golang.org/x/sys v0.8.0
modernc.org/sqlite v1.21.0

View file

@ -146,6 +146,8 @@ github.com/tc-hib/winres v0.1.6 h1:qgsYHze+BxQPEYilxIz/KCQGaClvI2+yLBAZs+3+0B8=
github.com/tc-hib/winres v0.1.6/go.mod h1:pe6dOR40VOrGz8PkzreVKNvEKnlE8t4yR8A8naL+t7A=
github.com/wailsapp/go-webview2 v1.0.1 h1:dEJIeEApW/MhO2tTMISZBFZPuW7kwrFA1NtgFB1z1II=
github.com/wailsapp/go-webview2 v1.0.1/go.mod h1:Uk2BePfCRzttBBjFrBmqKGJd41P6QIHeV9kTgIeOZNo=
github.com/wailsapp/go-webview2 v1.0.2-0.20230604075323-d593c659ca7b h1:cztK9x+ikg6nFscy5c8NgtfIXv/d0ESdENy9+JkE8i4=
github.com/wailsapp/go-webview2 v1.0.2-0.20230604075323-d593c659ca7b/go.mod h1:Uk2BePfCRzttBBjFrBmqKGJd41P6QIHeV9kTgIeOZNo=
github.com/wailsapp/mimetype v1.4.1 h1:pQN9ycO7uo4vsUUuPeHEYoUkLVkaRntMnHJxVwYhwHs=
github.com/wailsapp/mimetype v1.4.1/go.mod h1:9aV5k31bBOv5z6u+QP8TltzvNGJPmNJD4XlAL3U+j3o=
github.com/xo/terminfo v0.0.0-20210125001918-ca9a967f8778 h1:QldyIu/L63oPpyvQmHgvgickp1Yw510KJOqX7H24mg8=

View file

@ -374,8 +374,8 @@ func (w *windowsWebviewWindow) setPosition(x int, y int) {
// on is used to indicate that a particular event should be listened for
func (w *windowsWebviewWindow) on(eventID uint) {
//TODO implement me
panic("implement me")
// We don't need to worry about this in Windows as we do not need
// to optimise cgo calls
}
func (w *windowsWebviewWindow) minimise() {
@ -1192,13 +1192,27 @@ func (w *windowsWebviewWindow) setupChromium() {
chromium.SetGlobalPermission(edge.CoreWebView2PermissionStateAllow)
chromium.AddWebResourceRequestedFilter("*", edge.COREWEBVIEW2_WEB_RESOURCE_CONTEXT_ALL)
chromium.Navigate("http://wails.localhost")
if w.parent.options.HTML != "" {
var script string
if w.parent.options.JS != "" {
script = w.parent.options.JS
}
if w.parent.options.CSS != "" {
script += fmt.Sprintf("; addEventListener(\"DOMContentLoaded\", (event) => { document.head.appendChild(document.createElement('style')).innerHTML=\"%s\"; });", strings.ReplaceAll(w.parent.options.CSS, `"`, `\"`))
}
chromium.Init(script)
chromium.NavigateToString(w.parent.options.HTML)
} else {
chromium.Navigate("http://wails.localhost")
}
}
func (w *windowsWebviewWindow) navigationCompleted(sender *edge.ICoreWebView2, args *edge.ICoreWebView2NavigationCompletedEventArgs) {
// TODO: DomReady Event
// Emit DomReady Event
windowEvents <- &WindowEvent{EventID: uint(events.Windows.WebViewNavigationCompleted), WindowID: w.parent.id}
// Todo: Resize hacks
/*

View file

@ -15,7 +15,7 @@ type commonEvents struct {
func newCommonEvents() commonEvents {
return commonEvents{
ApplicationStarted: 1152,
ApplicationStarted: 1153,
}
}
@ -276,21 +276,23 @@ func newMacEvents() macEvents {
var Windows = newWindowsEvents()
type windowsEvents struct {
SystemThemeChanged ApplicationEventType
APMPowerStatusChange ApplicationEventType
APMSuspend ApplicationEventType
APMResumeAutomatic ApplicationEventType
APMResumeSuspend ApplicationEventType
APMPowerSettingChange ApplicationEventType
SystemThemeChanged ApplicationEventType
APMPowerStatusChange ApplicationEventType
APMSuspend ApplicationEventType
APMResumeAutomatic ApplicationEventType
APMResumeSuspend ApplicationEventType
APMPowerSettingChange ApplicationEventType
WebViewNavigationCompleted WindowEventType
}
func newWindowsEvents() windowsEvents {
return windowsEvents{
SystemThemeChanged: 1146,
APMPowerStatusChange: 1147,
APMSuspend: 1148,
APMResumeAutomatic: 1149,
APMResumeSuspend: 1150,
APMPowerSettingChange: 1151,
SystemThemeChanged: 1146,
APMPowerStatusChange: 1147,
APMSuspend: 1148,
APMResumeAutomatic: 1149,
APMResumeSuspend: 1150,
APMPowerSettingChange: 1151,
WebViewNavigationCompleted: 1152,
}
}

View file

@ -126,4 +126,5 @@ windows:APMSuspend
windows:APMResumeAutomatic
windows:APMResumeSuspend
windows:APMPowerSettingChange
windows:WebViewNavigationCompleted
common:ApplicationStarted