磁力快显
// ==UserScript==
// @name 磁力快显
// @author zxf10608
// @version 4.0
// @homepageURL https://greasyfork.org/zh-CN/scripts/397490
// @icon https://cdn.jsdelivr.net/gh/zxf10608/JavaScript/icon/magnet00.png
// @description 在磁力宝、BTSOW、ØMagnet、磁力狗等磁力搜索引擎的搜索列表增加磁力链接显示,方便快速下载资源。
// @require https://cdn.jsdelivr.net/npm/jquery@3.7.1/dist/jquery.min.js
// @include *://clb*.*
// @include *://clg*.*/search*
// @include *://www.btmov*.*/so/*
// @include *://*mag.net/search*
// @include *://btsow*/search/*
// @include *://www.*yuhuage*.*/search/*
// @connect *
// @grant unsafeWindow
// @grant GM_setValue
// @grant GM_getValue
// @grant GM_openInTab
// @grant GM_notification
// @grant GM_setClipboard
// @grant GM_xmlhttpRequest
// @grant GM_registerMenuCommand
// @grant GM_unregisterMenuCommand
// @run-at document-start
// @license GPL License
// ==/UserScript==
(function() {
'use strict';
if(document.title.includes('磁力宝')){
unsafeWindow.alert = () => console.log('已阻止弹窗');
};
$(document).ready(function(){
// 菜单注册逻辑
const toggleNotification = () => {
const isOpen = GM_getValue('open');
GM_registerMenuCommand(isOpen ? '关闭复制弹窗通知' : '开启复制弹窗通知', () => {
GM_setValue('open', Number(!isOpen));
location.reload();
});
};
toggleNotification();
// 公共样式常量
const MAGNET_STYLE = 'z-index:9123456789;display:inline-block;cursor:pointer;margin:0 5px 2px;'
+ 'border-radius:50%;border:0;vertical-align:middle;outline:none!important;'
+ 'padding:0!important;height:20px!important;width:20px!important;';
// base32转换函数
const base32To16 = str => {
if(str.length % 8 !== 0 || /[0189]/.test(str)) return str;
return [...str.toUpperCase()]
.map(c => (c.charCodeAt(0) < 65 ? c.charCodeAt(0)-24 : c.charCodeAt(0)-65).toString(2).padStart(5,'0'))
.join('')
.match(/.{1,4}/g)
.map(bin => parseInt(bin,2).toString(16))
.join('')
.toUpperCase();
};
// 创建磁力图标函数
const createMagnetIcon = hash => {
const magnetLink = `magnet:?xt=urn:btih:${hash}`;
return `<img src="https://cdn.jsdelivr.net/gh/zxf10608/JavaScript/icon/magnet00.png"
class="mag1"
href="${magnetLink}"
title="识别到磁力链接,左键打开,右键复制\n${magnetLink}"
style="${MAGNET_STYLE}">`;
};
// 磁力链接检测逻辑
const processLinks = () => {
$('a:not([href^="magnet:"])').each(function() {
const link = $(this).attr('href') || '';
const hashMatch = link.match(/(?:^|\/|&|[-.?=:])([a-fA-F0-9]{40})(?!\w)/)
|| link.match(/\/([a-zA-Z2-7]{32})$/);
if(hashMatch) {
let hash = hashMatch[1];
if(hash.length === 32) hash = base32To16(hash);
$(this).attr('target', '_blank').append(createMagnetIcon(hash));
}
});
};
processLinks();
// 异步加载逻辑
if($('.mag1').length < 1 && !document.title.includes('磁力宝')) {
const magnetCall = href => new Promise((resolve, reject) => {
GM_xmlhttpRequest({
method: 'GET',
url: href,
onload: ({status, responseText}) => status === 200 ? resolve(responseText) : reject(),
onerror: reject
});
});
const magnetEl = $('h3 a[href!="/"], li a[href!="/"], td a[href!="/"], dd a[href!="/"]');
magnetEl.attr({target: '_blank', style: 'display:inline-block;'});
const maxRequests = Math.min(magnetEl.length, 20);
const requests = Array.from({length: maxRequests}, (_, i) => {
const link = (magnetEl.eq(i).attr('href') || '').replace(/^\//, location.origin + '/');
return magnetCall(link).then(html => {
const magnetLink = html.match(/href="(magnet.{54})/)?.[1];
magnetLink && magnetEl.eq(i).after(createMagnetIcon(magnetLink.split(':').pop()));
return magnetLink;
});
});
Promise.allSettled(requests).then(results => {
const successCount = results.filter(r => r.status === 'fulfilled' && r.value).length;
console.log(`成功加载磁力链接:${successCount}/${maxRequests}`);
});
}
setTimeout(function(){
if($('.115offline').length>0){
$('.mag1').remove();//隐藏图标
};
},800);
$('body').on('contextmenu click','.mag1', function(e){
var link=$(this).attr('href')
if(e.type == 'click'){//左键
GM_openInTab(link,false);
}else{//右键
GM_setClipboard(link);
if(GM_getValue('open')==1){
GM_notification({
title:'磁力快显:',
text:'磁力链接复制成功!',
timeout:2000,
});
};
console.log('磁力链接复制成功:\n'+link);
};
return false;
});
});
})();