Send to Real-Debrid

Add a button next to each magnet link to send it to Real-Debrid, with user-friendly API token setup and confirmation

Verze ze dne 16. 01. 2024. Zobrazit nejnovější verzi.

K instalaci tototo skriptu si budete muset nainstalovat rozšíření jako Tampermonkey, Greasemonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Violentmonkey.

K instalaci tohoto skriptu si budete muset nainstalovat rozšíření jako Tampermonkey nebo Userscripts.

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

K instalaci tohoto skriptu si budete muset nainstalovat manažer uživatelských skriptů.

(Už mám manažer uživatelských skriptů, nechte mě ho nainstalovat!)

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.

(Už mám manažer uživatelských stylů, nechte mě ho nainstalovat!)

// ==UserScript==
// @name         Send to Real-Debrid
// @namespace    http://tampermonkey.net/
// @version      0.2.2
// @description  Add a button next to each magnet link to send it to Real-Debrid, with user-friendly API token setup and confirmation
// @author       dlee2
// @license      MIT
// @match        *://*.nyaa.si/*
// @grant        GM_xmlhttpRequest
// @grant        GM_setValue
// @grant        GM_getValue
// ==/UserScript==

(function () {
    'use strict';

    // Function to get or ask for the API token
    function getApiToken() {
        let apiToken = GM_getValue('apiToken');
        if (!apiToken) {
            const instructions = 'To use this script, you need to enter your Real-Debrid API token.\n' +
                'You can find your API token in your Real-Debrid account settings.\n\n' +
                'Please enter your Real-Debrid API token:';
            apiToken = prompt(instructions, '');

            if (apiToken) {
                GM_setValue('apiToken', apiToken);
            } else {
                alert('No API token provided. You can set it later by clicking a "Send to RD" button.');
                return null;
            }
        }
        return apiToken;
    }

    // Function to send magnet link to Real-Debrid
    function sendToRealDebrid(magnetLink) {
        const API_TOKEN = getApiToken();
        if (!API_TOKEN) {
            alert("Operation canceled: API token is not set.");
            return;
        }

        GM_xmlhttpRequest({
            method: "POST",
            url: "https://api.real-debrid.com/rest/1.0/torrents/addMagnet",
            headers: {
                "Authorization": `Bearer ${API_TOKEN}`,
                "Content-Type": "application/x-www-form-urlencoded"
            },
            data: "magnet=" + encodeURIComponent(magnetLink),
            onload: function (response) {
                var responseJson = JSON.parse(response.responseText);
                if (responseJson.id) {
                    alert("Magnet link successfully sent to Real-Debrid!");
                } else {
                    console.error('Error adding magnet link:', response.responseText);
                    alert("Failed to send magnet link to Real-Debrid.");
                }
            },
            onerror: function (response) {
                console.error('Error sending request to Real-Debrid:', response.responseText);
                alert("Error occurred while sending request to Real-Debrid.");
            }
        });
    }

    // Function to create a send button next to a magnet link
    function createSendButton(magnetLink) {
        let button = document.createElement('button');
        button.textContent = 'Send to RD';
        button.style.marginLeft = '10px';
        button.addEventListener('click', function () {
            sendToRealDebrid(magnetLink);
        });

        return button;
    }

    // Add send buttons next to all magnet links
    function addSendButtons() {
        let magnetLinks = document.querySelectorAll('a[href^="magnet:"]');
        magnetLinks.forEach(link => {
            let sendButton = createSendButton(link.href);
            link.parentNode.insertBefore(sendButton, link.nextSibling);
        });
    }

    // Run the function to add send buttons
    addSendButtons();
})();