wujinjun / 移花宫(磁力搜索器)复制磁链时添加名称

// ==UserScript==
// @name         移花宫(磁力搜索器)复制磁链时添加名称
// @namespace    http://tampermonkey.net/
// @version      1.0.1
// @description  自动为移花宫的[磁力链接]按钮添加 &dn=种子名称 ,解决下载器未加载元数据时仅显示Hash的问题
// @author       wujinjun
// @license      MIT
// @match        *://yhg007.com/search-*.html
// @icon         https://www.google.com/s2/favicons?sz=64&domain=yhg007.com
// @grant        none
// @run-at       document-end
// ==/UserScript==

(function () {
    'use strict';

    function fixMagnets() {
        // 搜索结果容器
        const base = document.querySelector("body > div.wrapper > div.p-wrapper > div.searchbox > div:nth-child(4)");
        if (!base) return;

        // 遍历所有子结果 div:nth-child(1), div:nth-child(2), ...
        const items = base.children;
        for (const item of items) {

            // 1. 提取资源名称
            const titleA = item.querySelector("div.title > h3 > a");
            if (!titleA) continue;

            const name = titleA.textContent.trim(); // 去掉首尾空格

            // 2. 找到磁力链接按钮
            const magnetA = item.querySelector("div.sbar > span:nth-child(1) > a");
            if (!magnetA) continue;

            const href = magnetA.getAttribute("href");
            if (!href || !href.startsWith("magnet:?xt=urn:btih:")) continue;

            // 3. 提取 hash
            const hash = href.replace("magnet:?xt=urn:btih:", "");

            // 4. 生成新的 magnet 链接
            const newHref = `magnet:?xt=urn:btih:${hash}&dn=${encodeURIComponent(name)}`;

            // 5. 写回
            magnetA.setAttribute("href", newHref);
        }
    }

    // 页面加载完成后执行
    window.addEventListener("load", fixMagnets);
})();