Refactor instagram embedding

This commit is contained in:
isqua 2015-01-31 13:15:25 +03:00
parent 08fa6c7bfa
commit 0f9cb4e167

View file

@ -1553,41 +1553,36 @@ function twitter_tweet_embedding_parse_links() {
function instagram_posts_embedding_init(options) { function instagram_posts_embedding_init(options) {
var regex = new RegExp('^https?://(www\\.)?instagram\\.com/p/([\\w-]+)/?', 'i'); var regex = new RegExp('^https?://(www\\.)?instagram\\.com/p/([\\w-]+)/?', 'i');
$('.post-content a:not(.booru_pic)').each(function(num, $link) { $('.post-content a:not(.booru_pic)').each(function(num, link) {
var href = $link.href; var $link = $(link);
var n; var href = link.href;
var matches = href.match(regex);
if (n = href.match(regex)) { if (matches) {
$ajax({ $.ajax('https://api.instagram.com/oembed?url=' + 'http://instagr.am/p/' + matches[2] + '/', {
'url': 'https://api.instagram.com/oembed?url=' + urlencode('http://instagram.com/p/' + n[2] + '/'), dataType: 'json',
'success': function(text) { success: function(response) {
var answer = JSON.parse(text); var $imgLink = $('<a><img src="' + response.thumbnail_url +
var new_post = document.createElement('a'); '" + alt="' + response.title + '"></a>');
$(new_post).attr({
'id': 'instagram-' + num,
'href': answer.thumbnail_url,
'title': answer.title,
'target': '_blank',
'data-fancybox-group': (options.is('option_fancybox_bind_images_to_one_flow'))
? 'one_flow_gallery' : '',
'data-fancybox-title': (options.is('option_fancybox_smart_hints'))
? answer.title : ' '
}).addClass('postimg instagram-post-embedded');
var image = document.createElement('img'); $imgLink
image.alt = new_post.title; .addClass('postimg instagram-post-embedded')
image.src = new_post.href; .attr({
new_post.appendChild(image); id: 'instagram-' + num,
href: 'http://instagram.com/p/' + matches[2] + '/media/?size=l',
title: response.title,
traget: '_blank',
'data-fancybox-group': (options.is('option_fancybox_bind_images_to_one_flow')) ? 'one_flow_gallery' : '',
'data-fancybox-title': (options.is('option_fancybox_smart_hints')) ? response.title : ''
});
// Leave or replace $link.before($imgLink);
if (options.is('option_embedding_instagram_posts_orig_link')) {
$link.parentElement.insertBefore(new_post, $link); if ( ! options.is('option_embedding_instagram_posts_orig_link')) {
} else { $link.remove();
$($link).replaceWith(new_post);
} }
} }
}); });
} }
}); });
} }