IG小精靈

一鍵下載Instagram貼文中的相片及影片

目前為 2020-06-03 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         IG小精靈
// @namespace    https://github.snkms.com/
// @version      2.0.0
// @description  一鍵下載Instagram貼文中的相片及影片
// @author       5026
// @match        https://*.instagram.com/*
// @grant        GM_setValue
// @grant        GM_getValue
// @require      https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js
// @supportURL   https://www.facebook.com/smileopwe/
// ==/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('aas');
            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(onChangeURL() && $('div.PdwC2.fXiEu.s2MYR').length){
            $("body").on('DOMSubtreeModified', "div.PdwC2.fXiEu.s2MYR", function() {
                console.log('aa');
            });
        }
        */
    },200);

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

    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 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:5px;line-height:1;background:#fff;border-radius: 5px;cursor:pointer;";
                if($(this).find('video.tWeCl').length){

                    $(this).append('<div class="SNKMS_IG_DW_MAIN_VIDEO" style="'+style+'">提取影片資料</div>');
                    $(this).on('click','.SNKMS_IG_DW_MAIN_VIDEO',function(e){
                        IG_createDM();
                        var i = 0;
                        $(this).parent().find('video.tWeCl').each(function(){
                            i++;
                            console.log($(this).attr('src'));
                            $('.IG_SN_DIG>div').append('<a 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;" target="_blank" href="'+$(this).attr('src')+'&dl=1">影片'+i+'</a>');
                        });
                    });
                }
                else{
                    $(this).append('<div class="SNKMS_IG_DW_MAIN" style="'+style+'">提取圖片資料</div>');
                    $(this).on('click','.SNKMS_IG_DW_MAIN',function(e){
                        IG_createDM();
                        var i = 0;
                        $(this).parent().find('.FFVAD').each(function(){
                            i++;
                            console.log($(this).attr('src'));
                            $('.IG_SN_DIG>div').append('<a 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;" target="_blank" href="'+$(this).attr('src')+'&dl=1">圖片'+i+'</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: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('<button class="IG_SN_DIG_BTN">關閉視窗</button>');
    }
    $(function(){
        onReadyMyDW();

        $('body').on('click','.IG_SN_DIG_BTN',function(){
            $('.IG_SN_DIG').remove();
        });

    });
})();