Instagram Story Downloader

Adds a button that lets you download stories

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Greasemonkey 油猴子Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Violentmonkey 暴力猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴Userscripts ,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey 篡改猴,才能安装此脚本。

您需要先安装一款用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         Instagram Story Downloader
// @namespace    http://tampermonkey.net/
// @version      0.1
// @description  Adds a button that lets you download stories
// @author       Poup2804
// @match        https://www.instagram.com/stories/*
// @grant        none
// @run-at      document-idle
// ==/UserScript==


(function() {
    'use strict';
    let menuPath = "#react-root > section > div > div > section > div.GHEPc"
    let videoPath = "#react-root > section > div > div > section > div.GHEPc > div._53rSq > div > div > video"
    let imagePath = "#react-root > section > div > div > section > div.GHEPc > div._-6eR1 > div > div > img"


    let checkExist = setInterval(function() {
        if (document.querySelector(menuPath).tagName) {
            clearInterval(checkExist);
            setInterval(function() {
                if(document.getElementById('dlButton') == null){
                    let dlButton = createButton()
                    let filename = document.querySelector("#react-root > section > div > div > section > header > div > div.MS2JH > div > div > div > a").innerText + " " + document.querySelector("#react-root > section > div > div > section > header > div > div.MS2JH > div > div > time").dateTime + "."

                    dlButton.addEventListener('click', function() {

                        if(document.querySelector(videoPath) != null) {
                            download(document.querySelector(videoPath).children[0].src, filename + "mp4")

                        }
                        else {
                            download(document.querySelector(imagePath).src, filename + "jpg")
                        }
                    })

                    let controls = document.querySelector(menuPath)
                    controls.append(dlButton)
                    console.log("added")
                }
            }, 2000);
        }
    }, 100);


})();

function createButton() {
    let dlButton = document.createElement('button')
    dlButton.classList.add('-jHC6')
    dlButton.id = 'dlButton'
    dlButton.style.top = '16px'

    let iconDiv = document.createElement('div')
    iconDiv.classList.add('coreSpriteSaveStory')
    dlButton.append(iconDiv)

    return dlButton;
}

// credits to https://stackoverflow.com/a/9834261
function download(url, filename) {
 fetch(url)
  .then(resp => resp.blob())
  .then(blob => {
    const url = window.URL.createObjectURL(blob);
    const a = document.createElement('a');
    a.style.display = 'none';
    a.href = url;
    // the filename you want
    a.download = filename;
    document.body.appendChild(a);
    a.click();
    window.URL.revokeObjectURL(url);
  })
  .catch(() => alert('oh no!'));


}