Skip to content

Commit 23e89cf

Browse files
fix: improve macOS WebView initialization for JavaScript execution
CHANGES: - Increased WebView size from 1x1 to 100x100 (minimum viable size) - Changed positioning: use setAlphaValue:0 (transparent) instead of offscreen (-10000, -10000) - Improved console.log capture: preserve original console.log and forward to Unity - Prevents breaking JavaScript libraries that depend on console.log RATIONALE: WKWebView may not properly initialize or execute JavaScript with extremely small sizes (1x1). Using 100x100 with alpha=0 ensures the WebView is functional while remaining invisible to the user.
1 parent c16efdb commit 23e89cf

2 files changed

Lines changed: 12 additions & 5 deletions

File tree

Plugins/Mac/Sources/ImmutableWebView.mm

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,13 @@ - (id)initWithUa:(const char *)ua
119119
} \
120120
}; \
121121
window.UnityPostMessage = window.Unity.call; \
122-
function captureLog(msg) { window.webkit.messageHandlers.logHandler.postMessage(msg); } window.console.log = captureLog; \
122+
(function() { \
123+
var originalLog = console.log; \
124+
console.log = function(msg) { \
125+
try { window.webkit.messageHandlers.logHandler.postMessage(String(msg)); } catch(e) {} \
126+
originalLog.apply(console, arguments); \
127+
}; \
128+
})(); \
123129
";
124130

125131
WKUserScript *script
@@ -144,17 +150,18 @@ - (id)initWithUa:(const char *)ua
144150
webView.UIDelegate = self;
145151
webView.navigationDelegate = self;
146152

147-
// CRITICAL: WebView must be in view hierarchy and not hidden to execute JavaScript
148-
// Even though we don't need UI, we add it offscreen to ensure JavaScript runs
153+
// CRITICAL: WebView must be in view hierarchy to execute JavaScript
154+
// Use a reasonable minimum size (100x100) and make it transparent instead of tiny/offscreen
149155
webView.hidden = NO;
150-
webView.frame = CGRectMake(-10000, -10000, 1, 1); // Position offscreen
156+
webView.frame = CGRectMake(0, 0, 100, 100); // Minimum viable size
157+
[webView setAlphaValue:0.0]; // Make completely transparent (invisible but functional)
151158

152159
// Add to main window to ensure JavaScript execution
153160
NSArray *windows = [[NSApplication sharedApplication] windows];
154161
if ([windows count] > 0) {
155162
NSWindow *mainWindow = [windows objectAtIndex:0];
156163
[[mainWindow contentView] addSubview:webView];
157-
NSLog(@"[ImmutableWebView] WebView added to main window for JavaScript execution");
164+
NSLog(@"[ImmutableWebView] WebView (100x100, alpha=0) added to main window for JavaScript execution");
158165
} else {
159166
NSLog(@"[ImmutableWebView] WARNING: No window available, WebView may not execute JavaScript");
160167
}

0 commit comments

Comments
 (0)