Skip to content

Instantly share code, notes, and snippets.

@samsonjs
Created August 14, 2014 18:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save samsonjs/4e502948f06ae90be04e to your computer and use it in GitHub Desktop.
Save samsonjs/4e502948f06ae90be04e to your computer and use it in GitHub Desktop.
Vine's embed script
! function() {
function hasClass(el, klass) {
return el.className.match(new RegExp("(\\s|^)" + klass + "(\\s|$)"))
}
function addClass(el, klass) {
hasClass(el, klass) || (el.className += " " + klass)
}
function removeClass(el, klass) {
if (hasClass(el, klass)) {
var regex = new RegExp("(\\s|^)" + klass + "(\\s|$)");
el.className = el.className.replace(regex, " ")
}
}
if (!window.VINE_EMBEDS) {
window.VINE_EMBEDS = !0, window.setInterval(function() {
for (var els = document.querySelectorAll('iframe[src*="vine.co"]'), i = 0; i < els.length; i++) {
var top = els[i].getBoundingClientRect().top;
top > -300 && top < document.documentElement.clientHeight ? (addClass(els[i], "playing"), els[i].contentWindow.postMessage("play", "*")) : (removeClass(els[i], "playing"), els[i].contentWindow.postMessage("pause", "*"))
}
}, 100);
var d = window.addEventListener ? "addEventListener" : "attachEvent",
on = window[d],
messageEvent = "attachEvent" == d ? "onmessage" : "message";
on(messageEvent, function(el) {
var e = el.data.split("::");
var url = e[1].match(/https?:\/\/.+?(\/.+)$/)[1];
var iframes = iframes = document.querySelectorAll('iframe[src$="' + url + '"]');
if ("height" == e[0]) {
for (var i = 0; i < iframes.length; i++) {
var h = parseInt(e[2], 10);
iframes[i].style.removeProperty ? iframes[i].style.removeProperty("height") : iframes[i].style.removeAttribute("height"), iframes[i].height = 1 * h;
}
}
else if ("unmute" == e[0]) {
for (var iframe = null, i = 0; i < iframes.length; i++) {
iframe = iframes[i];
addClass(iframes[i], "unmuted");
}
iframes = document.querySelectorAll("iframe.loaded");
for (var k = 0; k < iframes.length; k++) {
if (iframes[k] != iframe) {
removeClass(iframes[k], "unmuted");
iframes[k].contentWindow.postMessage("mute", "*");
}
}
}
else if ("loaded" == e[0]) {
for (var i = 0; i < iframes.length; i++) {
iframes[i].setAttribute("frameborder", 0), addClass(iframes[i], "loaded");
iframes[i].contentWindow.postMessage("hasEmbedScript", "*");
}
}
})
}
}();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment