Refactor instagram embedding

This commit is contained in:
isqua 2015-01-31 13:15:25 +03:00
parent 08fa6c7bfa
commit 0f9cb4e167
1 changed files with 24 additions and 29 deletions

View File

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