ProxyFiend / Hentai Foundry Downloader

// ==UserScript==
// @name         Hentai Foundry Downloader
// @namespace    ProxyFiend.HentaiFoundryDownloader
// @author       ProxyFiend
// @prefix       HFD
// @copyright    2018, ProxyFiend (https://twitter.com/ProxyFiend)
// @license      MIT
// @version      1.1
// @description  A script to add Ctrl + S support to Hentai Foundry, as well as a convenient download button.
// @include      *://*.hentai-foundry.com/pictures/user/*
// @updateURL    https://openuserjs.org/meta/ProxyFiend/Hentai_Foundry_Downloader.meta.js
// @require      https://cdnjs.cloudflare.com/ajax/libs/jquery/3.2.1/jquery.min.js
// @require      https://cdnjs.cloudflare.com/ajax/libs/FileSaver.js/1.3.8/FileSaver.min.js
// @require      https://gist.githubusercontent.com/FlandreDaisuki/8970080786187e0333154b54869d079e/raw/abc01526232e05368bf8c75165d8d14606674f41/GM_download_polyfill.js
// @grant        GM_download
// @grant        GM_xmlhttpRequest
// ==/UserScript==

// ==OpenUserJS==
// @author ProxyFiend
// ==/OpenUserJS==

(function () {
    'use strict';

    if (!$("#picBox")[0]) {
        return 0;
    }

    const regex = /\/user\/(.*)\/(\d*)\/(.*)/g;
    let m;

    m = regex.exec(window.location.href);

    var imageFilename = m[1] + "/" + m[2] + "/" + m[1] + "-" + m[2] + "-" + m[3] + ".jpg"
    var imageUrl = "https://pictures.hentai-foundry.com/" + m[1].substring(0, 1).toLowerCase() + "/" + imageFilename;

    var downloadButton = $("<a/>")
        .attr("id", "downloadBtn")
        .attr("class", "linkButton picButton")
        .attr("title", "Download Image")
        .text("⇩ DOWNLOAD IMAGE");

    $("#picBox>div.boxfooter").append(downloadButton);

    downloadButton.click(function() {
        GM_download(imageUrl, imageFilename)
    });

    $(window).bind('keydown', function (event) {
        if (event.ctrlKey || event.metaKey) {
            switch (String.fromCharCode(event.which).toLowerCase()) {
                case 's':
                    event.preventDefault();
                    downloadButton.click();
                    break;
            }
        }
    });
})();