Can topic templates have javascript?

If you use tags you won’t have to write a plugin that will be likely to break every time templates change upstream. I recommend that you spend an hour or so trying that before starting to write a plugin.

You can have a look at discourse-topic-previews-sidecar/plugin.rb at master · merefield/discourse-topic-previews-sidecar · GitHub for an example of creating Topic Custom Fields.