Skip to content

Instantly share code, notes, and snippets.

@morningtoast
Created January 5, 2016 21:15
Show Gist options
  • Select an option

  • Save morningtoast/bc9a3deb0d4d3bc12b03 to your computer and use it in GitHub Desktop.

Select an option

Save morningtoast/bc9a3deb0d4d3bc12b03 to your computer and use it in GitHub Desktop.
Instagram link converter to embed
/*
Instagram Embed Link Sniffer - Converts Instagram links into embeds
Requires jQuery + Instagram API JS
https://platform.instagram.com/en_US/embeds.js
*/
var Instabed = {
mark_class: "instagram-link",
container: "body",
total:0,
count:0,
// Load class, check for API and then start sniffing
// Parameters: Selector of parent container
load:function(container) {
if (typeof window.instgrm !== 'undefined') {
this.container = container;
this.get_count();
this.process();
} else {
console.error("Instagram API script required");
}
},
// Gets a total count of all Instagram links
get_count:function() {
$(this.container).find("a").each(function(k, el) {
var el = $(this);
var href = el.attr("href");
if ((href.indexOf("instagram") >= 0) || (href.indexOf("instagr.am") >= 0)) {
el.addClass(Instabed.mark_class);
Instabed.total += 1;
}
});
},
// Goes back through each Instagram link and passes it to the API
process:function() {
$("a."+Instabed.mark_class).each(function(k, el) {
var href = $(this).attr("href");
Instabed.api(href, this);
$(this).hide();
});
},
// Goes out to Instagram to get data. Once all have been pre-loaded, runs API embed process
api:function(url, origin) {
$.ajax({
method: "GET",
url: "https://api.instagram.com/oembed/?omitscript=true&maxwidth=600&url="+url,
dataType: "jsonp",
success: function(data) {
$(origin).before(data.html);
Instabed.count += 1;
if (Instabed.count >= Instabed.total) {
window.instgrm.Embeds.process();
}
},
error: function(jqXHR, textStatus, errorThrown) {
console.error("Check you internet Connection");
}
});
}
}
//Instabed.load("#foo"); // How to init, pass it container element selector
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment