Last active
November 7, 2019 00:01
-
-
Save Rican7/9dbd28932bd01d9baee403cae36003aa to your computer and use it in GitHub Desktop.
Download all media of an Instagram post in new tabs (windows). (NOTE: Only works on direct post page links, not "lightboxed" posts from profile pages)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(function (scope) { | |
/* NOTE: Comments are all in "block" form for easier bookmark one-lining */ | |
let download = async function (url) { | |
const filename = (new URL(url)).pathname.split("/").pop(); | |
const fetched = await fetch(url); | |
const tempElement = document.createElement("a"); | |
tempElement.target = "_blank"; | |
tempElement.download = filename; | |
tempElement.href = URL.createObjectURL(await fetched.blob()); | |
tempElement.click(); | |
delete tempElement; | |
}; | |
/* Public pages will hold their entry data here */ | |
let entries = scope._sharedData.entry_data.PostPage; | |
/* If the data doesn't exist there... */ | |
if (entries[0].graphql === undefined) { | |
/* Logged in pages will have the data stored in a different location */ | |
entries = Object.values(scope.__additionalData).map(x => x.data); | |
} | |
for (let entry of entries) { | |
let entryMedia = entry.graphql.shortcode_media; | |
if (entryMedia.edge_sidecar_to_children) { | |
for (let edge of entryMedia.edge_sidecar_to_children.edges) { | |
download(edge.node.display_url); | |
} | |
} else { | |
download(entryMedia.display_url); | |
} | |
} | |
})(window); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment