// ==UserScript==
// @name MCGEN2下载助手
// @namespace http://tampermonkey.net/
// @version 1.2
// @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';
var host='';
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:60%; display: inline-block;overflow:hidden;}",
".MCGEN2_downloadList .status{xfloat:right;display: inline-block;}",
".MCGEN2_downloadList .size{width:100px;display: inline-block;}",
".MCGEN2_header{padding-left:20px;height:40px;width:100%;background:red;line-height:40px;}",
".MCGEN2_downloadList .btns{background:red;color:#fff;font-weight:bold;padding:10px;border:0;display:block;margin:10px;}",
".MCGEN2_downloadList .logo{position:fixed;bottom:30px;left:40px;color:#fff;font-weight:bold;padding:10px;border:0;}",
".MCGEN2_btnsDiv{position:fixed;top:50px;right:40px;padding:10px;}"
].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) {
console.log(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",""),false);
}
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');
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);
/*
case 0: //Success
case 1: //Success
_html = _T('_p2p','desc9'); //Text:Successfully added.
break;
case 101://Failed to add this torrent file. The torrent file is invalid or duplicate.
case 104:
_html = _T('_p2p','msg21');
break;
case 102://Failed to add this torrent file. The My Cloud system does not have enough free space.
case 103:
_html = _T('_p2p','msg22');
break;
default://Upload Torrent Error(Error Code:xxx).
_html = _T('_p2p','msg24')+"("+_T('_format','error_code')+":"+my_res+")";
break;
*/
request({
url:url,
data:data,
onload: function(response) {
if(response.responseText.indexOf('