mirror of
				https://github.com/zadam/trilium.git
				synced 2025-10-31 18:36:30 +01:00 
			
		
		
		
	support for execution of async functions + integration with backend through server.exec()
This commit is contained in:
		| @@ -196,11 +196,11 @@ const noteEditor = (function() { | |||||||
|         return currentNote ? currentNote.detail.type : null; |         return currentNote ? currentNote.detail.type : null; | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     function executeScript() { |     async function executeScript() { | ||||||
|         if (getCurrentNoteType() === 'code') { |         if (getCurrentNoteType() === 'code') { | ||||||
|             const script = codeEditor.getValue(); |             const script = codeEditor.getValue(); | ||||||
|  |  | ||||||
|             eval(script); |             eval("(async function() {" + script + "})()"); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
|  |  | ||||||
|   | |||||||
| @@ -29,6 +29,14 @@ const server = (function() { | |||||||
|         return await call('DELETE', url); |         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; |     let i = 1; | ||||||
|     const reqResolves = {}; |     const reqResolves = {}; | ||||||
|  |  | ||||||
| @@ -92,6 +100,6 @@ const server = (function() { | |||||||
|         post, |         post, | ||||||
|         put, |         put, | ||||||
|         remove, |         remove, | ||||||
|         getHeaders |         exec | ||||||
|     } |     } | ||||||
| })(); | })(); | ||||||
							
								
								
									
										19
									
								
								routes/api/script.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										19
									
								
								routes/api/script.js
									
									
									
									
									
										Normal file
									
								
							| @@ -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; | ||||||
| @@ -27,6 +27,7 @@ const anonymizationRoute = require('./api/anonymization'); | |||||||
| const cleanupRoute = require('./api/cleanup'); | const cleanupRoute = require('./api/cleanup'); | ||||||
| const imageRoute = require('./api/image'); | const imageRoute = require('./api/image'); | ||||||
| const attributesRoute = require('./api/attributes'); | const attributesRoute = require('./api/attributes'); | ||||||
|  | const scriptRoute = require('./api/script'); | ||||||
|  |  | ||||||
| function register(app) { | function register(app) { | ||||||
|     app.use('/', indexRoute); |     app.use('/', indexRoute); | ||||||
| @@ -57,6 +58,7 @@ function register(app) { | |||||||
|     app.use('/api/anonymization', anonymizationRoute); |     app.use('/api/anonymization', anonymizationRoute); | ||||||
|     app.use('/api/cleanup', cleanupRoute); |     app.use('/api/cleanup', cleanupRoute); | ||||||
|     app.use('/api/images', imageRoute); |     app.use('/api/images', imageRoute); | ||||||
|  |     app.use('/api/script', scriptRoute); | ||||||
| } | } | ||||||
|  |  | ||||||
| module.exports = { | module.exports = { | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user