// ==UserScript== // @name MCGEN2下载助手 // @namespace http://tampermonkey.net/ // @version 1.0 // @description 一键下载到MCGEN2 // @author jy // @require https://code.jquery.com/jquery-3.2.1.slim.min.js // @match http://*/* // @match https://*/* // @grant GM_xmlhttpRequest // @grant GM_addStyle // @grant GM_registerMenuCommand // @grant GM_getValue // @grant GM_setValue // @grant GM_openInTab // @grant unsafeWindow // @downloadURL none // ==/UserScript== (function() { 'use strict'; var mjq=jQuery.noConflict(true); var protocol={ed2k:{prefix:'ed2k://',cmd:'p2p_add_torrent_url'},magnet:{prefix:'magnet:',cmd:'p2p_add_torrent_magnet'}}; var buttonClassName='MCGEN2_downloadBtn'; GM_addStyle([ "."+buttonClassName+"{color: #fff !important;background:red;border:0px solid #000;font-weight:bold;}", ".MCGEN2_downloadList{text-align:left;opacity:0.8;width:100%;height:100%;background:#000;color:#fff;z-index:999999999;position:fixed;top:0;left:0;overflow-y: scroll;}", ".MCGEN2_scrollDiv{padding:20px;}", ".MCGEN2_downloadList .row{margin:5px;width:92%;}", ".MCGEN2_downloadList .name{width:70%; display: inline-block;overflow:hidden;}", ".MCGEN2_downloadList .status{float:right;}", ".MCGEN2_header{padding-left:20px;height:40px;width:100%;background:red;line-height:40px;}", ".MCGEN2_downloadList .mclose{position:fixed;top:50px;right:40px;background:red;color:#fff;font-weight:bold;padding:10px;border:0;}", ".MCGEN2_downloadList .logo{position:fixed;bottom:30px;left:40px;color:#fff;font-weight:bold;padding:10px;border:0;}" ].join("")); //function checkResponse(response){ //console.log(response); //} function request(c){ GM_xmlhttpRequest({ url: c.url, method: c.method || 'POST', headers: { "Content-Type": "application/x-www-form-urlencoded; charset=UTF-8", "X-Requested-With":"XMLHttpRequest", }, data:c.data, onload: function(response) { if(response.status!=200){ var r=confirm("网页状态为 "+response.status+',可能是未登录的原因,点击确定跳转到MC登录!登录完成请刷新页面重试。'); if (r) { //var window = unsafeWindow.location.href=GM_getValue("mc_server",""); GM_openInTab(GM_getValue("mc_server","")); } return; } if(typeof(c.onload)=='function'){ c.onload(response); } }, onerror:function(err){ alert('服务器地址似乎有点问题!请检查'); console.log(err); if(typeof(c.onerror)=='function'){ c.onerror(err); } } }); } mjq(function(){ //alert(123); mjq( 'body' ).on( "click", "."+buttonClassName, function() { var btn=mjq(this); var href=mjq(this).data('href'); var name=mjq(this).data('name'); var host=GM_getValue("mc_server",""); if(host=="" || !host.startsWith('http')){ alert('请先填写服务器地址'); return; } mjq(this).prop('disabled', true); var url=host+'/cgi-bin/p2p.cgi'; var data='cmd='+protocol[name].cmd+'&f_torrent_url='+encodeURIComponent(href); //alert(url); request({ url:url, data:data, onload: function(response) { if(response.responseText.indexOf('101')>0){ alert('任务已存在!'); }else if(response.responseText.indexOf('0')>0){ alert('添加任务成功!'); }else if(response.responseText.indexOf('1')>0){ alert('下载失败!'); }else{ alert(url+' 似乎有点问题!请检查'); } btn.prop('disabled', false); //console.log(response.responseText,data); }, onerror:function(err){ btn.prop('disabled', false); }}); }); function findLink(){ var isShowConfig=false; mjq('a[href]').each(function(){ var href=mjq(this).attr('href'); var addDownloadFlag=false; var pName=''; for(var p in protocol){ //console.log(p); if(href.startsWith(protocol[p].prefix)){ addDownloadFlag=true; pName=p; isShowConfig=true; } } if(addDownloadFlag){ var dbtn=mjq('