There should be a StackExchange
method you could hook into, but I don't see it. Also, post text can change for a few reasons. (The user finished an edit, or clicked on a snippet button, or clicked on one of those "This post has been edited" alerts, or triggered a spoiler, etc.)
To keep things simple and robust, I recommend just using an interval. EG:
var yourMarkupRegex = /\{\w{1,3}\}/;
//-- Poll for edits, irregardless of the cause:
setInterval ( function () {
var allPostText = $('.post-text').text ();
if (yourMarkupRegex.test (allPostText) ) {
// CALL YOUR PRETTIFIER FUNCTION HERE.
}
}, 222);
Update:
I found an event that seems to fire whenever a post is edited, directly or remotely, or otherwise changed by the page. It's the changeData!
event. It fires on a few other page changes but not too often, except it is triggered up to 50 times on some page loads that I observed!
This offers an alternative to timers (Still the overall best approach, in my experience). You must intercept events on the page's instance of jQuery.
The following code needs either @grant none
mode, or to be injectedbe injected. :
var yourMarkupRegex = /\{\w{1,3}\}/;
var _oldJQueryEventTrigger = jQuery.event.trigger;
jQuery.event.trigger = function (event, data, elem, onlyHandlers) {
if (event == "ajaxStop") {
Prettifier ();
jQuery.event.trigger = function (event, data, elem, onlyHandlers) {
if (event == "changeData!") {
Prettifier ();
}
_oldJQueryEventTrigger (event, data, elem, onlyHandlers);
}
}
_oldJQueryEventTrigger (event, data, elem, onlyHandlers);
}
function Prettifier () {
var allPostText = $('.post-text').text ();
if (yourMarkupRegex.test (allPostText) ) {
// CALL YOUR PRETTIFIER FUNCTION HERE.
}
}
Important:
Because changeData!
fires up to scores of times during page load, we wait until the first ajaxStop
event to start triggering this way.
ajaxStop
fires near the end of the page load process (on Stack Exchange pages) and a fair bit before the window load
event fires.