diff --git a/js/EmbedIt.js b/js/EmbedIt.js index 8f3e41a..f0c33e8 100644 --- a/js/EmbedIt.js +++ b/js/EmbedIt.js @@ -366,17 +366,30 @@ embedit.transformRedditData = function (pic) { pic.url = pic.url.replace(http_prefix, https_prefix); } else if (pic.url.indexOf("reddit.com/gallery") >= 0) { console.log("GOTCHA!"); + + var dataSource; if (pic.data.gallery_data && pic.data.gallery_data.items) { - var firstItemId = pic.data.gallery_data.items[0].media_id; - var encodedUrl = pic.data.media_metadata[firstItemId]["s"]["u"]; - pic.type = embedit.imageTypes.image; - if (encodedUrl === undefined) { - // some posts don't have the u key, but have gif and mp4 keys - encodedUrl = pic.data.media_metadata[firstItemId]["s"]["mp4"]; - pic.type = embedit.imageTypes.gifv; - } - pic.url = decodeEntities(encodedUrl); + dataSource = pic.data; + } else if ( + pic.data.crosspost_parent_list && + pic.data.crosspost_parent_list[0] && + pic.data.crosspost_parent_list[0].gallery_data && + pic.data.crosspost_parent_list[0].gallery_data.items + ) { + // Grab the first image from the crosspost parent + dataSource = pic.data.crosspost_parent_list[0]; + } + + var firstItemId = dataSource.gallery_data.items[0].media_id; + var encodedUrl = dataSource.media_metadata[firstItemId]["s"]["u"]; + + pic.type = embedit.imageTypes.image; + if (encodedUrl === undefined) { + // some posts don't have the u key, but have gif and mp4 keys + encodedUrl = dataSource.media_metadata[firstItemId]["s"]["mp4"]; + pic.type = embedit.imageTypes.gifv; } + pic.url = decodeEntities(encodedUrl); console.log(pic.url); } else if (isImageExtension(pic.url)) { // simple image