Skip to content

Build your personal knowledge base with Trilium Notes

fix(scripting): show the friendly toast when a backend note is executed directly

Elian Doranb518f324398e
diff --git a/apps/client/src/components/entrypoints.ts b/apps/client/src/components/entrypoints.ts
index 8db24eb5c6..9c2f859b8e 100644
--- a/apps/client/src/components/entrypoints.ts
+++ b/apps/client/src/components/entrypoints.ts
@@ -3,9 +3,11 @@ import { CreateChildrenResponse, SqlExecuteResponse } from "@triliumnext/commons
import bundleService from "../services/bundle.js";
import dialog from "../services/dialog.js";
import dateNoteService from "../services/date_notes.js";
+import { showBackendScriptingDisabledToast } from "../services/frontend_script_api.js";
import froca from "../services/froca.js";
import { t } from "../services/i18n.js";
import linkService from "../services/link.js";
+import options from "../services/options.js";
import protectedSessionHolder from "../services/protected_session_holder.js";
import server from "../services/server.js";
import toastService from "../services/toast.js";
@@ -182,7 +184,18 @@ export default class Entrypoints extends Component {
return;
}
} else if (note.mime.endsWith("env=backend")) {
- await server.post(`script/run/${note.noteId}`);
+ if (!options.is("backendScriptingEnabled")) {
+ // Show the same friendly toast as the frontend runOnBackend path, rather than letting
+ // the /script/run request 500 and surface as an "Uncaught error" via the global handler.
+ showBackendScriptingDisabledToast(note.noteId);
+ return;
+ }
+ try {
+ await server.post(`script/run/${note.noteId}`);
+ } catch {
+ // server.js already reported the error; don't fall through to "Note executed".
+ return;
+ }
} else if (note.mime === "text/x-sqlite;schema=trilium") {
const response = await server.post<SqlExecuteResponse>(`sql/execute/${note.noteId}`);
await appContext.triggerEvent("sqlQueryResults", { ntxId, response });
diff --git a/apps/client/src/services/frontend_script_api.ts b/apps/client/src/services/frontend_script_api.ts
index a76082f78c..ea3eb27caf 100644
--- a/apps/client/src/services/frontend_script_api.ts
+++ b/apps/client/src/services/frontend_script_api.ts
@@ -834,11 +834,16 @@ export class BackendScriptingDisabledError extends Error {
}
}
-// The scripts that attempted a backend call while it was disabled, accumulated so the single
-// deduplicated toast can list all of them as reference links. Cleared when the toast is removed.
+// The notes that attempted to run backend code while it was disabled, accumulated so the single
+// deduplicated toast can list them all as reference links. Cleared when the toast is removed.
const backendScriptingAttempts = new Set<string>();
-function showBackendScriptingDisabledToast(noteId: string) {
+/**
+ * Shows the single deduplicated "backend scripting is disabled" toast, adding the given note to the
+ * list of scripts that tried to run backend code. Exported so any backend-execution entry point can
+ * reuse it — a frontend `runOnBackend()` call, or executing a backend code note directly.
+ */
+export function showBackendScriptingDisabledToast(noteId: string) {
backendScriptingAttempts.add(noteId);
toastService.showPersistent({
id: "backend-scripting-disabled",