ProxyFiend / TumblrDown

// ==UserScript==
// @name         TumblrDown
// @namespace    ProxyFiend.TumblrDown
// @author       ProxyFiend
// @copyright    2017, ProxyFiend (https://twitter.com/ProxyFiend)
// @license      Mozilla Public License 2.0; https://opensource.org/licenses/MPL-2.0
// @version      1.1
// @description  Allows you to download posts of nearly any type.
// @include      /^.*:\/\/[^\/]*\/post\/(\d{12}).*/
// @updateURL    https://openuserjs.org/meta/ProxyFiend/TumblrDown.meta.js
// @downloadURL  https://openuserjs.org/src/scripts/ProxyFiend/TumblrDown.user.js
// @require      https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js
// @require      https://openuserjs.org/src/libs/ProxyFiend/ProxyFiendLib.js
// @grant        GM_download
// @grant        GM_xmlhttpRequest
// @noframes
// ==/UserScript==

(function () {
    'use strict';

    const regex = /^.*:\/\/[^\/]*\/post\/(\d{12}).*/g;
    let m;

    m = regex.exec(window.location);

    $(window).bind('keydown', function (event) {
        if (event.ctrlKey || event.metaKey) {
            switch (String.fromCharCode(event.which).toLowerCase()) {
                case 's':
                    event.preventDefault();
                    GM_debug("Trying to download.");
                    downloadPost();
                    break;
            }
        }
    });

    var downloadPost = function () {
        GM_getJSON({
            url: "https://api.tumblr.com/v2/blog/" + window.location.host + "/posts",
            data: {
                api_key: "TCOBlLpiC1YV0J3pwZbihgm1I3bxD4tSdp5J5gpkxwzNClVuI4",
                id: m[1]
            },
            onload: function (json, raw) {
                json.response.posts.forEach(function (post) {
                    switch (post.type) {
                        case "photo":
                            downloadPostPhotos(post);
                            break;
                        case "video":
                            downloadPostVideo(post);
                            break;
                    }
                });
            }
        });
    };

    var downloadPostPhotos = function (post) {
        post.photos.forEach(function (photo) {
            GM_debug("Downloading \"" + photo.original_size.url + "\" as \"" + post.blog_name + "-" + post.id + "-" + post.photos.indexOf(photo) + "." + (photo.original_size.url.substring(photo.original_size.url.lastIndexOf(".") + 1)) + "\"");
            GM_download({
                url: photo.original_size.url,
                name: post.blog_name + "-" + post.id + "-" + post.photos.indexOf(photo) + "." + (photo.original_size.url.substring(photo.original_size.url.lastIndexOf(".") + 1))
            });
        });
    };

    var downloadPostVideo = function (post) {
        GM_debug("Downloading \"" + post.video_url + "\" as \"" + post.blog_name + "-" + post.id + "." + (post.video_url.substring(post.video_url.lastIndexOf(".") + 1)) + "\"");
        GM_download({
            url: post.video_url,
            name: post.blog_name + "-" + post.id + "." + (post.video_url.substring(post.video_url.lastIndexOf(".") + 1))
        });
    };
})();