Greasy Fork

Greasy Fork is available in English.

Deviantart better downloaded file names (public version)

Deviantart

目前为 2023-12-05 提交的版本,查看 最新版本

// ==UserScript==
// @name         Deviantart better downloaded file names (public version)
// @namespace    http://tampermonkey.net/
// @version      1.2
// @description  Deviantart
// @author       TeshRoa
// @include http://deviantart.com/*
// @include https://deviantart.com/*
// @include http://*.deviantart.com/*
// @include https://*.deviantart.com/*
// @match http://deviantart.com/*
// @match https://deviantart.com/*
// @match http://*.deviantart.com/*
// @match https://*.deviantart.com/*
// @grant        GM_download
// ==/UserScript==

(function() {
    console.log("V1.0");
    var basepath = "" //OPTIONAL base file path for INSIDE THE DOWNLOADS FOLDER!!!!! does not work outside the downloads folder of the browser; for ex "lorem/ipsum/" refers to the folder [browser-download-folder]/lorem/ipsum/
    //var basepath ="Lorem/Ipsum/"
    var path = ""; //blank variable, do not change at this line!

window.addEventListener ("keydown", keyboardHandler, false); //for detecting pressed keys

    function keyboardHandler (zEvent) {
        var bBlockDefaultAction = false;
        if (zEvent.key == "F2") { //pressed key(s)
            bBlockDefaultAction = true; //suppress normal function of pressed key
            console.log("F2 was pressed"); //log key press for debugging
            path = basepath + ""; //OPTIONAL further specification of file path
            saveimage(); //function for saving the img
        }
        else if (zEvent.ctrlKey & zEvent.key == "s") { //same function as previous code block but for other keys
            bBlockDefaultAction = true;
            console.log("ctrl+s was pressed");
            path = basepath + "";
            saveimage();
        }
        if (bBlockDefaultAction) {
            zEvent.preventDefault ();
            zEvent.stopPropagation ();
        }
    }
function saveimage() {
    //retrieve the url based on datahook
    var artstage = document.querySelectorAll("div[data-hook=art_stage]"); //select correct div based on data-hook attribute
    var url = artstage[0].querySelector("img[src]").getAttribute("src"); //select img element in the found div
    console.log("the url has been retrieved");
    console.log(url);
    //retrieve the desc
    var metadata = document.querySelectorAll("div[data-hook=deviation_meta]");
    var desc = metadata[0].querySelector("h1[data-hook=deviation_title]").textContent; //get the text of the element
    console.log("the description has been retrieved");
    var patt1 = /[:|<>?\/*~]/g;
    desc = desc.replace(patt1,"_");//replace illegal file characters
    console.log(desc);
    //retrieve the artist
    var artist = metadata[0].querySelector("a[data-hook=user_link]").getAttribute("data-username");
    console.log("the artist has been retrieved");
    console.log(artist);
    //saveAs dialog window details
    var arg = {
        url: url,
        name:  path + desc + " By " + artist + ".jpg", //correct file extension is automatically chosen by the saveas dialog for some reason when we specify a single extension
        saveAs: true,
        onerror: function(download) {
					console.log(name + " > GM_download > onerror.error:", download.error);
					console.log(name + " > GM_download > onerror.details:", download.details);
					console.log(name + " > GM_download > onerror.details.current:", download.details);
				},
    };
    var result= GM_download(arg);
    path = ""; //reset path
}
})();