Skip to content

Build your personal knowledge base with Trilium Notes

feat(electron): basic preload script

Elian Dorand419e56c56b7
diff --git a/apps/client/src/services/utils.ts b/apps/client/src/services/utils.ts
index e6cbdc3986..a6e6fc279c 100644
--- a/apps/client/src/services/utils.ts
+++ b/apps/client/src/services/utils.ts
@@ -132,7 +132,7 @@ function now() {
* Returns `true` if the client is currently running under Electron, or `false` if running in a web browser.
*/
export function isElectron() {
- return !!(window && window.process && window.process.type);
+ return "electronApi" in window;
}
export const isStandalone = window.glob.isStandalone;
diff --git a/apps/server/src/assets/preload.js b/apps/server/src/assets/preload.js
new file mode 100644
index 0000000000..de36441491
--- /dev/null
+++ b/apps/server/src/assets/preload.js
@@ -0,0 +1,3 @@
+const { contextBridge } = require("electron");
+
+contextBridge.exposeInMainWorld("electronApi", {});
diff --git a/apps/server/src/services/window.ts b/apps/server/src/services/window.ts
index 35af280545..7db17b74e1 100644
--- a/apps/server/src/services/window.ts
+++ b/apps/server/src/services/window.ts
@@ -13,6 +13,8 @@ import { RESOURCE_DIR } from "./resource_dir.js";
import sqlInit from "./sql_init.js";
import { isDev, isMac, isWindows } from "./utils.js";
+const PRELOAD_SCRIPT = path.resolve(RESOURCE_DIR, "preload.js");
+
// In dev mode, disable Chromium's HTTP cache so stale assets cached from a
// previous production run (which served `max-age: 1y` headers) don't shadow
// freshly built dev output. Must be set before the app's `ready` event.
@@ -57,6 +59,7 @@ async function createExtraWindow(extraWindowHash: string) {
webPreferences: {
nodeIntegration: false,
contextIsolation: true,
+ preload: PRELOAD_SCRIPT,
spellcheck: spellcheckEnabled,
webviewTag: true
},
@@ -120,6 +123,7 @@ async function createMainWindow(app: App) {
webPreferences: {
nodeIntegration: false,
contextIsolation: true,
+ preload: PRELOAD_SCRIPT,
spellcheck: spellcheckEnabled,
webviewTag: true
},
@@ -243,7 +247,8 @@ async function createSetupWindow() {
...(isMac && { transparent: true, visualEffectState: "active" as const, vibrancy: "under-window" as const, titleBarStyle: "hiddenInset" as const }),
webPreferences: {
nodeIntegration: false,
- contextIsolation: true
+ contextIsolation: true,
+ preload: PRELOAD_SCRIPT
}
});
setupWindow.removeMenu();