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.
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).
Thanks! Funnily enough I started customising the Tag Icons last night and managed to get it to work!
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?)