Skip to content

Instantly share code, notes, and snippets.

@zhimiaoli
Last active March 18, 2025 18:29
Show Gist options
  • Save zhimiaoli/fc7c22a27feaa2cbf9ec4563fe860d30 to your computer and use it in GitHub Desktop.
Save zhimiaoli/fc7c22a27feaa2cbf9ec4563fe860d30 to your computer and use it in GitHub Desktop.
在bt4g.org的页面上直接显示磁力添加下载地址
// ==UserScript==
// @name bt4g Magnet direct show
// @namespace http://tampermonkey.net/
// @version 0.3
// @description 在bt4g.org的页面上直接显示磁力添加下载地址,现在仅支持详情页。
// @author lizhimiao
// @match https://bt4g.org/magnet/*
// @match https://bt4gprx.com/magnet/*
// @icon https://www.google.com/s2/favicons?domain=bt4g.org
// @grant none
// @updateURL https://gist.github.com/zhimiaoli/fc7c22a27feaa2cbf9ec4563fe860d30/raw/bt4g_Magnet_direct_show.user.js
// ==/UserScript==
(function() {
'use strict';
var linkDOM = document.querySelector("body > main > div > div:nth-child(2) > div > table:nth-child(3) > tbody > tr > th > a");
if (linkDOM) {
console.log("page sinle");
var link = document.querySelector("a[href^='\//downloadtorrentfile.com/hash\/']");
var hash = link.href.replace("https://downloadtorrentfile.com/hash/","").replace("?name","&dn");
linkDOM.href = "magnet:?xt=urn:btih:" + hash;
}
/* comment out list page function.
function appendLink(item) {
var d = document.createElement("a");
d.href=item.href;
d.target="_blank";
d.innerHTML = "🔗";
item.parentNode.insertBefore(d,item.nextSibling);
//console.log(item.href.replace("/magnet/","magnet:?xt=urn:btih:").replace("https://bt4g.org",""));
//item.href = item.href.replace("https://bt4g.org","");
console.log(item.href);
item.href = item.href.replace(/.*magnet\//,"magnet:?xt=urn:btih:");
//.replace("https\:\/\/bt4gprx\.com","");
}
var links = document.querySelectorAll("a[href^='\/magnet\/']");
links.forEach(appendLink);
*/
})();
@cleuton4k
Copy link

not work

@zhimiaoli
Copy link
Author

updated,dropped support for list page ,now only support detail page.

@Lovebragi
Copy link

I have the same problem, Bt4g used to put the file hash values in the search result page as a part of the result links, and I could easily grab them then generate full magnet links (I added a button with the magnet link beside each result). But later they changed all the hash values in the search page to encrypted strings, now I have to check the result link pages to have the right magnet link, inconveniently.

I'm not a pro, AI told me those encrypted strings cannot be reversed, what do you think?

@253623279
Copy link

已经失效了 能否更新一下脚本

@freetong55
Copy link

not work

@qwdsksk
Copy link

qwdsksk commented Nov 21, 2024

