Refactor js

Rewriting our js to be more readable and less hacky.

I would recommend using `json.dumps` on a Python dict here, because all this concatenation makes the code difficult to read.