Greasy Fork

来自缓存

Greasy Fork is available in English.

YTS Magnet Button

Add magnet button next to YTS download buttons

您需要先安装一个扩展,例如 篡改猴Greasemonkey暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴暴力猴,之后才能安装此脚本。

您需要先安装一个扩展,例如 篡改猴Userscripts ,之后才能安装此脚本。

您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。

您需要先安装用户脚本管理器扩展后才能安装此脚本。

(我已经安装了用户脚本管理器,让我安装!)

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展,比如 Stylus,才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

您需要先安装一款用户样式管理器扩展后才能安装此样式。

(我已经安装了用户样式管理器,让我安装!)

// ==UserScript==
// @name         YTS Magnet Button
// @namespace    http://tampermonkey.net/
// @version      0.4
// @description  Add magnet button next to YTS download buttons
// @match        https://yts.mx/*
// @grant        none
// @license      MIT
// ==/UserScript==

(function() {
    'use strict';

    // Function to create and add the magnet button
    function addMagnetButton(downloadButton) {
        // Check if the magnet button already exists
        if (downloadButton.nextSibling && downloadButton.nextSibling.classList && downloadButton.nextSibling.classList.contains('magnet-button')) {
            return;
        }

        // Create the magnet button
        const magnetButton = document.createElement('a');
        magnetButton.innerHTML = '🧲'; // Magnet emoji as the button text
        magnetButton.style.cursor = 'pointer';
        magnetButton.title = 'Magnet and open to default torrent app';
        magnetButton.classList.add('magnet-button'); // Add a class for identification

        // Add click event to the magnet button
        magnetButton.addEventListener('click', function(e) {
            e.preventDefault();
            e.stopPropagation();

            // Get the torrent URL from the download button
            const torrentUrl = downloadButton.href;

            // Construct the magnet link (this is a simplified version, you may need to adjust it)
            const magnetLink = `magnet:?xt=urn:btih:${torrentUrl.split('/').pop()}&dn=${encodeURIComponent(document.title)}`;

            // Open the magnet link (this should trigger qBittorrent if it's set as the default app)
            window.location.href = magnetLink;
        });

        // Create a container for the buttons
        const buttonContainer = document.createElement('div');
        buttonContainer.style.display = 'flex';
        buttonContainer.style.marginBottom = '10px';

        // Move the original download button into the container
        downloadButton.parentNode.insertBefore(buttonContainer, downloadButton);
        buttonContainer.appendChild(downloadButton);

        // Add the magnet button to the container
        buttonContainer.appendChild(magnetButton);
    }

    // Find all download buttons and add magnet buttons
    function addMagnetButtons() {
        const downloadButtons = document.querySelectorAll('a[href^="https://yts.mx/torrent/download/"]');
        downloadButtons.forEach(addMagnetButton);
    }

    // Function to add a line break after the specified <em> element
    function addLineBreakAfterEmElement() {
        const emElement = document.querySelector('em.pull-left');
        if (emElement && emElement.textContent.includes('Available in:')) {
            const br = document.createElement('br');
            emElement.parentNode.insertBefore(br, emElement.nextSibling);
        }
    }

    // Run the script when the page loads
    window.addEventListener('load', function() {
        addMagnetButtons();
        addLineBreakAfterEmElement();
    });
})();