Rumble Download Button

Add a download button for Rumble

目前為 2024-02-11 提交的版本,檢視 最新版本

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Rumble Download Button
// @namespace    http://tampermonkey.net/
// @version      2024-02-11
// @description  Add a download button for Rumble
// @author       Zeek Bower
// @match        https://rumble.com/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=rumble.com
// @grant        none
// @license MIT
// ==/UserScript==

(function() {
    'use strict';

    window.addEventListener("load", function() {
        console.log("load");

function downloadMP4WithProgress() {
    const mp4Url = document.getElementsByTagName("video")[0].currentSrc.split('?')[0];

    fetch(mp4Url)
        .then(response => {
            if (!response.ok) {
                throw new Error('Network response was not ok');
            }
            const totalSize = parseInt(response.headers.get('content-length')); // Get total size of the response
            let loadedBytes = 0;

            const reader = response.body.getReader();

            return new ReadableStream({
                start(controller) {
                    function pump() {
                        reader.read().then(({ done, value }) => {
                            if (done) {
                                controller.close();
                                return;
                            }

                            controller.enqueue(value);
                            loadedBytes += value.byteLength;

                            const progress = loadedBytes / totalSize;
                            const percentage = Math.round(progress * 100);
                            console.log(`Downloaded ${percentage}%`);

                            let dlbutton = window.document.getElementsByClassName("subscribe-buttons")[0].lastChild

                            dlbutton.firstChild.innerText = `Downloaded ${percentage}%`;

                            // Trigger the progress event here if needed

                            pump();
                        }).catch(error => {
                            console.error('Stream read error:', error);
                            controller.error(error);
                        });
                    }

                    pump();
                }
            });
        })
        .then(stream => new Response(stream))
        .then(response => response.blob())
        .then(blob => {
            const url = URL.createObjectURL(blob);
            const link = document.createElement('a');
            link.href = url;
            link.download = document.getElementsByTagName("h1")[0].innerText + ".mp4";
            link.click();
        })
        .catch(error => {
            console.error('Fetch error:', error);
        });
}

function downloadMP4() {
    // URL of the MP4 file
    const mp4Url = document.getElementsByTagName("video")[0].currentSrc.split('?')[0];

    // Fetch the MP4 file
    fetch(mp4Url)
        .then(response => response.blob())
        .then(response => console.log(response))
        .then(blob => {
        // Create a temporary URL for the blob
        const url = URL.createObjectURL(blob);

        // Create a link element
        const link = document.createElement('a');

        // Set the link's href to the URL of the blob
        link.href = url;

        // Set the download attribute to specify the filename
        link.download = document.getElementsByTagName("h1")[0].innerText + ".mp4";
        // Simulate a click on the link to trigger the download
        link.click();

        // Cleanup by revoking the object URL
        URL.revokeObjectURL(url);
    });
}

// Create a button element
const button = document.createElement('button');

// Set the button's text content
button.textContent = 'DOWNLOAD4';
        button.innerHTML = "DOWNLOAD";
        button.class = "round-button bg-purple";
        button.setAttribute("class","round-button bg-purple");

        // Assign the downloadMP4 function to the button's onclick event handler
        button.onclick = downloadMP4WithProgress;

         const theVideo = document.getElementsByTagName("video")[0].currentSrc.split('?')[0];
        console.log(theVideo);
        const link = document.createElement('a');

         const ourDiv = window.document.createElement("div");
         ourDiv.class = "subscribe-buttons";
         ourDiv.appendChild(button);
         const currentDiv = window.document.getElementsByClassName("subscribe-buttons");
         currentDiv[0].appendChild(ourDiv)
                console.log("end");
    });


})();