From 0f9cb4e1672d6fe3b810f7e470258b4f48bef64b Mon Sep 17 00:00:00 2001 From: isqua Date: Sat, 31 Jan 2015 13:15:25 +0300 Subject: [PATCH] Refactor instagram embedding --- chrome_point_plus/js/point-plus.js | 53 ++++++++++++++---------------- 1 file changed, 24 insertions(+), 29 deletions(-) diff --git a/chrome_point_plus/js/point-plus.js b/chrome_point_plus/js/point-plus.js index 0c5e3dd..598a9db 100644 --- a/chrome_point_plus/js/point-plus.js +++ b/chrome_point_plus/js/point-plus.js @@ -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 = $('' + response.title + ''); - 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(); } } }); - } }); }