I would use esprima to analyze user JavaScript functions that are stored in files or in a database. And I would allow to run only the code that passes the parsing test (only whitelisted features - using local variables, parameters, ...).
You can start with a very simple verification code that allows you to use very limited scripts and gradually improve it. However, I think you will make a lot of efforts to solve over time, because your users will always want more.
Note: Angular.js uses this type of "trick" for its dependent injection: https://jsfiddle.net/987Lwezy/
function test() { console.log("This is my secret!"); } function parser(f) { document.body.innerHTML = test.toString(); } parser(test);
source share