Proxy TPB Torrent file URL

Get the direct URL to the torrent file from itorrents.org!

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

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

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

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

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

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

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

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

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

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

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

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

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

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

// ==UserScript==
// @name         Proxy TPB Torrent file URL
// @namespace    https://github.com/joex92/TBP-Torrent-File-URL-Linker/
// @version      2025-11-15
// @description  Get the direct URL to the torrent file from itorrents.org!
// @author       JoeX92
// @match        https://*/torrent/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=thepiratebay.org
// @grant        GM.setClipboard
// @grant        GM_openInTab
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    // Wait for the window to finish loading all content
    window.addEventListener('load', function() {
        runLinker();
    }, false);

    function runLinker() {
        // --- 1. Find the Source Information ---
        // TODO: Change this selector to find your source element.
        // Use your browser's "Inspect" tool to find a unique ID or class.
        const sourceName = document.querySelector('#title');

        // Check if the element was found
        if (!sourceName) {
            console.error('Linker Script: Could not find the title element.');
            return;
        }

        // Get the text from the source element and clean it up
        const sourceNameInfo = sourceName.innerText.trim();

        const sourceHash = document.querySelector('.col2');

        // Check if the element was found
        if (!sourceHash) {
            console.error('Linker Script: Could not find the hash element.');
            return;
        }

        // Get the text from the source element and clean it up
        const sourceHashInfo = sourceHash.innerText.match(/Info Hash:\s*([A-Fa-f0-9]{40})/)[1].trim();

        // --- 2. Find the Target Element ---
        // TODO: Change this selector to find the element you want to replace.
        const targetElement = document.querySelector('#title');

        // Check if the target element was found
        if (!targetElement) {
            console.error('Linker Script: Could not find the target element.');
            return;
        }

        // --- 3. Build the New URL ---
        // TODO: Change this URL structure to match what you need.
        // I'm using a template literal (backticks ``) to insert the variable.
        const newURL = `http://itorrents.net/torrent/${sourceHashInfo}.torrent?title=${encodeURIComponent(sourceNameInfo)}`;
        console.log(newURL);

        // --- 4. Create and Insert the Link ---

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

        // Set its properties to look like a link but not go anywhere
        link.href = '#'; // A "dead" link
        link.innerText = targetElement.innerText; // Use the original text
        link.style.cursor = 'pointer'; // Show a pointer cursor on hover
        link.title = 'Click to copy Torrent file URL to clipboard'; // Add a helpful tooltip

        // *** THIS IS THE NEW PART ***
        // Add a click event listener
        link.addEventListener('click', function(event) {
            // Prevent the browser from trying to follow the "#" link
            event.preventDefault();

            // Use Tampermonkey's API to copy the URL to the clipboard
            GM.setClipboard(newURL).then(() => {
                // Success!
                console.log('URL copied to clipboard:', newURL);

                // Optional: Give the user visual feedback
                const originalText = link.innerText;
                link.innerText = 'Torrent file URL copied to clipboard! Now redirecting to URL...';
                setTimeout(() => {
                    link.innerText = originalText;
                    // window.location.href = newURL;
                    GM_openInTab(newURL, false);
                }, 2000); // Reset the text after 2 seconds

            }).catch(err => {
                // Handle any errors
                console.error('Failed to copy URL: ', err);
            });
        });

        // Clear the original target element's content...
        targetElement.innerHTML = '';

        // ...and add your new "copy" link inside it.
        targetElement.appendChild(link);
    }
})();