Open tryrebooting opened 1 week ago
me and ChatGPT worked it out:
Key Updates:
1. Replaced fetch with GM_xmlhttpRequest to bypass CORS restrictions.
2. Added @grant GM_xmlhttpRequest and @connect api.real-debrid.com for permissions.
3. Adjusted the logic to fit the new method.
// ==UserScript== // @name Magnet Link to Real-Debrid Bas test // @namespace http://tampermonkey.net/ // @version 1.6 // @description Automatically send magnet links to Real-Debrid, check for duplicates, and select specific file types // @author Pahiro // @match :///* // @grant GM_xmlhttpRequest // @connect api.real-debrid.com // ==/UserScript==
(function() { 'use strict';
const apiKey = 'xxxxxxxx'; // Replace with your Real-Debrid API key
const allowedExtensions = ['mp3', 'm4b', 'mp4', 'mkv', 'cbz', 'cbr', 'pdf'];
let existingTorrents = [];
// Function to get the hash from a magnet link
function getMagnetHash(magnetLink) {
const magnetUri = new URL(magnetLink);
const hashParam = magnetUri.searchParams.get('xt');
return hashParam ? hashParam.split(':').pop().toUpperCase() : null;
}
// Function to fetch the list of existing torrents from Real-Debrid
function fetchExistingTorrents() {
return new Promise((resolve, reject) => {
GM_xmlhttpRequest({
method: 'GET',
url: 'https://api.real-debrid.com/rest/1.0/torrents',
headers: {
'Authorization': `Bearer ${apiKey}`
},
onload: function(response) {
try {
existingTorrents = JSON.parse(response.responseText);
console.log('Fetched existing torrents:', existingTorrents);
resolve();
} catch (error) {
console.error('Error parsing existing torrents:', error);
reject(error);
}
},
onerror: function(error) {
console.error('Error fetching torrents from Real-Debrid:', error);
reject(error);
}
});
});
}
// Function to check if a torrent already exists in Real-Debrid
function isTorrentInList(magnetHash) {
return existingTorrents.some(torrent => torrent.hash.toUpperCase() === magnetHash);
}
function sendToRealDebrid(magnetLink, icon) {
const magnetHash = getMagnetHash(magnetLink);
if (!magnetHash) {
showTemporaryMessage('Invalid magnet link.', 'red');
return;
}
if (isTorrentInList(magnetHash)) {
showTemporaryMessage('Torrent already exists in Real-Debrid.', 'red');
icon.style.filter = 'hue-rotate(0deg)';
return;
}
// Step 1: Add the magnet link to Real-Debrid
GM_xmlhttpRequest({
method: 'POST',
url: 'https://api.real-debrid.com/rest/1.0/torrents/addMagnet',
headers: {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/x-www-form-urlencoded'
},
data: `magnet=${encodeURIComponent(magnetLink)}`,
onload: function(addMagnetResponse) {
try {
const addMagnetData = JSON.parse(addMagnetResponse.responseText);
const torrentId = addMagnetData.id;
// Step 2: Retrieve the list of files in the torrent
GM_xmlhttpRequest({
method: 'GET',
url: `https://api.real-debrid.com/rest/1.0/torrents/info/${torrentId}`,
headers: {
'Authorization': `Bearer ${apiKey}`
},
onload: function(torrentInfoResponse) {
const torrentInfoData = JSON.parse(torrentInfoResponse.responseText);
const files = torrentInfoData.files;
// Step 3: Filter the files by specific extensions
const selectedFiles = files
.filter(file => allowedExtensions.includes(file.path.split('.').pop().toLowerCase()))
.map(file => file.id)
.join(',');
// Step 4: Select the filtered files in the torrent
if (selectedFiles.length > 0) {
GM_xmlhttpRequest({
method: 'POST',
url: `https://api.real-debrid.com/rest/1.0/torrents/selectFiles/${torrentId}`,
headers: {
'Authorization': `Bearer ${apiKey}`,
'Content-Type': 'application/x-www-form-urlencoded'
},
data: `files=${selectedFiles}`,
onload: function() {
showTemporaryMessage('Magnet link added and files selected in Real-Debrid!', 'green');
icon.style.filter = 'invert(18%) sepia(88%) saturate(7485%) hue-rotate(357deg) brightness(103%) contrast(105%)';
}
});
} else {
showTemporaryMessage('No files matched the selected extensions.', 'red');
}
}
});
} catch (error) {
console.error('Error processing addMagnet response:', error);
showTemporaryMessage('Failed to send magnet link to Real-Debrid.', 'red');
}
},
onerror: function(error) {
console.error('Error adding magnet link to Real-Debrid:', error);
showTemporaryMessage('Failed to send magnet link to Real-Debrid.', 'red');
}
});
}
// Function to show a temporary message
function showTemporaryMessage(message, color) {
const msgDiv = document.createElement('div');
msgDiv.textContent = message;
msgDiv.style.position = 'fixed';
msgDiv.style.bottom = '20px';
msgDiv.style.left = '20px';
msgDiv.style.backgroundColor = color;
msgDiv.style.color = 'white';
msgDiv.style.padding = '10px';
msgDiv.style.borderRadius = '5px';
msgDiv.style.zIndex = 10000;
document.body.appendChild(msgDiv);
// Automatically remove the message after 3 seconds
setTimeout(() => {
msgDiv.remove();
}, 3000);
}
// Function to create a send icon next to the magnet link
function createSendIcon(link) {
const icon = document.createElement('img');
icon.src = 'https://fcdn.real-debrid.com/0830/favicons/favicon.ico'; // Real-Debrid icon
icon.style.cursor = 'pointer';
icon.style.marginLeft = '5px';
icon.style.width = '16px';
icon.style.height = '16px';
icon.addEventListener('click', () => {
sendToRealDebrid(link.href, icon);
});
link.parentNode.insertBefore(icon, link.nextSibling);
}
async function main() {
const magnetLinks = document.querySelectorAll('a[href*="magnet:"]');
if (magnetLinks.length > 0) {
await fetchExistingTorrents();
magnetLinks.forEach(createSendIcon);
} else {
console.log('No magnet links found on the page.');
}
}
main(); // Call the main function
})();
Thank you for the reply. Sorry, I am not a coder, I am not sure if this is something I should do, or will there be a new release version that includes any new fixes?
Not a coder either my man, if you want it to work again then you should use the above code ;)
Thanks do I copy this entire code into the existing magnet script?
Will there be a new version release of the userscript posted in GitHub?
On Thu, Nov 28, 2024, 09:51 TechXXX @.***> wrote:
Not a coder either my man, if you want it to work again then you should use the above code ;)
— Reply to this email directly, view it on GitHub https://github.com/Pahiro/Magnet-to-RD/issues/1#issuecomment-2506297604, or unsubscribe https://github.com/notifications/unsubscribe-auth/AHU5JKNW5ASNULQ7CMXC64L2C4UY5AVCNFSM6AAAAABSKYNEOGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDKMBWGI4TONRQGQ . You are receiving this because you authored the thread.Message ID: @.***>
Easiest is to just make a new one in tampermonkey, paste the new code in there (I put a copy in hastebin for you in case of the formatting being off https://hastebin.skyra.pw/mufixodido.ts ) . Then copy your API from the old to the new code, turn the old one off in tampermonkey dashboard and you're done. No idea if the dev ([Pahiro])will see this and make a new version, I thought since I was using his script for so long I should post the solution/update on here.
As of 11/22 it seems that all attempts to use Magnet-to-RD are resulting in ""Failed to send magnet link to Real-Debrid." Using version 1.3