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

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

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

You will need to install an extension such as Tampermonkey to install this script.

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 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();
        }
    };
})();