Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
feat(llm): add section on electron API in frontend skill
Elian Doran55b0d9ac9fbf
diff --git a/apps/server/src/assets/llm/skills/frontend_scripting.md b/apps/server/src/assets/llm/skills/frontend_scripting.mdindex 493b4446f8..d9be5f107d 100644--- a/apps/server/src/assets/llm/skills/frontend_scripting.md+++ b/apps/server/src/assets/llm/skills/frontend_scripting.md@@ -208,6 +208,82 @@ Available via `getNote()`, `getActiveContextNote()`, `useNoteContext()`, etc.- `note.getOwnedAttributes(type?, name?)` - only owned attributes- `note.hasAttribute(type, name)` - check for attribute+## Electron API (desktop only)++Desktop-only functionality is exposed on `window.electronApi` by the preload script. It is **not** part of the `api` global. The global is `undefined` in the browser/server build and in the standalone (WASM) build, so always guard usage.++```jsx+if (window.electronApi) {+ window.electronApi.window.setZoomFactor(1.2);+}+```++Use optional chaining for one-off calls: `window.electronApi?.window.minimizeWindow()`.++### Groups++| Group | What it covers |+|---|---|+| `window` | Zoom, theme, title bar, full screen, lifecycle, devtools, extra windows, global shortcut + open-in-tab events |+| `clipboard` | `copyImageToClipboard(buffer)` for raw PNG bytes |+| `shell` | `openExternal`, `openPath`, `openFileUrl`, `downloadURL`, `openCustom` — all validated in main process |+| `contextMenu` | Subscribe to right-click events, dispatch cut/copy/paste/insertText |+| `spellcheck` | `addWordToDictionary`, `getAvailableSpellCheckerLanguages` |+| `tray` | `reloadTray()` |+| `printing` | PDF export/preview/save, printer list, print progress events |+| `navigation` | Back/forward history accessors and navigation events |++### Common examples++```js+// Open a URL in the user's default browser+window.electronApi?.shell.openExternal("https://example.com");++// Toggle full screen+const api = window.electronApi;+if (api) api.window.setFullScreen(!api.window.isFullScreen());++// Read & adjust zoom+const zoom = window.electronApi?.window.getZoomFactor() ?? 1;+window.electronApi?.window.setZoomFactor(zoom + 0.1);++// React to global shortcuts (configured in Trilium options)+window.electronApi?.window.onGlobalShortcut((actionName) => {+ console.log("shortcut fired:", actionName);+});++// Copy a PNG to the clipboard+const bytes = new Uint8Array(await blob.arrayBuffer());+window.electronApi?.clipboard.copyImageToClipboard(bytes);+```++### Security notes (shell group)++Every `shell.*` call is validated in the main process and will throw on invalid input — the renderer is treated as untrusted:++- `openExternal(url)`: scheme is allowlisted. `file:`, `data:`, `smb:`, `ldap:`/`ldaps:`, `jar:`, `view-source:`, and Follina-class schemes are blocked.+- `openPath(path)` / `openFileUrl(fileUrl)`: must resolve under the Trilium data dir or tmp dir. Returns an empty string on success, an error message on failure. UNC `file://host/share` URLs are blocked (NTLM-leak prevention).+- `downloadURL(url)`: pinned to the app's own origin — cross-origin downloads are rejected.+- `openCustom(filePath)`: must be a descendant of the tmp dir and the file must exist.++### Cross-build patterns++Because `window.electronApi` is missing outside desktop, write code that degrades:++```jsx+import { showMessage } from "trilium:api";++function openInBrowser(url) {+ if (window.electronApi) {+ window.electronApi.shell.openExternal(url);+ } else {+ window.open(url, "_blank", "noopener,noreferrer");+ }+}+```++Avoid `require("electron")`, `@electron/remote`, and `process` — `nodeIntegration` is disabled and `contextIsolation` is enabled, so they aren't available in the renderer.+## Legacy jQuery widgets (avoid if possible)Only use legacy widgets if you specifically need jQuery or cannot use JSX.