// ==UserScript== // @name IG Helper // @name:zh-TW IG小精靈 // @name:zh-CN IG小助手 // @name:ja IG助手 // @name:ko IG조수 // @namespace https://github.snkms.com/ // @version 2.3.7 // @description Downloading Instagram posts photos and videos or their stories! // @description:zh-TW 一鍵下載對方 Instagram 貼文中的相片、影片甚至是他們的限時動態! // @description:zh-CN 一键下载对方 Instagram 帖子中的相片、视频甚至是他们的快拍! // @description:ja 写真、ビデオ、そしてお互いの Instagram 投稿からのストーリーずズのワンクリックダウンロード! // @description:ko Instagram 게시물에서 사진, 비디오 또는 이야기를 다운로드하십시오. // @author SN-Koarashi (5026) // @match https://*.instagram.com/* // @grant GM_setValue // @grant GM_getValue // @grant GM_xmlhttpRequest // @require https://code.jquery.com/jquery-3.6.0.min.js // @supportURL https://www.facebook.com/smileopwe/ // @icon https://www.google.com/s2/favicons?domain=instagram.com // @compatible firefox >= 87 // @compatible chrome >= 90 // @compatible edge >= 90 // @license GPLv3 // @downloadURL none // ==/UserScript== (function(jQuery) { 'use strict'; // Icon download by https://www.flaticon.com/authors/pixel-perfect const $ = jQuery; const checkInterval = 250; var currentURL = location.href; var currentHeight = $(document).height(); var firstStarted = false; var pageLoaded = false; var GL_postPath; var GL_username; // Main Timer var timer = setInterval(function(){ currentHeight = $(document).height(); // Call Instagram dialog function if url changed. if(currentURL != location.href || !firstStarted || !pageLoaded){ pageLoaded = false; firstStarted = true; currentURL = location.href; if($('div.sspdcydq[role="dialog"]').length || $('main._a993[role="main"]').length){ console.log('isDialog'); setTimeout(()=>{ onReadyMyDW(false); },150); pageLoaded = true; } if(location.href == "https://www.instagram.com/"){ console.log('isHomepage'); setTimeout(()=>{ onReadyMyDW(false); },150); pageLoaded = true; } if(!pageLoaded){ // Call Instagram stories function if(location.href.match(/^(https:\/\/www\.instagram\.com\/stories\/)/ig)){ console.log('isStory'); onStoryDW(false); onStoryThumbnailDW(false); if($(".IG_DWSTORY").length) setTimeout(()=>{pageLoaded = true;},150); } else{ pageLoaded = false; // Remove the download icon $('.IG_DWSTORY').remove(); $('.IG_DWSTORY_THUMBNAIL').remove(); } } } // Direct Download Checkbox if(!$('.AutoDownload_dom').length){ let ckValue = (GM_getValue('AutoDownload'))?'checked':''; $('body ._acuq._acur').append('
'); } },checkInterval); // Call general function when user scroll the page $(document).scroll(function(){ if(currentHeight != $(this).height()){ onReadyMyDW(); } }); // Stories funcion function onStoryDW(isDownload){ if(isDownload){ let date = new Date().getTime(); let timestamp = Math.floor(date / 1000); let username = $("body > div section._ac0a header._ac0k ._ac0l div ._ac0q > a").text(); if($('body > div section._ac0a video._aa63').length){ // Download stories if it is video let downloadLink = $('video._aa63 source').attr('src')+'&dl=1'; let type = 'mp4'; saveFiles(downloadLink,username,"stories",timestamp,type); } else{ // Download stories if it is image let srcset = $('section._ac0a ._aa64 img._aa63').attr('srcset').split(',')[0].split(' ')[0]; let link = (srcset)?srcset:$('section._ac0a ._aa64 img._aa63').attr('src'); let downloadLink = link+'&dl=1'; let type = 'jpg'; saveFiles(downloadLink,username,"stories",timestamp,type); } } else{ // Add the stories download button let style = "position: absolute;right:-40px;top:15px;padding:5px;line-height:1;background:#fff;border-radius: 5px;cursor:pointer;"; if(!$('.IG_DWSTORY').length){ $('body > div section._ac0a').append('
'); return true; } } } // Stories Thumbnail funcion function onStoryThumbnailDW(isDownload){ if(isDownload){ // Download stories if it is video let srcset = $('section._ac0a ._aa64 img._aa63').attr('srcset').split(',')[0].split(' ')[0]; let downloadLink = (srcset)?srcset:$('section._ac0a ._aa64 img._aa63').attr('src'); let date = new Date().getTime(); let timestamp = Math.floor(date / 1000); let type = 'jpg'; let username = $("body > div section._ac0a header._ac0k ._ac0l div ._ac0q > a").text(); let style = 'margin:5px 0px;padding:5px 0px;color:#111;font-size:1rem;line-height:1rem;text-align:center;border:1px solid #000;border-radius: 5px;'; // Download thumbnail saveFiles(downloadLink,username,"thumbnail",timestamp,type); } else{ if($('body > div section._ac0a video._aa63').length){ // Add the stories download button let style = "position: absolute;right:-40px;top:45px;padding:5px;line-height:1;background:#fff;border-radius: 5px;cursor:pointer;"; if(!$('.IG_DWSTORY_THUMBNAIL').length){ $('body > div section._ac0a').append('
'); } } else{ $('.IG_DWSTORY_THUMBNAIL').remove(); } } } // Prepare promise to cache article which contains blob media function getBlobMedia(postPath){ return new Promise((resolve,reject)=>{ if(!postPath) reject("NOPATH"); let postShortCode = postPath.substring(3, postPath.length - 1); let getURL = `https://www.instagram.com/graphql/query/?query_hash=2c4c2e343a8f64c625ba02b2aa12c7f8&variables=%7B%22shortcode%22:%22${postShortCode}%22}`; GM_xmlhttpRequest({ method: "GET", url: getURL, onload: function(response) { let obj = JSON.parse(response.response); resolve(obj.data); }, onerror: function(err){ reject(err); } }); }); } // Main function function onReadyMyDW(NoDialog){ // Whether is Instagram dialog? if(!NoDialog){ // Running if it is dialog $('article[role="presentation"]').each(function(){ $(this).removeAttr('data-snig'); $(this).unbind('click'); }); $('.SNKMS_IG_DW_MAIN,.SNKMS_IG_DW_MAIN_VIDEO').remove(); } if(NoDialog == false){ var repeat = setInterval(() => { if($('article[data-snig="canDownload"]').length > 0) clearInterval(repeat); createArtBtn(); },250); } else{ createArtBtn(); } } function createArtBtn(){ // Add download icon per each posts $('article[role="presentation"]').each(function(){ // If it is have not download icon if(!$(this).attr('data-snig')){ console.log("Found article"); var style = "position: absolute;right:15px;top:15px;padding:6px;line-height:1;background:#fff;border-radius: 50%;cursor:pointer;"; // Add the download icon $(this).children("div").children("div").eq(1).append('
'); // Running if user click the download icon $(this).on('click','.SNKMS_IG_DW_MAIN', async function(e){ GL_username = $(this).parent().parent().parent().attr('data-username'); GL_postPath = $(this).parent().parent().children("div:last-child").children("div").children("div").children("div:last-child").find("a").attr("href"); // Create element that download dailog IG_createDM(GM_getValue('AutoDownload')); var style = 'display:block;margin:5px 0px;padding:5px 0px;color:#111;font-size:1rem;line-height:1rem;text-align:center;border:1px solid #000;border-radius: 5px;'; // Find video/image element and add the download icon var s = 0; var multiple = $(this).parent().parent().find('._aap0 ._acaz').length; var pathname = window.location.pathname; var fullpathname = "/"+pathname.split('/')[1]+"/"+pathname.split('/')[2]+"/"; // If posts have more than one images or videos. if(multiple){ var blob = false; $(this).parent().parent().find('._aap0 ._acaz').each(function(){ let element_videos = $(this).parent().parent().find('video._ab1d'); if(element_videos && element_videos.attr('src') && element_videos.attr('src').match(/^blob:/ig)){ blob = true; } }); if(blob){ $('.IG_SN_DIG .IG_SN_DIG_MAIN').append('

