MS Downloader

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

目前為 2025-01-31 提交的版本,檢視 最新版本

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

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

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

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

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

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

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==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 = true; // Par défaut, les boutons DL sont visibles

    // Fonction pour créer un bouton de téléchargement
    function createDownloadButton(url) {
        let button = document.createElement("button");
        button.innerText = "DL";
        button.style.position = "absolute";
        button.style.top = "5px";
        button.style.right = "5px";
        button.style.backgroundColor = "rgba(0, 0, 0, 0.7)";
        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.opacity = "0.8";
        button.style.transition = "opacity 0.2s";
        button.style.display = autoShowButtons ? "block" : "none";

        // Effet de survol pour rendre le bouton plus visible
        button.onmouseover = () => button.style.opacity = "1";
        button.onmouseout = () => button.style.opacity = "0.8";

        // Action du bouton pour télécharger la vidéo
        button.onclick = () => {
            let a = document.createElement("a");
            a.href = url;
            a.download = "video.mp4"; // Nom générique pour le fichier
            document.body.appendChild(a);
            a.click();
            document.body.removeChild(a);
        };

        return button;
    }

    // Fonction pour ajouter le bouton de téléchargement uniquement pour les vidéos valides
    function addDownloadButtons() {
        // Sélectionner uniquement les éléments vidéo avec une source
        document.querySelectorAll("video").forEach(video => {
            if (!video.dataset.hasDownloadButton && video.src) {
                // Vérifie si la source vidéo est valide
                if (video.src && (video.src.includes(".mp4") || video.src.includes(".webm"))) { // Format MP4 ou WebM
                    let button = createDownloadButton(video.src);
                    video.style.position = "relative";
                    video.parentNode.appendChild(button);
                    video.dataset.hasDownloadButton = "true";
                }
            }
        });
        
        // Vérification pour les vidéos intégrées (comme celles de YouTube)
        document.querySelectorAll("iframe").forEach(iframe => {
            if (!iframe.dataset.hasDownloadButton && iframe.src && iframe.src.includes("youtube.com") && iframe.src.includes("watch")) {
                // Extrait l'URL de la vidéo YouTube
                let url = iframe.src.replace("https://www.youtube.com/embed/", "https://www.youtube.com/watch?v=");
                let button = createDownloadButton(url);
                iframe.style.position = "relative";
                iframe.parentNode.appendChild(button);
                iframe.dataset.hasDownloadButton = "true";
            }
        });
    }

    // Observer pour les vidéos chargées dynamiquement (scroll infini)
    const observer = new MutationObserver(addDownloadButtons);
    observer.observe(document.body, { childList: true, subtree: true });

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

    console.log("Script DL activé :\n[D] Afficher/Cacher les boutons de téléchargement");
})();