diff --git a/src/entities/note.js b/src/entities/note.js index ac9f99a41..f8787011d 100644 --- a/src/entities/note.js +++ b/src/entities/note.js @@ -23,6 +23,10 @@ class Note extends Entity { return this.type === "code" && this.mime === "application/json"; } + isJavaScript() { + return this.type === "code" && this.mime === "application/javascript"; + } + async getAttributes() { return this.repository.getEntities("SELECT * FROM attributes WHERE noteId = ? AND isDeleted = 0", [this.noteId]); } diff --git a/src/public/javascripts/utils.js b/src/public/javascripts/utils.js index 9b8de60bd..eec8fa13e 100644 --- a/src/public/javascripts/utils.js +++ b/src/public/javascripts/utils.js @@ -116,8 +116,7 @@ async function stopWatch(what, func) { } function executeScript(script) { - // last \r\n is necessary if script contains line comment on its last line - eval("(async function() {" + script + "\r\n})()"); + eval(script); } function formatValueWithWhitespace(val) { diff --git a/src/routes/api/script.js b/src/routes/api/script.js index 85042a30e..ad09dfe12 100644 --- a/src/routes/api/script.js +++ b/src/routes/api/script.js @@ -31,26 +31,28 @@ router.get('/startup', auth.checkApiAuth, wrap(async (req, res, next) => { })); router.get('/subtree/:noteId', auth.checkApiAuth, wrap(async (req, res, next) => { + const repository = new Repository(req); const noteId = req.params.noteId; - const repository = new Repository(req); - - const noteScript = (await repository.getNote(noteId)).content; - - const subTreeScripts = await getSubTreeScripts(noteId, [noteId], repository); - - res.send(subTreeScripts + noteScript); + res.send(await getNoteWithSubtreeScript(noteId, repository)); })); async function getNoteWithSubtreeScript(noteId, repository) { - const noteScript = (await repository.getNote(noteId)).content; + const note = await repository.getNote(noteId); - const subTreeScripts = await getSubTreeScripts(noteId, [noteId], repository); + let noteScript = note.content; + + if (note.isJavaScript()) { + // last \r\n is necessary if script contains line comment on its last line + noteScript = "(async function() {" + noteScript + "\r\n})()"; + } + + const subTreeScripts = await getSubTreeScripts(noteId, [noteId], repository, note.isJavaScript()); return subTreeScripts + noteScript; } -async function getSubTreeScripts(parentId, includedNoteIds, repository) { +async function getSubTreeScripts(parentId, includedNoteIds, repository, isJavaScript) { const children = await repository.getEntities(` SELECT notes.* FROM notes JOIN note_tree USING(noteId) @@ -69,7 +71,7 @@ async function getSubTreeScripts(parentId, includedNoteIds, repository) { script += await getSubTreeScripts(child.noteId, includedNoteIds, repository); - if (child.mime === 'application/javascript') { + if (!isJavaScript && child.mime === 'application/javascript') { child.content = ''; }