diff --git a/public/javascripts/note_editor.js b/public/javascripts/note_editor.js index 50362b1f0..7d1d98005 100644 --- a/public/javascripts/note_editor.js +++ b/public/javascripts/note_editor.js @@ -196,11 +196,11 @@ const noteEditor = (function() { return currentNote ? currentNote.detail.type : null; } - function executeScript() { + async function executeScript() { if (getCurrentNoteType() === 'code') { const script = codeEditor.getValue(); - eval(script); + eval("(async function() {" + script + "})()"); } } diff --git a/public/javascripts/server.js b/public/javascripts/server.js index 58f5aa47b..95c3a404b 100644 --- a/public/javascripts/server.js +++ b/public/javascripts/server.js @@ -29,6 +29,14 @@ const server = (function() { return await call('DELETE', url); } + async function exec(script) { + if (typeof script === "function") { + script = script.toString(); + } + + return await post('script/exec', { script: script }); + } + let i = 1; const reqResolves = {}; @@ -92,6 +100,6 @@ const server = (function() { post, put, remove, - getHeaders + exec } })(); \ No newline at end of file diff --git a/routes/api/script.js b/routes/api/script.js new file mode 100644 index 000000000..d8dfc2955 --- /dev/null +++ b/routes/api/script.js @@ -0,0 +1,19 @@ +"use strict"; + +const express = require('express'); +const router = express.Router(); +const auth = require('../../services/auth'); +const wrap = require('express-promise-wrap').wrap; +const log = require('../../services/log'); + +router.post('/exec', auth.checkApiAuth, wrap(async (req, res, next) => { + log.info('Executing script: ' + req.body.script); + + const ret = await eval("(" + req.body.script + ")()"); + + log.info('Execution result: ' + ret); + + res.send(ret); +})); + +module.exports = router; \ No newline at end of file diff --git a/routes/routes.js b/routes/routes.js index bcc2abe7a..326d32158 100644 --- a/routes/routes.js +++ b/routes/routes.js @@ -27,6 +27,7 @@ const anonymizationRoute = require('./api/anonymization'); const cleanupRoute = require('./api/cleanup'); const imageRoute = require('./api/image'); const attributesRoute = require('./api/attributes'); +const scriptRoute = require('./api/script'); function register(app) { app.use('/', indexRoute); @@ -57,6 +58,7 @@ function register(app) { app.use('/api/anonymization', anonymizationRoute); app.use('/api/cleanup', cleanupRoute); app.use('/api/images', imageRoute); + app.use('/api/script', scriptRoute); } module.exports = {