mirror of
				https://github.com/TriliumNext/Notes.git
				synced 2025-11-04 15:11:31 +08:00 
			
		
		
		
	support for execution of async functions + integration with backend through server.exec()
This commit is contained in:
		
							parent
							
								
									27cb6b1c4d
								
							
						
					
					
						commit
						f439969962
					
				@ -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 + "})()");
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -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
 | 
			
		||||
    }
 | 
			
		||||
})();
 | 
			
		||||
							
								
								
									
										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 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 = {
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user