Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions v3/UNRELEASED_CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ After processing, the content will be moved to the main changelog and this file

## Fixed
<!-- Bug fixes -->
- Fix JS/CSS options in WebviewWindowOptions not being executed when using URL navigation (not just HTML)
- Fix SetProcessDpiAwarenessContext "Access is denied" error when DPI awareness is already set via application manifest (#4803)

## Deprecated
Expand Down
14 changes: 13 additions & 1 deletion v3/internal/assetserver/bundledassets/runtime.debug.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion v3/internal/assetserver/bundledassets/runtime.js

Large diffs are not rendered by default.

18 changes: 18 additions & 0 deletions v3/internal/runtime/desktop/@wailsio/runtime/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,21 @@ window._wails.handleDragLeave = handleDragLeave;
window._wails.handleDragOver = handleDragOver;

System.invoke("wails:runtime:ready");

// Load optional window init scripts (fire and forget, matching current async behavior)
// The backend identifies the window from the x-wails-window-id header
fetch('/wails/init.js')
.then(r => r.ok && r.status !== 204 ? r.text() : null)
.then(js => { if (js) eval(js); })
.catch(() => {}); // Silently ignore errors

fetch('/wails/init.css')
.then(r => r.ok && r.status !== 204 ? r.text() : null)
.then(css => {
if (css) {
const style = document.createElement('style');
style.textContent = css;
document.head.appendChild(style);
}
})
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Outdated
.catch(() => {}); // Silently ignore errors
44 changes: 44 additions & 0 deletions v3/pkg/application/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,50 @@
if err != nil {
result.fatal("unable to serve transport.js: %w", err)
}
case "/wails/init.js", "/wails/init.css":
// Serve window-specific init JS/CSS based on x-wails-window-id header
windowIdStr := req.Header.Get(webViewRequestHeaderWindowId)
if windowIdStr == "" {
rw.WriteHeader(http.StatusNoContent)
return
}
windowId, err := strconv.ParseUint(windowIdStr, 10, 64)
if err != nil {
rw.WriteHeader(http.StatusNoContent)
return
}

result.windowsLock.RLock()
window, exists := result.windows[uint(windowId)]
Comment thread Fixed
result.windowsLock.RUnlock()

if !exists {
rw.WriteHeader(http.StatusNoContent)
return
}

webviewWindow, ok := window.(*WebviewWindow)
if !ok {
rw.WriteHeader(http.StatusNoContent)
return
}

rw.Header().Set("Cache-Control", "no-cache")
if path == "/wails/init.js" {
if webviewWindow.options.JS != "" {
rw.Header().Set("Content-Type", "application/javascript")
rw.Write([]byte(webviewWindow.options.JS))
Comment thread
leaanthony marked this conversation as resolved.
Outdated
} else {
rw.WriteHeader(http.StatusNoContent)
}
} else { // init.css
if webviewWindow.options.CSS != "" {
rw.Header().Set("Content-Type", "text/css")
rw.Write([]byte(webviewWindow.options.CSS))
} else {
rw.WriteHeader(http.StatusNoContent)
}
}
default:
next.ServeHTTP(rw, req)
}
Expand Down
8 changes: 1 addition & 7 deletions v3/pkg/application/webview_window_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -1351,15 +1351,9 @@ func (w *macosWebviewWindow) run() {

w.setURL(startURL)

// We need to wait for the HTML to load before we can execute the javascript
// We need to wait for the HTML to load before we can show the window
w.parent.OnWindowEvent(events.Mac.WebViewDidFinishNavigation, func(_ *WindowEvent) {
InvokeAsync(func() {
if options.JS != "" {
w.execJS(options.JS)
}
if options.CSS != "" {
C.windowInjectCSS(w.nsWindow, C.CString(options.CSS))
}
if !options.Hidden {
w.parent.Show()
w.setHasShadow(!options.Mac.DisableShadow)
Expand Down
11 changes: 0 additions & 11 deletions v3/pkg/application/webview_window_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,17 +341,6 @@ func (w *linuxWebviewWindow) run() {
}

w.setURL(startURL)
w.parent.OnWindowEvent(events.Linux.WindowLoadFinished, func(_ *WindowEvent) {
InvokeAsync(func() {
if w.parent.options.JS != "" {
w.execJS(w.parent.options.JS)
}
if w.parent.options.CSS != "" {
js := fmt.Sprintf("(function() { var style = document.createElement('style'); style.appendChild(document.createTextNode('%s')); document.head.appendChild(style); })();", w.parent.options.CSS)
w.execJS(js)
}
})
})

w.parent.RegisterHook(events.Linux.WindowLoadFinished, func(e *WindowEvent) {
// Inject runtime core and EnableFileDrop flag together
Expand Down
13 changes: 0 additions & 13 deletions v3/pkg/application/webview_window_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -2060,19 +2060,6 @@ func (w *windowsWebviewWindow) setupChromium() {
chromium.AddWebResourceRequestedFilter("*", edge.COREWEBVIEW2_WEB_RESOURCE_CONTEXT_ALL)

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, `"`, `\"`),
)
}
if script != "" {
chromium.Init(script)
}
chromium.NavigateToString(w.parent.options.HTML)
} else {
startURL, err := assetserver.GetStartURL(w.parent.options.URL)
Expand Down
Loading