Magnet Link Handler for qBittorrent WebUI

Intercept magnet links and open qBittorrent download dialog in a popup.

当前为 2025-01-24 提交的版本,查看 最新版本

// ==UserScript==
// @name         Magnet Link Handler for qBittorrent WebUI
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  Intercept magnet links and open qBittorrent download dialog in a popup.
// @match        *://*/*
// @grant        GM_getValue
// @grant        GM_setValue
// @grant        GM_registerMenuCommand
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    // Define the default qBittorrent WebUI URL
    const defaultQbWebUIHost = 'http://localhost:8080';

    // Retrieve the saved qBittorrent WebUI URL or use the default
    let qbWebUIHost = GM_getValue('qbWebUIHost', defaultQbWebUIHost);

    // Add a menu command to set the qBittorrent WebUI URL
    GM_registerMenuCommand('Set qBittorrent WebUI URL', () => {
        const newUrl = prompt('Enter the qBittorrent WebUI URL:', qbWebUIHost);
        if (newUrl) {
            GM_setValue('qbWebUIHost', newUrl); // Save the new URL
            qbWebUIHost = newUrl; // Update the script's URL
            alert(`qBittorrent WebUI URL set to: ${newUrl}`);
        }
    });

    // Check if the current page is the qBittorrent WebUI's download.html
    const isDownloadPage = window.location.href.startsWith(qbWebUIHost) && window.location.pathname === '/download.html';

    if (isDownloadPage) {
        // Check if the URL contains the `?link=` parameter
        const urlParams = new URLSearchParams(window.location.search);
        const encodedMagnetLink = urlParams.get('url');
        if (!encodedMagnetLink) {
            return; // Exit if no `?url=` parameter is found
        }

        // Decode the magnet link
        const magnetLink = decodeURIComponent(encodedMagnetLink);

        // Function to fill the textarea with the magnet link
        const magnetTextarea = document.querySelector('#urls');
        if (magnetTextarea) {
            magnetTextarea.value = magnetLink; // Fill the textarea
        }

        // Register close handler.
        document.querySelector('#download_frame').addEventListener("load", function() {
            window.close();
        });
    } else {
        // Function to handle magnet link clicks
        function handleMagnetLinkClick(event) {
            let target = event.target;

            while (target && target.tagName !== 'A') {
                target = target.parentElement;
            }

            // Check if the clicked element is a magnet link
            if (target?.href.startsWith('magnet:')) {
                event.preventDefault(); // Prevent the default behavior

                // Encode the magnet link for use in a URL
                const encodedMagnetLink = encodeURIComponent(target.href);

                // Open the qBittorrent WebUI's download.html in a popup
                const popupUrl = `${qbWebUIHost}/download.html?url=${encodedMagnetLink}`;

                // Define the popup dimensions
                const popupWidth = 500;
                const popupHeight = 600;

                // Calculate the position to center the popup
                const left = (window.screen.width - popupWidth) / 2;
                const top = (window.screen.height - popupHeight) / 2;

                // Open the popup with centered position
                window.open(
                    popupUrl,
                    'qBittorrentAddMagnet',
                    `width=${popupWidth},height=${popupHeight},left=${left},top=${top}`
                );
            }
        }

        // Attach the event listener to the document
        document.addEventListener('click', handleMagnetLinkClick, true);
    }
})();

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址