IG小助手

一键下载对方 Instagram 帖子中的相片、视频甚至是快拍!

目前为 2020-06-05 提交的版本。查看 最新版本

// ==UserScript==
// @name               IG Helper
// @name:zh-TW         IG小精靈
// @name:zh-CN         IG小助手
// @namespace          https://github.snkms.com/
// @version            2.0.10
// @description        Downloading Instagram posts photos and videos or their stories!
// @description:zh-TW  一鍵下載對方 Instagram 貼文中的相片、影片甚至是限時動態!
// @description:zh-CN  一键下载对方 Instagram 帖子中的相片、视频甚至是快拍!
// @author             SN-Koarashi (5026)
// @match              https://*.instagram.com/*
// @grant              GM_setValue
// @grant              GM_getValue
// @require            https://code.jquery.com/jquery-3.5.1.min.js
// @supportURL         https://www.facebook.com/smileopwe/
// @compatible         firefox >=52
// @compatible         chrome >=55
// @license            MIT
// ==/UserScript==

(function() {
    'use strict';
    GM_setValue('dialog',true);
    GM_setValue('URLs',location.href);

    var $ = window.jQuery;
    var timer = setInterval(function(){

        GM_setValue('oldHeight',$(document).height());


        if(GM_getValue('URLs') != location.href && $('div.PdwC2.fXiEu.s2MYR').length && onChangeURL()){
            console.log('isDialog');
            onReadyMyDW(false);
            GM_setValue('URLs',location.href);
        }

        if($('article ._97aPb[data-snig="canDownload"]').length==0 && onChangeURL() && !$('div._2dDPU[role="dialog"]').length){
            console.log(true);
            onReadyMyDW(true);
        }

        if($('div#react-root section._9eogI._01nki.lXJWB').length && onChangeStoryURL()){
            onStoryDW(false);
        }
        else{
            $('.IG_DWSTORY').remove();
        }
    },500);

    $(document).scroll(function(){
        if(GM_getValue('oldHeight') != $(this).height()){
            console.log('onChange');
            onReadyMyDW();
        }
    });

    function onStoryDW(a){
        if(a){
            if($('video.y-yJ5').length){
                window.open($('video.y-yJ5 source').attr('src')+'&dl=1');
            }
            else{
                window.open($('img.y-yJ5').attr('src')+'&dl=1');
            }
        }
        else{
            var 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._8XqED').append('<div class="IG_DWSTORY" style="'+style+'"><svg width="16" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 384 512"><path d="M224 136V0H24C10.7 0 0 10.7 0 24v464c0 13.3 10.7 24 24 24h336c13.3 0 24-10.7 24-24V160H248c-13.2 0-24-10.8-24-24zm76.45 211.36l-96.42 95.7c-6.65 6.61-17.39 6.61-24.04 0l-96.42-95.7C73.42 337.29 80.54 320 94.82 320H160v-80c0-8.84 7.16-16 16-16h32c8.84 0 16 7.16 16 16v80h65.18c14.28 0 21.4 17.29 11.27 27.36zM377 105L279.1 7c-4.5-4.5-10.6-7-17-7H256v128h128v-6.1c0-6.3-2.5-12.4-7-16.9z"/></svg></div>')
            }
        }
    }

    function onChangeURL(){
        var reA = /^(https:\/\/www.instagram.com\/p\/)/g;
        var reB = /^(https:\/\/www.instagram.com\/)$/g;
        var URLs = location.href;
        if(URLs.match(reA) || URLs.match(reB)){
            return true;
        }
    }

    function onChangeStoryURL(){
        var re = /^(https:\/\/www.instagram.com\/stories\/)/g;
        var URLs = location.href;
        if(URLs.match(re)){
            return true;
        }
    }

    function onReadyMyDW(NoDialog){
        if(!NoDialog){
            $('article ._97aPb').each(function(){
                $(this).removeAttr('data-snig');
                $(this).unbind('click');
            });
            $('.SNKMS_IG_DW_MAIN,.SNKMS_IG_DW_MAIN_VIDEO').remove();
        }

        $('article ._97aPb').each(function(){
            if(!$(this).attr('data-snig')){
                var style = "position: absolute;right:15px;top:15px;padding:3px;line-height:1;background:#fff;border-radius: 5px;cursor:pointer;";


                $(this).append('<div class="SNKMS_IG_DW_MAIN" style="'+style+'"><svg width="16" xmlns="http://www.w3.org/2000/svg" viewBox="0 0 384 512"><path d="M224 136V0H24C10.7 0 0 10.7 0 24v464c0 13.3 10.7 24 24 24h336c13.3 0 24-10.7 24-24V160H248c-13.2 0-24-10.8-24-24zm76.45 211.36l-96.42 95.7c-6.65 6.61-17.39 6.61-24.04 0l-96.42-95.7C73.42 337.29 80.54 320 94.82 320H160v-80c0-8.84 7.16-16 16-16h32c8.84 0 16 7.16 16 16v80h65.18c14.28 0 21.4 17.29 11.27 27.36zM377 105L279.1 7c-4.5-4.5-10.6-7-17-7H256v128h128v-6.1c0-6.3-2.5-12.4-7-16.9z"/></svg></div>');
                $(this).on('click','.SNKMS_IG_DW_MAIN',function(e){
                    IG_createDM();
                    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;';
                    var i = 0;
                    $(this).parent().find('video.tWeCl').each(function(){
                        i++;
                        console.log($(this).attr('src'));

                        $('.IG_SN_DIG>div').append('<a style="'+style+'" target="_blank" href="'+$(this).attr('src')+'&dl=1"><img width="100" src="'+$(this).next().attr('src')+'" /><br/>Video '+i+'</a>');
                    });

                    var n = 0;
                    $(this).parent().find('.FFVAD').each(function(){
                        n++;
                        console.log($(this).attr('src'));
                        $('.IG_SN_DIG>div').append('<a style="'+style+'" target="_blank" href="'+$(this).attr('src')+'&dl=1"><img width="100" src="'+$(this).attr('src')+'" /><br/>Image '+n+'</a>');
                    });
                });

                $(this).attr('data-snig','canDownload');
            }
        });
    }

    function IG_createDM(){
        $('body').append('<div class="IG_SN_DIG" style="position: fixed;left: 0px;right: 0px;bottom: 0px;top: 0px;background: rgba(0,0,0,.75);z-index: 500;"><div style="z-index: 501;padding:10px 15px;top:7%;position: absolute;left: 50%;transform: translateX(-50%);width: 500px;min-height: 200px;background:#fff;border-radius: 15px;"></div></div>');
        $('.IG_SN_DIG>div').append('<div style="position:relative;height:36px;line-height:36px;">Alt+Q [關閉/Close]<svg class="IG_SN_DIG_BTN" style="cursor:pointer;position:absolute;right:0px;" xmlns="http://www.w3.org/2000/svg" width="36" height="36" viewBox="0 0 36 36"><path d="M28.5 9.62l-2.12-2.12-8.38 8.38-8.38-8.38-2.12 2.12 8.38 8.38-8.38 8.38 2.12 2.12 8.38-8.38 8.38 8.38 2.12-2.12-8.38-8.38z"/><path d="M0 0h36v36h-36z" fill="none"/></svg></div>');
    }

    $(function(){
        onReadyMyDW();

        $('body').on('click','.IG_SN_DIG_BTN',function(){
            $('.IG_SN_DIG').remove();
        });
        $(window).keydown(function(e){
            if (e.keyCode == '81' && e.altKey){
                $('.IG_SN_DIG').remove();
                e.preventDefault();
            }
        });

        $('body').on('click','.IG_DWSTORY',function(){
            onStoryDW(true);
        });
    });

})();