feat(script): warn if user is trying to run the script in a wrong environment (closes #342)

This commit is contained in:
Elian Doran
2026-04-11 11:01:04 +03:00
parent 4ed2226206
commit 3410f0f5bc
2 changed files with 31 additions and 0 deletions

View File

@@ -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}}",

View File

@@ -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) {