Loading Blob Media and others...

'); let media = await getBlobMedia(GL_postPath); let idx = 1; let resource = media.shortcode_media; // GraphVideo if(resource.__typename == "GraphVideo" && resource.video_url){ $('.IG_SN_DIG .IG_SN_DIG_MAIN').append('
- Video '+idx+' -
'); idx++; } // GraphSidecar if(resource.__typename == "GraphSidecar" && resource.edge_sidecar_to_children){ for(let e of resource.edge_sidecar_to_children.edges){ if(e.node.__typename == "GraphVideo"){ $('.IG_SN_DIG .IG_SN_DIG_MAIN').append('
- Video '+idx+' -
'); } if(e.node.__typename == "GraphImage"){ $('.IG_SN_DIG .IG_SN_DIG_MAIN').append('
- Image '+idx+' -
'); } idx++; } } $("#_SNLOAD").remove(); } else{ $(this).parent().find('._aap0 ._acaz').each(function(){ s++; let element_videos = $(this).find('video._ab1d'); let element_images = $(this).find('._aagv img'); let imgLink = (element_images.attr('srcset'))?element_images.attr('srcset').split(" ")[0]:element_images.attr('src'); if(element_videos && element_videos.attr('src')){ let video_image = element_videos.attr('poster'); let video_url = element_videos.attr('src'); $('.IG_SN_DIG .IG_SN_DIG_MAIN').append('
- Videos '+s+' -
'); } if(element_images && imgLink){ $('.IG_SN_DIG .IG_SN_DIG_MAIN').append('
- Image '+s+' -
'); } }); } } else{ s++; let element_videos = $(this).parent().parent().find('video._ab1d'); let element_images = $(this).parent().parent().find('._aagv img'); let imgLink = (element_images.attr('srcset'))?element_images.attr('srcset').split(" ")[0]:element_images.attr('src'); if(element_videos && element_videos.attr('src')){ let video_image = element_videos.attr('poster'); let video_url = element_videos.attr('src'); if(element_videos.attr('src').match(/^blob:/ig)){ $('.IG_SN_DIG .IG_SN_DIG_MAIN').append('

