Skip to content

Commit b8dbba5

Browse files
committed
Update preload.ts
1 parent 61e658c commit b8dbba5

File tree

1 file changed

+52
-13
lines changed

1 file changed

+52
-13
lines changed

packages/core/assets/electron/preload.ts

Lines changed: 52 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,54 @@ type PassedDesktopArgs = {
1111
const globalVariableName = '__commoners'
1212
const services = ipcRenderer.sendSync('commoners:services')
1313

14-
const args = process.argv.slice(1).reduce((acc, arg) => {
15-
const match = arg.match(/^--(__.+)=(.+)$/)
16-
if (match) {
17-
acc[match[1]] = match[2]
18-
try {
19-
acc[match[1]] = JSON.parse(acc[match[1]])
20-
} catch {}
14+
// Parse arguments from process.argv (sandbox-compatible approach)
15+
// In sandbox mode, process.argv may be restricted, so we handle gracefully
16+
const args = (() => {
17+
try {
18+
// Try to access process.argv - works in non-sandboxed mode
19+
if (typeof process !== 'undefined' && process.argv) {
20+
return process.argv.slice(1).reduce((acc, arg) => {
21+
const match = arg.match(/^--(__.+)=(.+)$/)
22+
if (match) {
23+
acc[match[1]] = match[2]
24+
try {
25+
acc[match[1]] = JSON.parse(acc[match[1]])
26+
} catch {}
27+
}
28+
return acc
29+
}, {} as Record<string, any>)
30+
}
31+
} catch (e) {
32+
// In sandbox mode, process.argv might not be available
33+
console.warn('process.argv not available in sandbox mode, falling back to empty args')
2134
}
22-
return acc
23-
}, {})
35+
return {} as Record<string, any>
36+
})()
2437

2538
const { __id } = args as PassedDesktopArgs
2639

2740
const __location = ipcRenderer.sendSync(`commoners:location`, __id)
2841

2942
// Update URL search and hash for the current window without reloading
30-
const url = new URL(window.location.href)
31-
for (let [key, value] of Object.entries(__location)) value && (url[key] = value)
32-
window.history.replaceState(null, '', url.toString())
43+
// Defer to ensure window object is fully available
44+
if (typeof window !== 'undefined') {
45+
try {
46+
const url = new URL(window.location.href)
47+
for (let [key, value] of Object.entries(__location)) value && (url[key] = value)
48+
window.history.replaceState(null, '', url.toString())
49+
} catch (e) {
50+
// If window isn't ready yet, defer to DOMContentLoaded
51+
window.addEventListener('DOMContentLoaded', () => {
52+
try {
53+
const url = new URL(window.location.href)
54+
for (let [key, value] of Object.entries(__location)) value && (url[key] = value)
55+
window.history.replaceState(null, '', url.toString())
56+
} catch (err) {
57+
console.warn('Failed to update window location:', err)
58+
}
59+
})
60+
}
61+
}
3362

3463
const TEMP_COMMONERS = {
3564
quit: (message?: string) => ipcRenderer.send('commoners:quit', message),
@@ -83,7 +112,17 @@ for (let id in TEMP_COMMONERS.services) {
83112
}
84113

85114
// Expose ipcRenderer
86-
if (process.contextIsolated) {
115+
// Check for context isolation in a sandbox-compatible way
116+
const isContextIsolated = (() => {
117+
try {
118+
return typeof process !== 'undefined' && process.contextIsolated
119+
} catch {
120+
// If process is not available, assume context isolation is enabled (sandbox mode default)
121+
return true
122+
}
123+
})()
124+
125+
if (isContextIsolated) {
87126
try {
88127
contextBridge.exposeInMainWorld(globalVariableName, TEMP_COMMONERS)
89128
} catch (error) {

0 commit comments

Comments
 (0)