Magnet Link Extractor

提取页面中的所有磁力链接并保存为txt文件

이 스크립트를 설치하려면 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         Magnet Link Extractor
// @namespace    http://tampermonkey.net/
// @version      1.0
// @description  提取页面中的所有磁力链接并保存为txt文件
// @author       Uiharu
// @include      https://*nyaa.si*
// @grant        none
// @license GNU
// ==/UserScript==

(function() {
    'use strict';

    // 创建一个按钮
    const button = document.createElement('button');
    button.textContent = '下载磁力链接';
    button.style.position = 'fixed';
    button.style.top = '10px';
    button.style.right = '10px';
    button.style.zIndex = 1000;
    button.style.padding = '10px';
    button.style.backgroundColor = '#007bff';
    button.style.color = '#fff';
    button.style.border = 'none';
    button.style.borderRadius = '5px';
    button.style.cursor = 'pointer';

    // 将按钮添加到页面中
    document.body.appendChild(button);

    // 按钮点击事件
    button.addEventListener('click', () => {
        // 查找所有包含 magnet:?xt=urn:btih: 的链接
        const magnetLinks = [];
        const links = document.querySelectorAll('a[href^="magnet:?xt=urn:btih:"]');

        links.forEach(link => {
            magnetLinks.push(link.href);
        });

        // 如果没有找到磁力链接,提示用户
        if (magnetLinks.length === 0) {
            alert('未找到磁力链接');
            return;
        }

        // 获取页面标题,并清理标题中的非法文件名字符
        const pageTitle = document.title.replace(/[<>:"/\\|?*]/g, '_').trim() || 'magnet_links';
        const fileName = `${pageTitle}.txt`;

        // 将磁力链接保存为txt文件
        const blob = new Blob([magnetLinks.join('\n')], { type: 'text/plain' });
        const url = URL.createObjectURL(blob);

        // 创建一个下载链接
        const downloadLink = document.createElement('a');
        downloadLink.href = url;
        downloadLink.download = fileName;
        downloadLink.style.display = 'none';

        // 将下载链接添加到页面中并触发点击
        document.body.appendChild(downloadLink);
        downloadLink.click();

        // 清理
        URL.revokeObjectURL(url);
        document.body.removeChild(downloadLink);

        console.log(`磁力链接已保存到 ${fileName}`);
    });
})();