// ==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.2.14 // @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 // @grant unsafeWindow // @require https://code.jquery.com/jquery-3.5.1.min.js // @supportURL https://www.facebook.com/smileopwe/ // @compatible firefox >= 87 // @compatible chrome >= 90 // @license GPLv3 // @downloadURL none // ==/UserScript== (function() { 'use strict'; // Icon download by https://www.flaticon.com/authors/pixel-perfect // Global variable GM_setValue('dialog',true); GM_setValue('URLs',location.href); GM_setValue('dw_delay',false); var $ = window.jQuery; var __additionalData = unsafeWindow.__additionalData; // Main Timer var timer = setInterval(function(){ // Record document height GM_setValue('oldHeight',$(document).height()); // Call Instagram dialog function if url changed. if(GM_getValue('URLs') != location.href && $('div.PdwC2.fXiEu.s2MYR').length && onChangeURL()){ console.log('isDialog'); onReadyMyDW(false); GM_setValue('URLs',location.href); } // Call general function if($('article ._97aPb[data-snig="canDownload"]').length==0 && onChangeURL() && !$('div._2dDPU[role="dialog"]').length){ onReadyMyDW(true); } // Call Instagram stories function if($('div#react-root section._9eogI._01nki').length && onChangeStoryURL()){ onStoryDW(false); onStoryThumbnailDW(false); } else{ // 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 .ctQZg').append('
'); } },500); // Call general function when user scroll the page $(document).scroll(function(){ if(GM_getValue('oldHeight') != $(this).height()){ onReadyMyDW(); } }); // Stories funcion function onStoryDW(isDownload){ if(isDownload){ let date = new Date().getTime(); let timestamp = Math.floor(date / 1000); let username = $("div#react-root section._9eogI._01nki div section.szopg div.Cd8X1 header.C1rPk div.B7GUE div._295C2 div.Rkqev > a").attr('title'); if($('video.y-yJ5').length){ // Download stories if it is video let downloadLink = $('video.y-yJ5 source').attr('src')+'&dl=1'; let type = 'mp4'; saveFiles(downloadLink,username,"stories",timestamp,type); } else{ // Download stories if it is image let link = $('img.y-yJ5').attr('srcset').split(',')[0].split(' ')[0]; 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){ $('div#react-root section._9eogI._01nki div section.szopg div.Cd8X1').append('
'); } } } // Stories Thumbnail funcion function onStoryThumbnailDW(isDownload){ if(isDownload){ // Download stories if it is video let downloadLink = $('img.y-yJ5').attr('srcset').split(',')[0].split(' ')[0]; let date = new Date().getTime(); let timestamp = Math.floor(date / 1000); let type = 'jpg'; let username = $("div#react-root section._9eogI._01nki div section.szopg div.Cd8X1 header.C1rPk div.B7GUE div._295C2 div.Rkqev > a").attr('title'); 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($('video.y-yJ5').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){ $('div#react-root section._9eogI._01nki div section.szopg div.Cd8X1').append('
'); } } else{ $('.IG_DWSTORY_THUMBNAIL').remove(); } } } // URL change function function onChangeURL(){ let reA = /^(https:\/\/www.instagram.com\/p\/)/g; let reB = /^(https:\/\/www.instagram.com\/)$/g; let URLs = location.href; if(URLs.match(reA) || URLs.match(reB)){ return true; } } // URL change function if page in stories function onChangeStoryURL(){ let re = /^(https:\/\/www.instagram.com\/stories\/)/g; let URLs = location.href; if(URLs.match(re)){ return true; } } // Main function function onReadyMyDW(NoDialog){ // Whether is Instagram dialog? if(!NoDialog){ // Running if it is dialog $('article ._97aPb').each(function(){ $(this).removeAttr('data-snig'); $(this).unbind('click'); }); $('.SNKMS_IG_DW_MAIN,.SNKMS_IG_DW_MAIN_VIDEO').remove(); } // Add download icon per each posts $('article ._97aPb').each(function(){ // If it is have not download icon if(!$(this).attr('data-snig')){ 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).append('
'); // Running if user click the download icon $(this).on('click','.SNKMS_IG_DW_MAIN',function(e){ GM_setValue('mVCompleted',false); GM_setValue('username',$(this).parent().attr('data-username')); GM_setValue('postpath',$(this).parent().parent().find("div.eo2As a.c-Yi7").attr("href")); // Create element that download dailog IG_createDM(GM_getValue('AutoDownload')); var 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;'; // Find video/image element and add the download icon var s = 0; var multiple = $(this).parent().find('.EcJQs .RzuR0').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 vdI = 1 $(this).parent().find('.EcJQs .RzuR0').each(function(){ if(!GM_getValue('mVCompleted')){ s++; let element_videos = $(this).parent().find('video.tWeCl'); let element_images = $(this).parent().find('.FFVAD'); if(element_videos && element_videos.attr('src')){ let video_image = (__additionalData[fullpathname])?__additionalData[fullpathname].data.graphql.shortcode_media.display_url:element_videos.next().attr('src'); let video_url = (__additionalData[fullpathname])?__additionalData[fullpathname].data.graphql.shortcode_media.video_url:element_videos.attr('src'); if(element_videos.attr('src').match(/^blob:/ig)){ $('.IG_SN_DIG .IG_SN_DIG_MAIN').append("
Loading...
"); GM_setValue('dw_delay',true); GM_xmlhttpRequest({ method: "GET", url: "https://www.instagram.com"+GM_getValue('postpath'), onload: function(response) { $('.IG_SN_DIG .IG_SN_DIG_MAIN #loadingText').remove(); let regVD = new RegExp(`,"video_url":"(.*?)"`,"sg"); let regVD2 = new RegExp(`,"video_url":"(.*?)"`,"s"); let regIM = new RegExp(`,"display_url":"(.*?)"`,"sg"); let regIM2 = new RegExp(`,"display_url":"(.*?)"`,"s"); let tot = response.responseText.match(regVD).length; while(vdI <= tot){ let vd = response.responseText.match(regVD)[vdI-1].match(regVD2)[1]; let im = response.responseText.match(regIM)[vdI].match(regIM2)[1]; let vd_url = decodeURIComponent(JSON.parse('"'+vd+'"')); let im_url = decodeURIComponent(JSON.parse('"'+im+'"')); $('.IG_SN_DIG .IG_SN_DIG_MAIN').append('
- Blob Video '+vdI+' -
'); GM_setValue('dw_delay',false); vdI++; } } }); GM_setValue('mVCompleted',true); } else if(element_videos.attr('poster')){ $('.IG_SN_DIG .IG_SN_DIG_MAIN').append('
- IGTV '+s+' -
'); } else{ $('.IG_SN_DIG .IG_SN_DIG_MAIN').append('
- Video '+s+' -
'); } } if(element_images && element_images.attr('src')){ $('.IG_SN_DIG .IG_SN_DIG_MAIN').append('
- Image '+s+' -
'); } } }); } else{ s++; let element_videos = $(this).parent().find('video.tWeCl'); let element_images = $(this).parent().find('.FFVAD'); if(element_videos && element_videos.attr('src')){ let video_image = (__additionalData[fullpathname])?__additionalData[fullpathname].data.graphql.shortcode_media.display_url:element_videos.next().attr('src'); let video_url = (__additionalData[fullpathname])?__additionalData[fullpathname].data.graphql.shortcode_media.video_url:element_videos.attr('src'); if(element_videos.attr('src').match(/^blob:/ig)){ $('.IG_SN_DIG .IG_SN_DIG_MAIN').append("
Loading...
"); GM_setValue('dw_delay',true); GM_xmlhttpRequest({ method: "GET", url: "https://www.instagram.com"+GM_getValue('postpath'), onload: function(response) { $('.IG_SN_DIG .IG_SN_DIG_MAIN #loadingText').remove(); let reg = new RegExp(`,"video_url":"(.*?)"`,"s"); let vd = response.responseText.match(reg)[1]; let vd_url = decodeURIComponent(JSON.parse('"'+vd+'"')); $('.IG_SN_DIG .IG_SN_DIG_MAIN').append('
- Blob Video '+s+' -
'); GM_setValue('dw_delay',false); } }); } else if(element_videos.attr('poster')){ $('.IG_SN_DIG .IG_SN_DIG_MAIN').append('
- IGTV '+s+' -
'); } else{ $('.IG_SN_DIG .IG_SN_DIG_MAIN').append('
- Video '+s+' -
'); } } if(element_images && element_images.attr('src')){ $('.IG_SN_DIG .IG_SN_DIG_MAIN').append('
- Image '+s+' -
'); } } if(GM_getValue('AutoDownload')){ GM_setValue('GB_Index',0); let LeftButton = $(this).parent().find('button.POSa_').length; let RightButton = $(this).parent().find('button._6CZji').length; if(LeftButton && !RightButton){ // Far Right GM_setValue('GB_Index',2); } else if(!LeftButton && RightButton){ // Far Left GM_setValue('GB_Index',1); } else if(!LeftButton && !RightButton){ // Both Not Exist GM_setValue('GB_Index',1); } else{ // Both Exist GM_setValue('GB_Index',2); } if(GM_getValue('dw_delay')){ var tempTimer = setInterval(function(){ if(!GM_getValue('dw_delay')) { let downloadLink = $('.IG_SN_DIG').find('a[data-globalindex="'+GM_getValue('GB_Index')+'"]').attr('href'); let date = new Date().getTime(); let timestamp = Math.floor(date / 1000); let type = $('.IG_SN_DIG').find('a[data-globalindex="'+GM_getValue('GB_Index')+'"]').attr('data-type'); saveFiles(downloadLink,GM_getValue('username'),GM_getValue('GB_Index'),timestamp,type); $('.IG_SN_DIG').remove(); clearInterval(tempTimer); } },150); } else{ let downloadLink = $('.IG_SN_DIG').find('a[data-globalindex="'+GM_getValue('GB_Index')+'"]').attr('href'); let date = new Date().getTime(); let timestamp = Math.floor(date / 1000); let type = $('.IG_SN_DIG').find('a[data-globalindex="'+GM_getValue('GB_Index')+'"]').attr('data-type'); saveFiles(downloadLink,GM_getValue('username'),GM_getValue('GB_Index'),timestamp,type); $('.IG_SN_DIG').remove(); } } }); // Add the mark that download is ready $(this).attr('data-snig','canDownload'); $(this).attr('data-username',$(this).prev().prev().find(".o-MQd .e1e1d .Jv7Aj a").text()); } }); } // 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); } }); // 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); }); }); })();