api.onPageChange not working with endless scroll

I’m trying to change the tag links using the below and it seems to work on refreshing the page or going to a new page, but it does not work when you scroll down the page where endless scroll loads in new items:

<script type="text/discourse-plugin" version="0.8">
	api.onPageChange(() =>{
		var tags = document.getElementsByClassName("discourse-tag");
		for (i = 0; i < tags.length; i++) {
			var tag = tags[i];
			var url = tag.href;
			var newUrl = "https://site.com" + url;
			tag.href = newUrl;
		}
	});
</script>

Do I need to use something other than or in addition to api.onPageChange?

1 Like

Yeah that won’t work. It’s only going to fire on a route change. You need to consider overriding the code that actually renders those tags directly rather than using Dom selection.

8 Likes

Thanks Robert - I was hoping a something simple would suffice! :see_no_evil:

It’s been a while since I read the plugin guide but from memory it goes something like this?

  • rails g plugin NAME
  • locate the file you want to change
  • copy it into the same directory in your plugin
  • make your changes

However this is not working for me.

Editing this file directly (on line 29): discourse/render-tag.js at bbe5d8d5cf1220165842985c0e2cd4c454d501cd · discourse/discourse · GitHub has the desired effect, but when I duplicate the file in the following location it has no impact plugins/my-plugin/app/assets/javascripts/discourse/app/lib/render-tag.js.

Any ideas what I’m doing wrong? Has something changed since I read the guides perhaps? (For some reason I thought it was as simple as overriding files to change small things like this).

1 Like

You don’t need a plugin, this is client side. Use a Theme Component.

6 Likes

Thanks! Funnily enough I started customising the Tag icons component last night and managed to get it to work! :smiley:

Out of curiosity tho, should the plugin file-override method as I described above should have worked as well? (If so any ideas what I was doing wrong?)

1 Like