Last active
January 29, 2025 14:22
-
-
Save evstraw/3dac7401661cdee5b496823d1e83afaf to your computer and use it in GitHub Desktop.
Userscript for adding download links to instagram videos on the instagram website.
This file contains hidden or 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 Download Instagram Videos | |
// @namespace https://www.instagram.com/ | |
// @version 0.1 | |
// @description Adds download links for videos, even on private accounts. | |
// @author Evan Straw | |
// @include http://www.instagram.com/* | |
// @include https://www.instagram.com/* | |
// @grant none | |
// ==/UserScript== | |
(function() { | |
setInterval(addDownloadLinks,100); | |
setInterval(checkURLChange,100); | |
})(); | |
function getVideoElements(root){ | |
if(root){ | |
return root.getElementsByTagName("video"); | |
} | |
else{ | |
return document.getElementsByTagName("video"); | |
} | |
} | |
function getNumVideos(root){ | |
return getVideoElements(root).length; | |
} | |
function isPostPage(){ | |
return window.location.pathname.startsWith("/p/"); | |
} | |
function makeDLLink(url){ | |
let linkElement = document.createElement("A"); | |
linkElement.href = url; | |
linkElement.innerText = "Download Video"; | |
linkElement.target = "_blank"; | |
linkElement.style = "padding: 10px;font-size:1.5em;font-weight:bold;"; | |
linkElement.classList.add("download-link"); | |
return linkElement; | |
} | |
function alreadyHasLink(element){ | |
return element.getElementsByClassName("download-link").length > 0; | |
} | |
function getLink(element){ | |
return element.getElementsByClassName("download-link")[0]; | |
} | |
function addDownloadLinks(){ | |
let articles = document.getElementsByTagName("article"); | |
for(let article of articles){ | |
if(getNumVideos(article)>0){ | |
let videos = getVideoElements(article); | |
let dlLink = videos[0].src; | |
if(alreadyHasLink(article)){ | |
let link = getLink(article); | |
if(link.href!==dlLink){ | |
link.parentElement.removeChild(link); | |
let linkElement = makeDLLink(dlLink); | |
article.append(linkElement); | |
} | |
} | |
else{ | |
let linkElement = makeDLLink(dlLink); | |
article.append(linkElement); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment