Image Downloader with Format Option

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

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey, το Greasemonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Violentmonkey για να εγκαταστήσετε αυτόν τον κώδικα.

θα χρειαστεί να εγκαταστήσετε μια επέκταση όπως το Tampermonkey ή το Userscripts για να εγκαταστήσετε αυτόν τον κώδικα.

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

Θα χρειαστεί να εγκαταστήσετε μια επέκταση διαχείρισης κώδικα χρήστη για να εγκαταστήσετε αυτόν τον κώδικα.

(Έχω ήδη έναν διαχειριστή κώδικα χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install an extension such as Stylus to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

You will need to install a user style manager extension to install this style.

(Έχω ήδη έναν διαχειριστή στυλ χρήστη, επιτρέψτε μου να τον εγκαταστήσω!)

// ==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);
})();