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


(Mittineague) #24

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.


(Joe) #25

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>

(Vinoth Kannan) #26

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'));

IFrame CSS Code - Disable Scrolling Bar
Tiles Image Gallery
(Joe) #27

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:


#28

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>

(Vinoth Kannan) #30

@Zyniker read the :arrow_up: topic carefully.


#31

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


#32

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>

(Vinoth Kannan) #33

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


#34

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


(Bart) #35

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.


(Joe) #36

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. )


(Bart) #37

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


(Bart) #38

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?


(Joe) #39

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:

discourse/topic-list-item.js.es6 at master · discourse/discourse · GitHub

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

discourse/latest-topic-list-item.js.es6 at master · discourse/discourse · GitHub

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>

(Bart) #40

Perfect, thanks once more!