Hi,
I have published this a few month ago: A more simple way to define components
It seems to me that you can reuse the code to solve your problem. You need to create a component that includes your javascript code. The example shows how to pass parameters from Python to Javascript and how to return a result from javascript.
Regards