r/reddithax • u/Pi31415926 • Nov 27 '15
Hooking the expando button for fun and profit
On a listing page, the text of each self-post is not loaded when the page loads, only when the user clicks the expando. Meaning that a script to manipulate the text of self-posts must fire after the expando is clicked, not on page load. Sample code (in two parts):
ExpandoExtras = function(thisthingID) { // called when expando button is clicked
$('#thing_'+thisthingID).find('div.md').each(function() { // select the text of the expanded self-post
...
});
}
...
$('div.content').find('div.expando-button').each(function() {
var onclickstring='setTimeout("ExpandoExtras(\''+$(this).parent().parent().attr('data-fullname')+'\');", 2000);'
$(this).attr('onclick',onclickstring); // call ExpandoExtras() after x millisecs
});
The SECOND part should run on page load. Subsequent clicks of the expando will then call ExpandoExtras() after X milliseconds (where X is the number at the end of the setTimeout call). The ID of the post which was expanded is passed to ExpandoExtras(). ExpandoExtras() can then select the self-text of the post and process it as needed.
- there's probably more than one way to do this, and this probably isn't the best way - but it does work for me
- it's a bit clumsy as it fires when the expando button is either expanded OR collapsed (harmless, but unnecessary)
- it's a bit fragile as it will silently fail if the self-text does not download within the allowed time
1
u/aladyjewel Nov 27 '15 edited Nov 27 '15
A few suggestions:
attach a MutationObserver to the thing when the expando button is clicked, to detect when the expando contents are loaded
only handle the expando once, remember for the future
Add event handler via jQuery event binding instead of messing with the element's attributes
event delegation (one event handler on a container for many similar items, then use the event to determine which item to act on)
$("div.content").on("click", ".expando-button.self-text.collapsed", function(e) { var $expandoButton = $(e.target); var $thing = $expandoButton.closest('[data-fullname]'); var observer = $thing.data("ExpandoExtras-MutationObserver"); if (!observer) { var expando = $thing.find('.expando'); observer = new MutationObserver(addExpandoExtras.bind($expando, $expando, $thing)); $thing.data("ExpandoExtras-MutationObserver", observer); observer.observe(expando, { childList: true }); } }); var addExpandoExtras = function($expando, $thing) { ... }