Image Downloader with Format Option

Download images in different formats (PNG, JPEG, WEBP) from any website.

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Image Downloader with Format Option
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Download images in different formats (PNG, JPEG, WEBP) from any website.
// @author       GMbox
// @match        *://*/*
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    const formats = ['png', 'jpeg', 'webp'];

    function createDownloadButton(img) {
        const btn = document.createElement('button');
        btn.textContent = '↓';
        btn.style.position = 'absolute';
        btn.style.zIndex = 9999;
        btn.style.top = '5px';
        btn.style.right = '5px';
        btn.style.padding = '2px 5px';
        btn.style.backgroundColor = '#000';
        btn.style.color = '#fff';
        btn.style.border = 'none';
        btn.style.cursor = 'pointer';
        btn.style.fontSize = '12px';
        btn.style.borderRadius = '4px';

        btn.addEventListener('click', async (e) => {
            e.stopPropagation();
            e.preventDefault();
            const format = prompt("Choose format: png, jpeg, webp", "png");
            if (!formats.includes(format)) {
                alert("Unsupported format");
                return;
            }

            const canvas = document.createElement('canvas');
            const context = canvas.getContext('2d');
            canvas.width = img.naturalWidth;
            canvas.height = img.naturalHeight;
            context.drawImage(img, 0, 0);

            canvas.toBlob((blob) => {
                const a = document.createElement('a');
                a.href = URL.createObjectURL(blob);
                a.download = `image.${format}`;
                document.body.appendChild(a);
                a.click();
                a.remove();
            }, `image/${format}`);
        });

        return btn;
    }

    function wrapImage(img) {
        if (img.closest('.image-download-wrapper')) return;

        const wrapper = document.createElement('div');
        wrapper.style.position = 'relative';
        wrapper.style.display = 'inline-block';
        wrapper.classList.add('image-download-wrapper');

        const clonedImg = img.cloneNode(true);
        img.parentNode.replaceChild(wrapper, img);
        wrapper.appendChild(clonedImg);

        const btn = createDownloadButton(clonedImg);
        wrapper.appendChild(btn);
    }

    function observeImages() {
        document.querySelectorAll('img').forEach(wrapImage);
    }

    new MutationObserver(observeImages).observe(document.body, {
        childList: true,
        subtree: true
    });

    window.addEventListener('load', observeImages);
})();