Skip to content

Instantly share code, notes, and snippets.

@revenkroz
Last active January 12, 2023 10:42
Show Gist options
  • Save revenkroz/8ea295c2dc4b3752567135590ccc88d6 to your computer and use it in GitHub Desktop.
Save revenkroz/8ea295c2dc4b3752567135590ccc88d6 to your computer and use it in GitHub Desktop.
Instagram Image Download (updated for 2023)

Instagram Image Download (User Script)

How To Use

  1. Install TamperMonkey extension
  2. Click on Raw button near the instagram-image-download.user.js script
  3. Click "Install"

Now you have a download button on each photo.

// ==UserScript==
// @name Instagram Image Download
// @namespace ig
// @include https://www.instagram.com/*
// @match *://www.instagram.com/*
// @version 1.3
// @grant none
// @author Oleg Koval
// ==/UserScript==
let lastUrl = location.href;
let styleTag = document.createElement('style');
styleTag.innerHTML = `
.ig-photo-dl {
z-index: 11;
position: absolute;
top: 16px;
left: 16px;
padding: 11px 10px 7px;
background-color: #fff;
text-decoration: none;
border-radius: 2px;
color: #222;
opacity: 0.6;
}
.ig-photo-dl:hover, .ig-photo-dl:active, .ig-photo-dl:focus {
opacity: 1;
color: #222;
text-decoration: none;
}
`;
document.body.appendChild(styleTag);
function setButtons() {
const uls = document.querySelectorAll('article ul');
if (uls.length === 0) {
return;
}
let imgs = uls[0].querySelectorAll('img');
for (const img of imgs) {
if (img.parentNode.getElementsByClassName('ig-photo-dl').length) {
continue;
}
if (img.parentNode.parentNode.parentNode.tagName.toLowerCase() === 'a') {
continue;
}
let imageUrl = img.getAttribute('src');
let dl = document.createElement('a');
dl.href = imageUrl;
dl.setAttribute('download', imageUrl.substr(imageUrl.lastIndexOf('/') + 1));
dl.setAttribute('target', '_blank');
dl.setAttribute('class', 'ig-photo-dl');
dl.innerHTML = `
<svg version="1.1" id="Capa_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px"
width="16px" height="16px" viewBox="0 0 512 512" style="enable-background:new 0 0 512 512;" xml:space="preserve">
<g>
<path d="M256,512l256-256H352V0.001L160,0v256H0L256,512z"/>
</g>
</svg>
`;
img.parentNode.insertBefore(dl, img);
let observer = new MutationObserver(function(mutations) {
for (const mutation of mutations) {
if (mutation.attributeName == 'src') {
console.log(mutation);
let target = mutation.target;
let button = target.previousSibling;
let targetImageUrl = target.src;
button.href = targetImageUrl;
button.setAttribute('download', targetImageUrl.substr(targetImageUrl.lastIndexOf('/') + 1));
}
}
});
observer.observe(img.parentNode, {attributes: true, subtree: true});
}
}
// something changed?
let observer = new MutationObserver(function(mutations) {
setButtons();
});
observer.observe(document, {attributes: true, childList: true, characterData: false, subtree: true});
@Korb
Copy link

Korb commented Jan 5, 2021

Под Firefox 85.0b4 (64-bit) и Tampermonkey 4.11.6120 - работает, под Firefox 84.0.1 x64 - нет.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment