Skip to content

Instantly share code, notes, and snippets.

@aiexz
Created July 28, 2025 13:22
Show Gist options
  • Save aiexz/6c44f643981c3066ff047b86e5fba1a4 to your computer and use it in GitHub Desktop.
Save aiexz/6c44f643981c3066ff047b86e5fba1a4 to your computer and use it in GitHub Desktop.
Userscript to add checkboxes to thepiratebay and download all selected files at once
// ==UserScript==
// @name The Pirate Bay - Multi-Magnet Downloader
// @namespace http://tampermonkey.net/
// @version 1.0
// @description Adds checkboxes and buttons to copy or download multiple magnet links at once on The Pirate Bay.
// @author Gemini
// @match https://thepiratebay.org/search.php*
// @match https://thepiratebay.org/browse.php*
// @match https://thepiratebay.org/top.php*
// @icon https://thepiratebay.org/favicon.ico
// @grant none
// ==/UserScript==
(function() {
'use strict';
// --- 1. Find the necessary elements on the page ---
const torrentList = document.getElementById('torrents');
const filterSection = document.querySelector('section.col-center');
// Exit if the page structure isn't what we expect
if (!torrentList || !filterSection) {
console.warn('TPB Multi-Magnet: Required page elements not found.');
return;
}
// --- 2. Create and inject the control panel with buttons ---
const controlPanel = document.createElement('div');
controlPanel.id = 'tpb-multi-dl-panel';
controlPanel.style.cssText = `
margin-top: 15px;
padding: 8px;
border: 1px solid #777;
border-radius: 4px;
background-color: #f4f4f4;
`;
// "Select All" checkbox
const masterCheckbox = document.createElement('input');
masterCheckbox.type = 'checkbox';
masterCheckbox.title = 'Select or Deselect All';
masterCheckbox.style.marginRight = '5px';
const masterLabel = document.createElement('label');
masterLabel.appendChild(masterCheckbox);
masterLabel.appendChild(document.createTextNode('Select All'));
masterLabel.style.marginRight = '20px';
masterLabel.style.fontWeight = 'bold';
// "Copy" button
const copyButton = document.createElement('button');
copyButton.textContent = 'Copy Selected Magnets';
copyButton.style.marginRight = '10px';
// "Download as File" button
const downloadButton = document.createElement('button');
downloadButton.textContent = 'Download Selected as File';
// Add controls to the panel and inject it into the page
controlPanel.appendChild(masterLabel);
controlPanel.appendChild(copyButton);
controlPanel.appendChild(downloadButton);
filterSection.appendChild(controlPanel);
// --- 3. Add a checkbox to each torrent result ---
const torrentEntries = torrentList.querySelectorAll('li.list-entry');
torrentEntries.forEach(entry => {
const checkbox = document.createElement('input');
checkbox.type = 'checkbox';
checkbox.className = 'tpb-multi-dl-checkbox';
checkbox.style.cssText = `
margin-right: 8px;
vertical-align: middle;
transform: scale(1.2);
`;
const typeSpan = entry.querySelector('span.item-type');
if (typeSpan) {
// Add the checkbox to the beginning of the row
typeSpan.prepend(checkbox);
}
});
// --- 4. Define helper function to get selected links ---
const getSelectedMagnets = () => {
const selectedMagnets = [];
const checkedBoxes = document.querySelectorAll('.tpb-multi-dl-checkbox:checked');
checkedBoxes.forEach(cb => {
const row = cb.closest('li.list-entry');
const magnetLink = row.querySelector('a[href^="magnet:"]');
if (magnetLink) {
selectedMagnets.push(magnetLink.href);
}
});
return selectedMagnets;
};
// --- 5. Add event listeners for all controls ---
// Logic for "Select All" checkbox
masterCheckbox.addEventListener('change', (event) => {
const isChecked = event.target.checked;
document.querySelectorAll('.tpb-multi-dl-checkbox').forEach(cb => {
cb.checked = isChecked;
});
});
// Logic for "Copy" button
copyButton.addEventListener('click', () => {
const magnets = getSelectedMagnets();
if (magnets.length === 0) {
alert('No torrents selected.');
return;
}
const magnetString = magnets.join('\n');
navigator.clipboard.writeText(magnetString).then(() => {
const originalText = copyButton.textContent;
copyButton.textContent = `✅ Copied ${magnets.length} link(s)!`;
setTimeout(() => {
copyButton.textContent = originalText;
}, 2500);
}).catch(err => {
console.error('Failed to copy magnet links: ', err);
alert('Could not copy links to clipboard. Please check browser permissions or console for details.');
});
});
// Logic for "Download" button
downloadButton.addEventListener('click', () => {
const magnets = getSelectedMagnets();
if (magnets.length === 0) {
alert('No torrents selected.');
return;
}
const magnetString = magnets.join('\n');
const blob = new Blob([magnetString], { type: 'text/plain;charset=utf-8' });
const url = URL.createObjectURL(blob);
const link = document.createElement('a');
link.href = url;
link.download = 'tpb_magnets.txt';
document.body.appendChild(link);
link.click();
// Clean up
document.body.removeChild(link);
URL.revokeObjectURL(url);
});
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment