Created
June 12, 2016 10:52
-
-
Save dshster/2f4aef73fa336e126f83a06efb1c0a35 to your computer and use it in GitHub Desktop.
Append download link to instagram photos (userscript)
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
// ==UserScript== | |
// @name Instagram image in a new tab | |
// @namespace http://shvalyov.ru | |
// @version 0.1 | |
// @description append download link to photos | |
// @author dshster | |
// @match https://www.instagram.com/* | |
// @grant none | |
// ==/UserScript== | |
(function(window, undefined) { | |
'use strict'; | |
var document = window.document; | |
var $main = document.querySelector('main > section > div > div'); | |
function appendDownloadLinkToArticle(articleNode) { | |
var downloadLink = document.createElement('a'); | |
var header = articleNode.querySelector('header'); | |
var image = articleNode.querySelector('div > div > div > img[src]'); | |
var imageUrl = image.src; | |
var filename = imageUrl.split('/').pop(); | |
downloadLink.href = imageUrl; | |
downloadLink.setAttribute('download', filename); | |
downloadLink.appendChild(document.createTextNode('Download image')); | |
header.appendChild(downloadLink); | |
downloadLink.onclick = function(event) { | |
event.preventDefault(); | |
} | |
} | |
var observer = new window.MutationObserver(function(mutations) { | |
mutations.forEach(function(mutation) { | |
var article; | |
if (mutation.addedNodes.length === 1) { | |
article = mutation.addedNodes[0]; | |
if (article.nodeName === 'ARTICLE') { | |
appendDownloadLinkToArticle(article); | |
} | |
} | |
}); | |
}); | |
observer.observe($main, { childList: true }); | |
var articles = document.querySelectorAll('article'); | |
Array.prototype.forEach.call(articles, function(article) { | |
appendDownloadLinkToArticle(article); | |
}); | |
})(window); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Firefox 84.0.1 x64, Tampermonkey 4.11.6120 - не распознается, как userscript.