@BMmini

Telegtam Video DL

이 스크립트는 직접 설치하는 용도가 아닙니다. 다른 스크립트에서 메타 지시문 // @require https://update.sleazyfork.org/scripts/591220/1901744/%40BMmini.js을(를) 사용하여 포함하는 라이브러리입니다.

이 스크립트를 설치하려면 Tampermonkey, Greasemonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

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

이 스크립트를 설치하려면 Tampermonkey 또는 Violentmonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey 또는 Userscripts와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 Tampermonkey와 같은 확장 프로그램이 필요합니다.

이 스크립트를 설치하려면 유저 스크립트 관리자 확장 프로그램이 필요합니다.

(이미 유저 스크립트 관리자가 설치되어 있습니다. 설치를 진행합니다!)

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 Stylus와 같은 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

이 스타일을 설치하려면 유저 스타일 관리자 확장 프로그램이 필요합니다.

(이미 유저 스타일 관리자가 설치되어 있습니다. 설치를 진행합니다!)

// ==UserScript==
// @name         Telegram Web K Video Downloader
// @namespace    https://tampermonkey.net/
// @version      1.0
// @description  เพิ่มปุ่มดาวน์โหลดวิดีโอบน Telegram Web K (ดักจับไฟล์วิดีโอที่กำลังเล่น)
// @author       You
// @match        https://webk.telegram.org/*
// @grant        GM_download
// @grant        GM_xmlhttpRequest
// ==UserScript==

(function() {
    'use strict';

    // ฟังก์ชันสร้างปุ่มโหลด
    function createDownloadButton(videoElem) {
        if (videoElem.dataset.hasDlBtn) return;
        videoElem.dataset.hasDlBtn = "true";

        const btn = document.createElement('button');
        btn.innerText = '⬇️ โหลดวิดีโอนี้';
        btn.style.cssText = `
            position: absolute;
            top: 10px;
            right: 10px;
            z-index: 9999;
            background: #0088cc;
            color: white;
            border: none;
            padding: 8px 12px;
            border-radius: 6px;
            cursor: pointer;
            font-weight: bold;
            box-shadow: 0 2px 5px rgba(0,0,0,0.3);
        `;

        btn.addEventListener('click', async (e) => {
            e.stopPropagation();
            e.preventDefault();
            
            const src = videoElem.src || videoElem.currentSrc;
            if (!src) {
                alert('ไม่พบลิงก์วิดีโอ กรุณากดเล่นวิดีโอก่อนครับ');
                return;
            }

            btn.innerText = '⏳ กำลังเตรียมดาวน์โหลด...';

            try {
                // หากเป็น blob URL
                const response = await fetch(src);
                const blob = await response.blob();
                const blobUrl = URL.createObjectURL(blob);
                
                const a = document.createElement('a');
                a.href = blobUrl;
                a.download = `TG_Video_${Date.now()}.mp4`;
                document.body.appendChild(a);
                a.click();
                document.body.removeChild(a);
                URL.revokeObjectURL(blobUrl);

                btn.innerText = '✅ ดาวน์โหลดสำเร็จ';
                setTimeout(() => { btn.innerText = '⬇️ โหลดวิดีโอนี้'; }, 3000);
            } catch (err) {
                console.error(err);
                alert('เกิดข้อผิดพลาดในการดึงไฟล์วิดีโอ');
                btn.innerText = '❌ ล้มเหลว';
            }
        });

        // แปะปุ่มลงใน Container ของ Video
        const parent = videoElem.parentElement;
        if (parent) {
            parent.style.position = 'relative';
            parent.appendChild(btn);
        }
    }

    // ตรวจหาแท็ก <video> ในหน้าเว็บสม่ำเสมอ
    const observer = new MutationObserver(() => {
        const videos = document.querySelectorAll('video');
        videos.forEach(video => {
            if (video.src || video.currentSrc) {
                createDownloadButton(video);
            }
        });
    });

    observer.observe(document.body, { childList: true, subtree: true });
})();