Last active
January 1, 2016 10:28
-
-
Save erickreutz/8131226 to your computer and use it in GitHub Desktop.
Tumblr multi-like
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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