// ==UserScript==
// @name bt4g Magnet direct show
// @namespace http://tampermonkey.net/
// @Version 0.3
// @description 在bt4g.org的页面上直接显示磁力添加下载地址,现在仅支持详情页。
// @author lizhimiao
// @match https://bt4g.org/magnet/*
// @match https://bt4gprx.com/magnet/*
// @ICON https://www.google.com/s2/favicons?domain=bt4g.org
// @grant none
// @updateURL https://gist.github.com/zhimiaoli/fc7c22a27feaa2cbf9ec4563fe860d30/raw/bt4g_Magnet_direct_show.user.js
// ==/UserScript==
(function() {
'use strict';

// 查找具有特定href属性的<a>标签
var link = document.querySelector("a[href^='//downloadtorrentfile.com/hash/']");
if (link) {
    // 提取href属性(在这个例子中,我们实际上不需要它来完成样式的更改)
    // 但我们保留了提取哈希值的逻辑,以防后续需要使用它
    var href = link.href;
    var hashMatch = href.match(/\/hash\/([^\/?#]+)/);
    var hash = hashMatch && hashMatch[1] ? hashMatch[1] : '';

    // 构造磁力链接(虽然在这个例子中我们没有使用它,但保留了这个逻辑)
    var magnetLink = "magnet:?xt=urn:btih:" + hash;

    // 创建一个新的元素来显示磁力链接的文本
    var magnetLinkText = document.createElement('span');
    magnetLinkText.textContent = "Generated Magnet Link: " + magnetLink;
    magnetLinkText.style.color = 'red'; // 设置文本颜色为红色

    // 创建一个按钮来复制磁力链接到剪贴板
    var copyButton = document.createElement('button');
    copyButton.textContent = 'Copy Magnet Link';
    copyButton.style.backgroundColor = 'red'; // 设置按钮背景颜色为红色
    copyButton.style.color = '#fff'; // 设置按钮文本颜色为白色(可选,以便在红色背景上可读)
    copyButton.style.border = 'none'; // 去除按钮边框(可选,根据设计需求)
    copyButton.style.padding = '5px 10px'; // 设置按钮内边距(可选,根据设计需求)
    copyButton.style.cursor = 'pointer'; // 设置鼠标悬停时的样式为手形(可选,提高用户体验)

    // 为按钮添加点击事件监听器
    copyButton.addEventListener('click', function() {
        navigator.clipboard.writeText(magnetLink).then(function() {
            alert('Magnet link copied to clipboard!');
        }).catch(function(err) {
            console.error('Failed to copy text: ', err);
            alert('Failed to copy magnet link to clipboard. Please copy it manually: ' + magnetLink);
        });
    });

    // 将新元素插入到页面中的合适位置
    // 这里我们假设将按钮和文本插入到包含原始<a>标签的父元素之后
    var parentElement = link.parentElement;
    if (parentElement) {
        parentElement.appendChild(copyButton); // 插入复制按钮
        parentElement.appendChild(document.createElement('br')); // 插入一个换行符(可选,以便将按钮和文本分开)
        parentElement.appendChild(magnetLinkText); // 插入显示链接的文本
    }
} else {
    console.error("Failed to find the download link with the specified href attribute.");
}

})();

@qwdsksk
Copy link

qwdsksk commented Nov 21, 2024

微信图片_20241122012400
最终效果

@wilksu
Copy link

wilksu commented Nov 24, 2024

// ==UserScript== // @name bt4g Magnet direct show // @namespace http://tampermonkey.net/ // @Version 0.3 // @description 在bt4g.org的页面上直接显示磁力添加下载地址,现在仅支持详情页。 // @author lizhimiao // @match https://bt4g.org/magnet/* // @match https://bt4gprx.com/magnet/* // @ICON https://www.google.com/s2/favicons?domain=bt4g.org // @grant none // @updateURL https://gist.github.com/zhimiaoli/fc7c22a27feaa2cbf9ec4563fe860d30/raw/bt4g_Magnet_direct_show.user.js // ==/UserScript== (function() { 'use strict';

// 查找具有特定href属性的<a>标签
var link = document.querySelector("a[href^='//downloadtorrentfile.com/hash/']");
if (link) {
    // 提取href属性(在这个例子中,我们实际上不需要它来完成样式的更改)
    // 但我们保留了提取哈希值的逻辑,以防后续需要使用它
    var href = link.href;
    var hashMatch = href.match(/\/hash\/([^\/?#]+)/);
    var hash = hashMatch && hashMatch[1] ? hashMatch[1] : '';

    // 构造磁力链接(虽然在这个例子中我们没有使用它,但保留了这个逻辑)
    var magnetLink = "magnet:?xt=urn:btih:" + hash;

    // 创建一个新的元素来显示磁力链接的文本
    var magnetLinkText = document.createElement('span');
    magnetLinkText.textContent = "Generated Magnet Link: " + magnetLink;
    magnetLinkText.style.color = 'red'; // 设置文本颜色为红色

    // 创建一个按钮来复制磁力链接到剪贴板
    var copyButton = document.createElement('button');
    copyButton.textContent = 'Copy Magnet Link';
    copyButton.style.backgroundColor = 'red'; // 设置按钮背景颜色为红色
    copyButton.style.color = '#fff'; // 设置按钮文本颜色为白色(可选,以便在红色背景上可读)
    copyButton.style.border = 'none'; // 去除按钮边框(可选,根据设计需求)
    copyButton.style.padding = '5px 10px'; // 设置按钮内边距(可选,根据设计需求)
    copyButton.style.cursor = 'pointer'; // 设置鼠标悬停时的样式为手形(可选,提高用户体验)

    // 为按钮添加点击事件监听器
    copyButton.addEventListener('click', function() {
        navigator.clipboard.writeText(magnetLink).then(function() {
            alert('Magnet link copied to clipboard!');
        }).catch(function(err) {
            console.error('Failed to copy text: ', err);
            alert('Failed to copy magnet link to clipboard. Please copy it manually: ' + magnetLink);
        });
    });

    // 将新元素插入到页面中的合适位置
    // 这里我们假设将按钮和文本插入到包含原始<a>标签的父元素之后
    var parentElement = link.parentElement;
    if (parentElement) {
        parentElement.appendChild(copyButton); // 插入复制按钮
        parentElement.appendChild(document.createElement('br')); // 插入一个换行符(可选,以便将按钮和文本分开)
        parentElement.appendChild(magnetLinkText); // 插入显示链接的文本
    }
} else {
    console.error("Failed to find the download link with the specified href attribute.");
}

})();

it‘s work

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment