// ==UserScript== // @name 离线磁链一键下载 // @namespace https://example.com // @version 1.2 // @description 在磁力搜索列表中直接提取磁链,并在旁显示离线下载按钮。点击按钮后,如果当前页面不在目标离线服务页面,则保存磁链并打开目标离线页面;若已在目标页面,则自动调用离线下载操作(offFunc)。 // @author YourName // @require https://cdn.jsdelivr.net/npm/jquery@3.7.1/dist/jquery.min.js // @grant GM_xmlhttpRequest // @grant GM_openInTab // @grant GM_notification // @grant GM_setClipboard // @grant GM_getValue // @grant GM_setValue // @run-at document-end // @match *://*/* // @exclude http://www.toodledo.com/tasks/* // @exclude *://*.google.*/* // @exclude *://mega.*/* // @exclude *://*.mega.*/* // @exclude *://*.youku.com/v_* // @exclude *://*pan.baidu.com // @exclude *://*.iqiyi.com/v_* // @exclude *://*.iqiyi.com/w_* // @exclude *://*.iqiyi.com/a_* // @exclude *://*.le.com/ptv/vplay/* // @exclude *://v.qq.com/x/cover/* // @exclude *://v.qq.com/x/page/* // @exclude *://v.qq.com/tv/* // @exclude *://*.tudou.com/listplay/* // @exclude *://*.tudou.com/albumplay/* // @exclude *://*.tudou.com/programs/view/* // @exclude *://*.mgtv.com/b/* // @exclude *://film.sohu.com/album/* // @exclude *://tv.sohu.com/v/* // @exclude *://*.bilibili.com/video/* // @exclude *://*.bilibili.com/bangumi/play/* // @exclude *://*.baofeng.com/play/* // @exclude *://vip.pptv.com/show/* // @exclude *://v.pptv.com/show/* // @exclude *://www.le.com/ptv/vplay/* // @exclude *://www.wasu.cn/Play/show/* // @exclude *://m.v.qq.com/x/cover/* // @exclude *://m.v.qq.com/x/page/* // @exclude *://m.v.qq.com/* // @exclude *://m.iqiyi.com/* // @exclude *://m.iqiyi.com/kszt/* // @exclude *://m.youku.com/alipay_video/* // @exclude *://m.mgtv.com/b/* // @exclude *://m.tv.sohu.com/v/* // @exclude *://m.film.sohu.com/album/* // @exclude *://m.le.com/ptv/vplay/* // @exclude *://m.pptv.com/show/* // @exclude *://m.acfun.cn/v/* // @exclude *://m.bilibili.com/video/* // @exclude *://m.bilibili.com/anime/* // @exclude *://m.bilibili.com/bangumi/play/* // @exclude *://m.wasu.cn/Play/show/* // @exclude *://www.youtube.com // @exclude *://www.youtube.com/ // @exclude *://www.youtube.com/watch* // @license MIT // @downloadURL https://update.greasyfork.icu/scripts/529663/%E7%A6%BB%E7%BA%BF%E7%A3%81%E9%93%BE%E4%B8%80%E9%94%AE%E4%B8%8B%E8%BD%BD.user.js // @updateURL https://update.greasyfork.icu/scripts/529663/%E7%A6%BB%E7%BA%BF%E7%A3%81%E9%93%BE%E4%B8%80%E9%94%AE%E4%B8%8B%E8%BD%BD.meta.js // ==/UserScript== (function(){ 'use strict'; // 存储兼容:如无 GM_getValue/GM_setValue 则使用 localStorage var storage = { getItem: function(key, cb){ if(typeof GM_getValue === "function"){ cb(GM_getValue(key)); } else { cb(localStorage.getItem(key)); } }, setItem: function(key, val){ if(typeof GM_setValue === "function"){ GM_setValue(key, val); } else { localStorage.setItem(key, val); } }, delItem: function(key){ if(typeof GM_setValue === "function"){ GM_setValue(key, ""); } else { localStorage.removeItem(key); } } }; // _unsafeWindow 兼容 var _unsafeWindow = (typeof unsafeWindow !== 'undefined') ? unsafeWindow : window; // 全局变量存储当前磁链 window.curlink = ""; // ----------【第一部分】扫描页面并提取磁链,添加离线下载按钮---------- $(document).ready(function(){ $('a').each(function(){ var $a = $(this); var href = $a.attr('href'); if(!href) return; // 避免重复添加 if($a.find('.offline-btn').length > 0) return; if(/^magnet:\?xt=urn:btih:/i.test(href)){ appendOfflineButton($a, href); } else { var hash = null; var reg1 = /(?:^|\/|&|-|\.|\?|=|:)([a-fA-F0-9]{40})/; var reg2 = /\/([a-zA-Z2-7]{32})$/; if(reg1.test(href)){ hash = href.match(reg1)[1]; } else if(reg2.test(href)){ hash = reg2.exec(href)[1]; hash = base32To16(hash).toUpperCase(); } if(hash){ var magnetLink = 'magnet:?xt=urn:btih:' + hash; appendOfflineButton($a, magnetLink); } else { // 对于链接中未直接显示磁链的情况,尝试加载二级页面异步提取 if(typeof GM_xmlhttpRequest !== 'undefined'){ GM_xmlhttpRequest({ method: 'GET', url: href, onload: function(response) { if(response.status === 200){ var htmlTxt = response.responseText; var m = htmlTxt.match(/href="(magnet:\?xt=urn:btih:[^"]{40,})/i); if(m && m[1]){ appendOfflineButton($a, m[1]); } else { console.log(href + ' 无磁力链接'); } } }, onerror: function(err) { console.log('请求失败:' + href); } }); } } } }); }); // 添加离线按钮(样式参考第二脚本) function appendOfflineButton($anchor, magnetLink) { var btn = $(''); btn.css({ 'display': 'inline-block', 'margin-left': '5px', 'background-size': '20px', 'border-radius': '50%', 'border': '0', 'vertical-align': 'middle', 'outline': 'none', 'padding': '0', 'height': '26px', 'width': '26px', 'cursor': 'pointer', 'background-position': 'center', 'background-repeat': 'no-repeat', 'background-color': '#f2f2f2' }); var downIconBg = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABkAAAAZCAMAAADzN3VRAAAARVBMVEUAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAADc6ur3AAAAFnRSTlMAYM5vMOA/ENGegK2olI6G1b97Z0sXENA+jAAAAKFJREFUKM+FklkSxCAIRHFfss3K/Y86iQSDVqzpH7FfgQpCVfAmGx+gl9JI0qrxrcNLzooEbKUG4EKWdkCiDRV0N0RTrZ5wvdgTTgp4SzCAHxAPZkAM5GOJWuuT7FE5OVPOBFLTYb3Oc2YB5uJ8+G6pgkTGt74ntcCJHiwFLHw10Tdc93jlGXGvSRtsHNpuPs+/o1ODfxAtSL0f7HPC+L/9AF60G3QxO1UaAAAAAElFTkSuQmCC"; btn.css('background-image', 'url("' + downIconBg + '")'); // 点击按钮时,将磁链赋给全局 curlink,并调用 showDiskIcons 显示离线服务选择面板 btn.on('click', function(e){ e.preventDefault(); e.stopPropagation(); window.curlink = magnetLink; var offset = btn.offset(); showDiskIcons(magnetLink, offset.top, offset.left); }); $anchor.append(btn); } // base32转16进制函数 function base32To16(str){ if(str.length % 8 !== 0 || /[0189]/.test(str)){ return str; } str = str.toUpperCase(); var bin = "", newStr = "", i; for(i = 0; i < str.length; i++){ var charCode = str.charCodeAt(i); if(charCode < 65) charCode -= 24; else charCode -= 65; charCode = '0000' + charCode.toString(2); charCode = charCode.substr(charCode.length - 5); bin += charCode; } for(i = 0; i < bin.length; i += 4){ newStr += parseInt(bin.substring(i, i+4), 2).toString(16); } return newStr; } // ----------【第二部分】离线下载面板及服务调用---------- // 添加离线下载面板容器 var parentDiv = $("
"); $("body").append(parentDiv); $("