Image Downloader with Format Option

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey, Greasemonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल.

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला Tampermonkey किंवा Violentmonkey यासारखे एक्स्टेंशन इंस्टॉल करावे लागेल..

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

ही स्क्रिप्ट इंस्टॉल करण्यासाठी तुम्हाला 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);
})();