Ideally you would do that on the server side, once a new post is saved. We have a plugin that exactly this for Amazon so you should be able to adapt it for eBay: GitHub - discourse/discourse-affiliate
If you still want to do it on the client side, you can follow this helpful tutorial: Link rewriting for affiliate codes - #8 by Johani