Need help getting function to fire when navigating forums


#1

I’ve been teaching myself JS by making things for the forums and getting my functions to fire when I needed to was always a problem. I thought I had figured it out, however it seems that what I had has become inconsistent. It might always have been inconsistent and I just didn’t notice, or it might be due to a change on Discourse, but about 1/5 of the time it just doesn’t fire, and I can’t figure out why.

What I had was this

function setTriggers() {
		//when loading the window
		window.addEventListener('load', initialiseScript);

		//when using back and forth buttons
		window.addEventListener('popstate', initialiseScript);

		//when navigating
		(function(history){
				var pushState = history.pushState;
				history.pushState = function(state) {
						if (typeof history.onpushstate == "function") {
								history.onpushstate({state: state});
						}
						initialiseScript();
						return pushState.apply(history, arguments);
				};
		})(window.history);
}

I believe both the regular event listeners work fine, the one causing me problem is the bottom one, which handles navigation between threads. To be honest I don’t entirely understand how it works, it’s something I found months ago when searching for how to solve the navigation problem.

If anyone knows why it’s inconsistent or if you have something else I could use to detect navigation that would be amazing. The only alternative I have myself is setting an interval to check the URL every second to see if it’s updated, and I’d rather not do that.


#2

https://greasyfork.org/en/scripts/28590-wk-forums-hide-users-posts/code

I tried to get that script to keep going whenever any sort of navigation occurs. But it sometimes, seemingly completely at random, it just decides it’s had enough and won’t fire anymore until an F5. So, probably not a great example, but it’s something… I guess?


#3

Just tired your code and it has the exact same problem. About 1/5 of the times it just doesn’t fire when navigating between threads. Thanks anyway, though. :hugs:


#4

Calling history.onpushstate directly seems wrong. I assume that’s included in the behavior of the original method, which you are calling via pushState.apply. If my guess is correct, that means onpushstate would be getting called twice. Try removing the following and see if it works

if (typeof history.onpushstate == "function") {
	history.onpushstate({state: state});
}

#5

It doesn’t seem to make any difference, so I suppose you’re right in that it was superfluous, but the issue persists.

edit: I have been mistaken, maybe this does work. Let me try some more

edit2: Yeah, that seems to have done it. Thanks! I wonder why it was there to begin with then.


#6

Now I gotta try fixing my thing. Thanks for giving me more work. :stuck_out_tongue_winking_eye:


#7

Still waiting for the more editor buttons script

origghghinal


#8

It’s live.
Anything you feel is missing, let me know.

https://greasyfork.org/en/scripts/37604-wanikani-more-comment-style-buttons


#9

285054948352196608