Magnet Url

复制磁力链接

目前為 2025-04-07 提交的版本,檢視 最新版本

您需要先安裝使用者腳本管理器擴展,如 TampermonkeyGreasemonkeyViolentmonkey 之後才能安裝該腳本。

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

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyViolentmonkey 後才能安裝該腳本。

您需要先安裝使用者腳本管理器擴充功能,如 TampermonkeyUserscripts 後才能安裝該腳本。

你需要先安裝一款使用者腳本管理器擴展,比如 Tampermonkey,才能安裝此腳本

您需要先安裝使用者腳本管理器擴充功能後才能安裝該腳本。

(我已經安裝了使用者腳本管理器,讓我安裝!)

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展,比如 Stylus,才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

你需要先安裝一款使用者樣式管理器擴展後才能安裝此樣式

(我已經安裝了使用者樣式管理器,讓我安裝!)

// ==UserScript==
// @name         Magnet Url
// @namespace    http://tampermonkey.net/
// @version      0.0.2
// @license      MIT
// @description  复制磁力链接
// @author       Xcec
// @match        *://*/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=xzonesoft.com
// @grant        none
// ==/UserScript==

"use strict";

(function () {
  "use strict";

  // Function to extract magnet links
  function extractMagnetLinks() {
    const magnetLinks = [];
    const regex = /magnet:\?xt=urn:btih:[a-fA-F0-9]{40,}/g;

    // Iterate over all text nodes in the document
    function traverse(node) {
      if (node.nodeType === Node.TEXT_NODE) {
        const matches = node.textContent.match(regex);
        if (matches) {
          matches.forEach((match) => {
            const parentElement = node.parentElement;
            magnetLinks.push({ link: match, element: parentElement });
          });
        }
      } else {
        node.childNodes.forEach((child) => traverse(child));
      }
    }

    traverse(document.body);

    return magnetLinks;
  }

  // Get all magnet links and their closest parent elements
  const magnets = extractMagnetLinks();

  // 在每个元素后增加一个按钮,点击复制磁力链接
  magnets.forEach((magnet) => {
    const button = document.createElement("button");
    button.innerText = "Copy";
    button.style.cssText =
      "position: absolute; right: -54px; top: -5px; z-index: 999";

    button.addEventListener("click", () => {
      navigator.clipboard.writeText(magnet.link);
    });
    magnet.element.style.cssText = "position: relative;";
    magnet.element.appendChild(button);
  });

  // 在页面底部增加一个按钮,点击复制所有磁力链接
  const button = document.createElement("button");
  button.className = "floating-btn"; // 使用类名控制样式
  button.innerHTML = `
    <span class="btn-text">🔗 Copy All</span>
    <span class="hover-effect"></span>
  `;

  // 通过CSS类集中管理样式
  const style = document.createElement("style");
  style.textContent = `
  .floating-btn {
    position: fixed;
    right: 20px;
    bottom: 20px;
    z-index: 999;
    
    /* 视觉设计 */
    min-width: 120px;
    padding: 12px 24px;
    border: none;
    border-radius: 30px;
    background: linear-gradient(135deg, #4a90e2, #6366f1);
    box-shadow: 0 4px 6px rgba(0, 0, 0, 0.1), 0 1px 3px rgba(0, 0, 0, 0.08);
    
    /* 文字样式 */
    color: white;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
    font-weight: 600;
    letter-spacing: 0.5px;
    font-size: 14px;
    
    /* 交互效果 */
    cursor: pointer;
    transition: all 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    transform: translateY(0);
    overflow: hidden;
  }

  /* 悬停状态 */
  .floating-btn:hover {
    transform: translateY(-2px);
    box-shadow: 0 10px 15px rgba(0, 0, 0, 0.1), 0 4px 6px rgba(0, 0, 0, 0.05);
    background: linear-gradient(135deg, #6366f1, #4a90e2);
  }

  /* 点击反馈 */
  .floating-btn:active {
    transform: translateY(1px);
    box-shadow: 0 2px 3px rgba(0, 0, 0, 0.1);
  }

  /* 焦点状态 */
  .floating-btn:focus-visible {
    outline: 3px solid rgba(99, 102, 241, 0.4);
    outline-offset: 2px;
  }

  /* 动态效果层 */
  .hover-effect {
    position: absolute;
    background: rgba(255, 255, 255, 0.2);
    width: 50px;
    height: 50px;
    border-radius: 50%;
    transform: translate(-50%, -50%) scale(0);
    animation: ripple 0.6s ease-out;
  }

  @keyframes ripple {
    from {
      transform: translate(-50%, -50%) scale(0);
      opacity: 1;
    }
    to {
      transform: translate(-50%, -50%) scale(4);
      opacity: 0;
    }
  }

  /* 文字防选中 */
  .btn-text {
    position: relative;
    z-index: 1;
    user-select: none;
  }
  `;

  button.addEventListener("click", () => {
    const magnetLinks = magnets.map((magnet) => magnet.link);
    navigator.clipboard.writeText(magnetLinks.join("\n"));
  });
  if(magnets.lenght > 0) {
    document.body.appendChild(style);
    document.body.appendChild(button);
  }
})();