api.onPageChange werkt niet met eindeloos scrollen

AI-generated summary

The user AstonJ is trying to modify tag links using api.onPageChange in a Discourse plugin, but the function only triggers on route changes, not during endless scroll. merefield explains that api.onPageChange won’t work for this use case and suggests overriding the tag rendering logic directly instead of using DOM selection. AstonJ attempts to override the render-tag.js file in a plugin but finds it ineffective. merefield clarifies that a plugin isn’t needed for client-side changes and recommends using a Theme Component instead. AstonJ confirms that using a Theme Component resolved the issue and asks if the plugin file override method should have worked, seeking clarification on the discrepancy.

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?

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.

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/app/assets/javascripts/discourse/app/lib/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).

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

Thanks! Funnily enough I started customising the Tag Icons 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?)