Image Download with Ctrl+Click/Shift/MMB [GLOBAL]

Image downloading for most single-image pages, with Ctrl+Click, Shift or MMB-Click combinations

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name            Image Download with Ctrl+Click/Shift/MMB [GLOBAL]
// @namespace       imageSaving
// @description     Image downloading for most single-image pages, with Ctrl+Click, Shift or MMB-Click combinations
// @author          NightLancerX
// @match           *://*/*.jpg*
// @match           *://*/*.png*
// @match           *://*/*.jpeg*
// @match           *://*/*.gif*
// @version         2.5
// @homepageURL     https://github.com/NightLancer/PixivPreview
// @license         MIT License
// @grant           none
// @run-at          document-end
// ==/UserScript==

(function() {
    'use strict';

    let img = document.querySelectorAll('img')[0];
    let imgSrc = img.src;

    let main = function()
    {
        let anchor = document.createElement('a'), name;
        name = (imgSrc.indexOf('?')>-1)? imgSrc.substring(imgSrc.lastIndexOf("/")+1, imgSrc.indexOf('?')): imgSrc.substring(imgSrc.lastIndexOf("/")+1);
        try {name = decodeURI(name);} catch(e){};
        anchor.href = img.src;
        anchor.target = '_self';
        anchor.download = name;
        document.body.appendChild(anchor);
        anchor.click();
        main = ()=>{};
    };

    //save with Ctrl+Click
    img.onclick = function(e){
        if (e.ctrlKey){
            e.preventDefault();
            main();
        }
    };

    //save with Shift
    document.onkeyup = function(e){
        if (e.keyCode == 16){
            main();
        }
    };

    //save with MMB-click
    img.onmouseup = function(e){
        if (e.button == 1){
            main();
        }
    };
})();