mirror of
https://github.com/zadam/trilium.git
synced 2026-05-06 19:06:35 +02:00
feat(script): warn if user is trying to run the script in a wrong environment (closes #342)
This commit is contained in:
@@ -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}}",
|
||||
|
||||
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user