project-clippi icon indicating copy to clipboard operation
project-clippi copied to clipboard

Add action to execute JS scripts

Open nachoverdon opened this issue 2 years ago • 3 comments

I needed a way to execute some code when a combo occured. This adds a simple action, that allows you to select a .js file, and then executes it, passing it the data from the event that triggered it.

  • Add action to execute JS scripts (adds dependency on VM2)
  • Include event data on emitEvent
  • Add .idea folder to .gitignore

nachoverdon avatar Jul 24 '22 19:07 nachoverdon

Hey! Thanks for this PR! I've been busy working on the Slippi Launcher these days so I haven't had a chance to go through user contributed PRs, but out of curiosity how does this PR relate with #126? It seems like if #126 was merged you'd also be able to run JS anyways without relying on another dependency VM2. You would just need to put in node yourScript.js in the Run Command action, with the added benefit of not needing to have a custom action for every programming language.

vinceau avatar Jul 24 '22 23:07 vinceau

I didn't see that PR. That could have saved me some time. You are right, #126 solution is more universal.

The only real difference is that I pass the data from the event that triggered the action, which was needed in my case.

Also, it doesn't require that you have Node installed in order to run the script, but I don't think this is too much of a problem, since this is a somewhat more advanced feature that only people with programming knowledge would use.

Note that this could have been done without depending on VM2, but while looking for recommended ways to execute user provided scripts, this popped up, as it was supposed to be more secure. "immune to all known methods of attacks" they claim lol.

So #126 doesn't solve my problem. If that is merged instead, we would need a way to add the event data as an argument to the command, maybe as a stringifyed JSON. We could make it so that it replaces some string in the given command, like:

node yourScript.js {event_data} && dosomethingelseorwhatever

this should turn into

node yourScript.js '{"id":"rkuepdffra","type":"...' && dosomethingelseorwhatever

nachoverdon avatar Jul 25 '22 10:07 nachoverdon

I tried a modified version of #126 , adding the event data to the actions calls and changing the way the command gets called to replace the {event} string with the data.

const ctxJson = JSON.stringify(ctx).replace(/"/gm, '\\"');
const command = ctx ? params.command.replace(/{event}/gm, ctxJson) : params.command;

runCommand(command);

So that I can call it like this:

node C:\Users\bazoo\Desktop\Smash\yourScript.js {event}

and then on yourScript.js I just do this:

const event = JSON.parse(process.argv[2]);
console.log(event.type);

nachoverdon avatar Jul 25 '22 13:07 nachoverdon

Closing in favour of #156.

vinceau avatar Jun 22 '23 07:06 vinceau