Skip to content

Instantly share code, notes, and snippets.

@Rican7
Last active November 7, 2019 00:01
Show Gist options
  • Save Rican7/9dbd28932bd01d9baee403cae36003aa to your computer and use it in GitHub Desktop.
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)
(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