From 3410f0f5bc47a8963cbb835759b415aa8aacd219 Mon Sep 17 00:00:00 2001 From: Elian Doran Date: Sat, 11 Apr 2026 11:01:04 +0300 Subject: [PATCH] feat(script): warn if user is trying to run the script in a wrong environment (closes #342) --- .../src/assets/translations/en/server.json | 3 ++ apps/server/src/services/script.ts | 28 +++++++++++++++++++ 2 files changed, 31 insertions(+) diff --git a/apps/server/src/assets/translations/en/server.json b/apps/server/src/assets/translations/en/server.json index 87e9f0bbcc..e1774fd589 100644 --- a/apps/server/src/assets/translations/en/server.json +++ b/apps/server/src/assets/translations/en/server.json @@ -446,6 +446,9 @@ "desktop": { "instance_already_running": "There's already an instance running, focusing that instance instead." }, + "script": { + "wrong-environment": "Cannot execute note \"{{- noteTitle}}\" ({{- noteId}}). This is a {{- actualEnv}} script, but execution was attempted in the {{- expectedEnv}}." + }, "search": { "error": { "in-context": "Error in {{- context}}: {{- message}}", diff --git a/apps/server/src/services/script.ts b/apps/server/src/services/script.ts index 97dd99898a..b914631e71 100644 --- a/apps/server/src/services/script.ts +++ b/apps/server/src/services/script.ts @@ -1,3 +1,4 @@ +import { t } from "i18next"; import { transform } from "sucrase"; import becca from "../becca/becca.js"; @@ -6,6 +7,7 @@ import type { ApiParams } from "./backend_script_api_interface.js"; import cls from "./cls.js"; import log from "./log.js"; import ScriptContext from "./script_context.js"; +import ws from "./ws.js"; export interface Bundle { note?: BNote; @@ -22,6 +24,18 @@ function executeNote(note: BNote, apiParams: ApiParams) { if (!note.isJavaScript() || note.getScriptEnv() !== "backend" || !note.isContentAvailable()) { log.info(`Cannot execute note ${note.noteId} "${note.title}", note must be of type "Code: JS backend"`); + // Warn the user if they're trying to run a frontend script in the backend + const actualEnv = note.getScriptEnv(); + if (note.isJavaScript() && actualEnv === "frontend") { + const message = t("script.wrong-environment", { + noteTitle: note.title, + noteId: note.noteId, + actualEnv: "frontend", + expectedEnv: "backend" + }); + ws.sendMessageToAllClients({ type: "toast", message }); + } + return; } @@ -119,6 +133,20 @@ function getParams(params?: ScriptParams) { } function getScriptBundleForFrontend(note: BNote, script?: string, params?: ScriptParams) { + // Warn the user if they're trying to run a backend script in the frontend + if (note.isJavaScript() && note.getScriptEnv() === "backend") { + log.info(`Cannot execute note ${note.noteId} "${note.title}" in frontend, note is of type "Code: JS backend"`); + + const message = t("script.wrong-environment", { + noteTitle: note.title, + noteId: note.noteId, + actualEnv: "backend", + expectedEnv: "frontend" + }); + ws.sendMessageToAllClients({ type: "toast", message }); + return; + } + let overrideContent: string | null = null; if (script) {