MS Downloader

Ajoute un bouton de téléchargement sous les vidéos et images sur YouTube, Instagram, Twitter et TikTok

当前为 2025-01-31 提交的版本,查看 最新版本

// ==UserScript==
// @name         MS Downloader
// @namespace    https://greasyfork.org/users/1429467
// @version      1.0
// @description  Ajoute un bouton de téléchargement sous les vidéos et images sur YouTube, Instagram, Twitter et TikTok
// @author       Lakfu Sama
// @match        *://*.youtube.com/*
// @match        *://*.instagram.com/*
// @match        *://*.twitter.com/*
// @match        *://*.tiktok.com/*
// @match        *://*/*
// @grant        none
// ==/UserScript==

(function() {
    'use strict';

    let autoShowButtons = false; // False = les boutons ne s'affichent pas par défaut

    function createDownloadButton(url) {
        let button = document.createElement("button");
        button.innerText = "⬇️ Télécharger";
        button.style.position = "absolute";
        button.style.bottom = "5px";
        button.style.right = "5px";
        button.style.backgroundColor = "#ff4500";
        button.style.color = "white";
        button.style.border = "none";
        button.style.padding = "5px 10px";
        button.style.borderRadius = "5px";
        button.style.cursor = "pointer";
        button.style.fontSize = "12px";
        button.style.zIndex = "1000";
        button.style.display = autoShowButtons ? "block" : "none"; // Cache par défaut si autoShowButtons = false

        button.onclick = () => {
            let a = document.createElement("a");
            a.href = url;
            a.download = url.split('/').pop(); // Génère un nom de fichier automatique
            document.body.appendChild(a);
            a.click();
            document.body.removeChild(a);
        };

        return button;
    }

    function isValidImage(img) {
        let width = img.naturalWidth;
        let height = img.naturalHeight;

        // Exclure les trop petites images (icônes, emojis, avatars)
        return width > 50 && height > 50; 
    }

    function addDownloadButtons() {
        document.querySelectorAll("img, video").forEach(media => {
            if (!media.dataset.hasDownloadButton && isValidImage(media)) {
                let url = media.src;
                if (url) {
                    let button = createDownloadButton(url);
                    media.style.position = "relative";
                    media.parentNode.appendChild(button);
                    media.dataset.hasDownloadButton = "true";
                }
            }
        });
    }

    // Observer pour détecter les nouveaux éléments (scroll infini)
    const observer = new MutationObserver(addDownloadButtons);
    observer.observe(document.body, { childList: true, subtree: true });

    // Touche "D" pour afficher/cacher les boutons manuellement
    document.addEventListener('keydown', function(event) {
        if (event.key.toLowerCase() === 'd') {
            autoShowButtons = !autoShowButtons;
            document.querySelectorAll("button").forEach(button => {
                if (button.innerText.includes("Télécharger")) {
                    button.style.display = autoShowButtons ? "block" : "none";
                }
            });
        }
    });

    console.log("Téléchargement Auto amélioré chargé :\n[D] Activer/Désactiver les boutons de téléchargement");
})();