Skip to content

Instantly share code, notes, and snippets.

@erickreutz
Last active January 1, 2016 10:28
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save erickreutz/8131226 to your computer and use it in GitHub Desktop.
Save erickreutz/8131226 to your computer and use it in GitHub Desktop.
Tumblr multi-like
(function() {
// This handles having multiple like buttons of the same post on the page
// at once. When you like/unlike one the others will update accordingly
// I.E. One in a grid of posts and one in an overlay loaded via ajax on top of the grid.
// See thingslog-theme.tumblr.com as an example.
if (Tumblr && Tumblr.LikeButton) {
Tumblr.LikeButton.update_like_state = function(data) {
if (data.post_id) {
var iframes = document.querySelectorAll("#like_iframe_" + data.post_id);
for (var i = 0; i < iframes.length; i++) {
iframes[i].contentWindow.postMessage("state_update;" + JSON.stringify(data), "http://assets.tumblr.com");
}
}
};
}
if (Tumblr) {
Tumblr.LikeButtonUpdate = (function() {
return {
post_message_event: function(action, a) {
var name = action[0];
var postID = action[1];
var result = action[2];
if (name === "tumblelog_like") {
var buttons = document.querySelectorAll("#like_button_" + postID);
for (var i = 0; i < buttons.length; i++) {
buttons[i].classList[result === "liked" ? "add" : "remove"]("liked");
}
}
}
}
}());
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment