How do you force a script to refire on every page load in Discourse?

Maybe try this?

http://api.jquery.com/ajaxcomplete/

2 Likes

You’re correct @Mittineague and that’s what I’m using now.

There’s only one small hick up which is I cannot seem to get it to work with anything except for the entire document which as you may have already guessed it, would mean it fires 2 or three times each time more posts are loaded into the post stream.

It would be really great if I can use it with the post stream as the base but so far I have not been able to do so.

However, my testing my be a bit off so I will try this again :ok_hand:

1 Like

The “poll” happens every 30 seconds to keep Notifications current. The URL contains “…/message-bus/…/poll”. I haven’t “caught” a post stream update in my dev tools yet. But if the URL is different it might be possible to “intercept” and only proceed when there are new posts.

1 Like

I tried again and I really cannot get it to work with anything except the entire document as a base

This is the closest thing I’ve seen that might be the ideal option:

The focus here being on:

this.appEvents.on('post-stream:refresh'

Which I can only assume is fired once the post stream has refreshed.

In the meanwhile, your suggestion still remains the best/only way I found to consistently fire a script while on a topic page that will re-fire once new posts are loaded. Combining the the two together now looks like this:

<script type="text/discourse-plugin" version="0.8.18">
// first you define a function
function FooBar() {
	// then bind a function to AjaxSuccess
	$(document).ajaxSuccess(function() {
		// Do stuff here that you want to be applied to all posts
	});
}

// Now we make sure the function is fired after an in-app navigation to a topic page
const TopicRoute = require("discourse/routes/topic").default;

TopicRoute.reopen({
	activate: function() {
		this._super();
		Em.run.next(function() {
			// Call FooBar() when a topic is entered. Since FooBar has a function that's bound to the
			// AjaxSuccess handler, it will fire on every AjaxSuccess
			FooBar();
		});
	},
	deactivate: function() {
		this._super();
		var foo_bar = FooBar();
		// and then we make sure to unbind the AjaxSuccess handler
		// once the use leave the topic page.
		$(document).unbind("ajaxSuccess", foo_bar);
	}
});
</script>
1 Like

Hi @lll, I am not sure about what’s exactly you are trying to achieve since I didn’t followed this topic fully. If you just want to modify all post contents as you described here then you can use below code

api.decorateCooked($elem => $elem.children('p').addClass('foo-class'));
11 Likes

Oh wow :scream: That is exactly what I’ve been searching for for the last 3 months! :heart::heart::heart:

This is really good @vinothkannans, thank you so much!

It works for me like this:

var selector = 'div[data-theme-tiles="1"]';

api.decorateCooked($elem =>
	$elem
		.children(selector)
		// do your work here for example:
		.imagesLoaded(function() {
			$(selector).masonry({
				itemSelector: ".lightbox-wrapper"
			});
		})
);

And it just works! no excessive firing, no bloat. Amazing! :fire::fire:


So, to recap all the stuff from earlier: (from my basic understanding which maybe off)

This will work if you want something done when topic pages are opened:

const TopicRoute = require("discourse/routes/topic").default;

TopicRoute.reopen({
	activate: function() {
		this._super();
		Em.run.next(function() {
			// do stuff here when a topic page is opend
		});
	}
});

And you can do stuff HOWEVER, you wont be able to modify the contents of the topic because they are in virtual dom and that’s why jQuery was not able to modify the topic contents while it was able to do stuff like console.log and modify parts of the pre-existing actual dom like this:

const TopicRoute = require("discourse/routes/topic").default;

TopicRoute.reopen({
	activate: function() {
		this._super();
		Em.run.next(function() {
			console.log("topic page opened");
			$("body").css(
				"background-color",
				"#" + ((Math.random() * 0xffffff) << 0).toString(16)
			);
		});
	}
});

In the same way, this can be used to make changes when the user leaves a topic page:

const TopicRoute = require("discourse/routes/topic").default;

TopicRoute.reopen({
	deactivate: function() {
		this._super();
		console.log("topic page closed");
		// do more stuff here or a bit of cleaning up
	}
});

Again you can do whatever you need here because the user will be leaving the topic and so you would not be modifying the virtual dom contents of the topic anyways.

Moreover;

api.onPageChange(url => {
	// do stuff here
});

Will also work on topic pages however, the same from above will still apply. You cannot modify the actual contents of the topic (again, they are in virtual dom) but you can modify elements in the pre-existing dom.

So this would work, even on topic pages:

api.onPageChange(url => {
	$("body").css(
		"background-color",
		"#" + ((Math.random() * 0xffffff) << 0).toString(16)
	);
});

Once I get a firmer grip on this I will probably create a how-to topic with the relevant bits

Thanks again @vinothkannans :sunflower:

13 Likes

Perhaps this is a better place to ask this question: I am currently attempting to use this API to trigger a redirection from a category to an external site (Redirect Category to External URL). For some reason, I cannot seem to get this API to trigger the script.

Here is the script I’m using:

<script type='text/discourse-plugin' version='0.8.19'>
$( document ).ready(function() {
	if ( window.location.href === "https://omnifora.com/c/redirect-politifora" ) {
		window.location.replace( "https://politifora.com/" );
	}
});
</script>

@Zyniker read the :arrow_up: topic carefully.

2 Likes

I suppose it would be easier for you to attempt to locate the issue if I posted the correct code:

<script type='text/discourse-plugin' version='0.8.19'>
api.onPageChange((url) => {
	if (url.includes('/c/redirect-politifora')) {
		window.location.replace('https://politifora.com/');
	}
});
</script>

@vinothkannans

While I’m uncertain why the script I posted, supra, does not work, apparently this script does work (when placed into </head> via Customize):

<script type="text/discourse-plugin" version="0.8.19">
api.onPageChange(() => {
	if ( window.location.href === "https://omnifora.com/c/redirect-politifora" ) {
		window.location.replace( "https://politifora.com/" );
	}
});
</script>
2 Likes

The previous code itself working fine for me. Can you please double-check it.

3 Likes

It appears to be working now, though I admit I have no idea why.

1 Like

Brilliant, I’ve been trying to achieve this for a long time!

Related, is there a way to do this for a topics list too? Use case: I’m trying to add a class to topics in the list that contain a specific tag.

1 Like

I’m not sure what your desired goal is, but this adds topic tags as CSS classes to items in the topic list

Anyway to style discourse topic title? ( IE, red, bold, highlight, anything. )

3 Likes

:100: that was EXACTLY what I was looking for. Thanks Joe!

3 Likes

Sorry, I have a follow up question. This script works fine in the ‘latest’ topic list, but not in the combined categories/latest topics list. Do you know how I can add it there too?

No problem,

It doesn’t work because topic-list items in the combined categories / latest page are not the same component. topic-list items in the latest - suggested - top - new - unread - user-topics - PM topic-lists and mobile latest all use this component:

https://github.com/discourse/discourse/blob/master/app/assets/javascripts/discourse/components/topic-list-item.js.es6

Whereas the ones on the desktop categories / latest view use this one

https://github.com/discourse/discourse/blob/master/app/assets/javascripts/discourse/components/latest-topic-list-item.js.es6

So the answer is that you need to make the same changes to that component as well. Try this instead and let me know if you have any problems.

<script type="text/discourse-plugin" version="0.8">
const tagClasses = function() {
  tags = this.get("topic.tags");
  if (tags && tags.length) {
    targetTopic = this.elementId;
    $("#" + targetTopic).addClass(tags);
  }
};

api.modifyClass("component:topic-list-item", {
  didInsertElement: function() {
    c = this;
    tagClasses.call(c);
  }
});

api.modifyClass("component:latest-topic-list-item", {
  didInsertElement: function() {
    c = this;
    tagClasses.call(c);
  }
}); 
</script>
8 Likes

Perfect, thanks once more!

1 Like

Thanks, It helped me for executing my dialog box ‘bootbox.dialog’ on every page load according to the condition :).
Loved this solution even in 2019 :stuck_out_tongue:

@Johani

Can you tell me what is the difference when you using this method instead standard discourse setup ?
I mean is the first time to byte or loading site faster or not?? how much ?