Skip to content

Commit 3799814

Browse files
committed
Expose function to launch app in main world
1 parent 52115f5 commit 3799814

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

preload.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ console.log('preload')
22
const { contextBridge, ipcRenderer } = require('electron')
33
const path = require('path')
44
const shortcuts = require('./backend/shortcuts.js').global
5-
const { emit, platform } = require('process')
5+
const { platform } = require('process')
66
const SerialBridge = require('./backend/serial/serial-bridge.js')
77

88
const Disk = {
@@ -80,6 +80,21 @@ const Window = {
8080
getShortcuts: () => shortcuts
8181
}
8282

83+
/**
84+
* Launches an app using the provided URL scheme (e.g. myapp://). If the app is not installed, it will
85+
* fallback to open the provided fallback URL.
86+
* @param {string} url The URL scheme to use to launch the app
87+
* @param {string} fallbackUrl The URL to open if the app is not installed
88+
*/
89+
async function launchApp(url, fallbackUrl) {
90+
const success = await ipcRenderer.invoke('launch-app', url);
91+
92+
if (!success) {
93+
await ipcRenderer.invoke('open-url', fallbackUrl); // Fallback to open a URL in the default browser
94+
}
95+
}
96+
97+
contextBridge.exposeInMainWorld('launchApp', launchApp)
8398
contextBridge.exposeInMainWorld('BridgeSerial', SerialBridge)
8499
contextBridge.exposeInMainWorld('BridgeDisk', Disk)
85100
contextBridge.exposeInMainWorld('BridgeWindow', Window)

0 commit comments

Comments
 (0)