55 lines
2.1 KiB
TypeScript
55 lines
2.1 KiB
TypeScript
import { contextBridge, ipcRenderer } from 'electron'
|
|
import { electronAPI } from '@electron-toolkit/preload'
|
|
|
|
const argv = process.argv
|
|
const isAppWindow = argv.includes('--app-window')
|
|
const appName = decodeURIComponent(
|
|
argv.find((a) => a.startsWith('--app-name='))?.slice('--app-name='.length) ?? ''
|
|
)
|
|
const appUrl = decodeURIComponent(
|
|
argv.find((a) => a.startsWith('--app-url='))?.slice('--app-url='.length) ?? ''
|
|
)
|
|
|
|
const appWindowMeta = { isAppWindow, appName, url: appUrl }
|
|
const launcherTailscale = {
|
|
status: () => ipcRenderer.invoke('launcher-tailscale-status'),
|
|
install: () => ipcRenderer.invoke('launcher-tailscale-install'),
|
|
login: (authKey: string, loginServer: string, hostname?: string, forceReset?: boolean) =>
|
|
ipcRenderer.invoke('launcher-tailscale-login', authKey, loginServer, hostname, forceReset),
|
|
logout: () => ipcRenderer.invoke('launcher-tailscale-logout'),
|
|
}
|
|
const launcherSshKey = {
|
|
ensure: () => ipcRenderer.invoke('launcher-ssh-key-ensure'),
|
|
}
|
|
const launcherAuth = {
|
|
startGoogleLogin: (url: string) => ipcRenderer.invoke('start-google-login', url),
|
|
onGoogleOAuthCallback: (callback: (payload: { loginToken?: string; error?: string }) => void) => {
|
|
const listener = (_event: Electron.IpcRendererEvent, payload: { loginToken?: string; error?: string }) => callback(payload)
|
|
ipcRenderer.on('google-oauth-callback', listener)
|
|
return () => ipcRenderer.removeListener('google-oauth-callback', listener)
|
|
},
|
|
}
|
|
|
|
if (process.contextIsolated) {
|
|
try {
|
|
contextBridge.exposeInMainWorld('electron', electronAPI)
|
|
contextBridge.exposeInMainWorld('appWindowMeta', appWindowMeta)
|
|
contextBridge.exposeInMainWorld('launcherTailscale', launcherTailscale)
|
|
contextBridge.exposeInMainWorld('launcherSshKey', launcherSshKey)
|
|
contextBridge.exposeInMainWorld('launcherAuth', launcherAuth)
|
|
} catch (error) {
|
|
console.error(error)
|
|
}
|
|
} else {
|
|
// @ts-ignore
|
|
window.electron = electronAPI
|
|
// @ts-ignore
|
|
window.appWindowMeta = appWindowMeta
|
|
// @ts-ignore
|
|
window.launcherTailscale = launcherTailscale
|
|
// @ts-ignore
|
|
window.launcherSshKey = launcherSshKey
|
|
// @ts-ignore
|
|
window.launcherAuth = launcherAuth
|
|
}
|