Loading Blob Media...

'); let media = await getBlobMedia(GL_postPath); let idx = 1; let resource = media.shortcode_media; // GraphVideo if(resource.__typename == "GraphVideo" && resource.video_url){ $('.IG_SN_DIG .IG_SN_DIG_MAIN').append('
- Video '+idx+' -
'); idx++; } // GraphSidecar if(resource.__typename == "GraphSidecar" && resource.edge_sidecar_to_children){ for(let e of resource.edge_sidecar_to_children.edges){ if(e.node.__typename == "GraphVideo"){ $('.IG_SN_DIG .IG_SN_DIG_MAIN').append('
- Video '+idx+' -
'); } if(e.node.__typename == "GraphImage"){ $('.IG_SN_DIG .IG_SN_DIG_MAIN').append('
- Image '+idx+' -
'); } idx++; } } $("#_SNLOAD").remove(); } else{ $('.IG_SN_DIG .IG_SN_DIG_MAIN').append('
- Video '+s+' -
'); } } if(element_images && imgLink){ $('.IG_SN_DIG .IG_SN_DIG_MAIN').append('
- Image '+s+' -
'); } } if(GM_getValue('AutoDownload')){ var autoTimer = setInterval(()=>{ if($('a[data-type]').length > 0){ var GB_Index = 1; if(!$('a[data-blob="true"]').length){ let selectionArr = [...$(this).parent().find('._aamk').children('._acnb')]; let LeftButton = $(this).parent().parent().find('button._aahh').length; let RightButton = $(this).parent().parent().find('button._aahi').length; if(LeftButton && !RightButton){ // Left Only GB_Index = 2; console.log('Left Only'); } else if(!LeftButton && RightButton){ // Right Only GB_Index = 1; console.log('Right Only'); } else if(!LeftButton && !RightButton){ // Both Not Exist GB_Index = 1; console.log('Both Not Exist'); } else{ // Both Exist GB_Index = 2; console.log('Both Exist'); } } else{ console.log('Blob Media'); let selectionArr = ($(this).parent().find('._aamk').children('._acnb').length)?[...$(this).parent().find('._aamk').children('._acnb')]:[...$(this).parent().find('._aamj').children('._acnb')]; let idx = 0; for(let element of selectionArr){ idx++; if($(element).attr('class').match(/_acnf/g)){ GB_Index = idx; } } } let downloadLink = $('.IG_SN_DIG').find('a[data-globalindex="'+GB_Index+'"]').attr('data-href'); let date = new Date().getTime(); let timestamp = Math.floor(date / 1000); let type = $('.IG_SN_DIG').find('a[data-globalindex="'+GB_Index+'"]').attr('data-type'); let name = $('.IG_SN_DIG').find('a[data-globalindex="'+GB_Index+'"]').attr('data-name'); saveFiles(downloadLink,GL_username,name,timestamp,type); $('.IG_SN_DIG').remove(); clearInterval(autoTimer); } },500); } }); // Add the mark that download is ready var username = $(this).find("header div:last-child span > a").text(); $(this).attr('data-snig','canDownload'); $(this).attr('data-username',username); } }); } // Download and rename files function saveFiles(downloadLink,username,index,timestamp,type){ fetch(downloadLink).then(res => { return res.blob().then(dwel => { const a = document.createElement("a"); const name = username+'-'+index+'-'+timestamp+'.'+type; a.href = URL.createObjectURL(dwel); a.setAttribute("download", name); a.click(); a.remove(); }); }); } // Create the download dialog element funcion function IG_createDM(a){ let style = (!a)?"position: fixed;left: 0px;right: 0px;bottom: 0px;top: 0px;":"display:none;"; $('body').append('
'); $('.IG_SN_DIG .IG_SN_DIG_MAIN').append('
Alt+Q [Close]
'); } // Running if document is ready $(function(){ // Ready~? GO!! onReadyMyDW(); // Close the download dialog if user click the close icon $('body').on('click','.IG_SN_DIG_BTN,.IG_SN_DIG_BG',function(){ $('.IG_SN_DIG').remove(); }); // Hot key [Alt+Q] to close the download dialog $(window).keydown(function(e){ if (e.keyCode == '81' && e.altKey){ $('.IG_SN_DIG').remove(); e.preventDefault(); } }); $('body').on('click','.AutoDownload',function(){ if($('.AutoDownload:checked').length){ GM_setValue('AutoDownload',true); } else{ GM_setValue('AutoDownload',false); } }); $('body').on('click','a[data-needed="direct"]',function(){ let date = new Date().getTime(); let timestamp = Math.floor(date / 1000); saveFiles($(this).attr('data-href'),GL_username,$(this).attr('data-name'),timestamp,$(this).attr('data-type')); }); // Running if user left-click download icon in stories $('body').on('click','.IG_DWSTORY',function(){ onStoryDW(true); }); // Running if user left-click download icon in stories $('body').on('click','.IG_DWSTORY_THUMBNAIL',function(){ onStoryThumbnailDW(true); }); }); })(jQuery);