Skip to content
Open
Show file tree
Hide file tree
Changes from 3 commits
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)

## Deprecated
<!-- Soon-to-be removed features -->
Expand Down
13 changes: 13 additions & 0 deletions v3/internal/assetserver/bundledassets/runtime.debug.js

Large diffs are not rendered by default.

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

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


/**
* Loads a script from the given URL if it exists.
* Uses HEAD request to check existence, then injects a script tag.
Expand All @@ -99,3 +100,5 @@ export function loadOptionalScript(url: string): Promise<void> {

// Load custom.js if available (used by server mode for WebSocket events, etc.)
loadOptionalScript('/wails/custom.js');
loadOptionalScript('/wails/init.js');
loadOptionalScript('/wails/init.css');
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 @@ func New(appOptions Options) *App {
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.Atoi(windowIdStr)
if err != nil || windowId < 0 {
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 @@ -1359,15 +1359,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