2 // @name 115网盘aria2导出工具
4 // @description 一个方便把115网盘导出到Aria2rpc的脚本。
6 // @include http://*.115.com/*
7 // @run-at document-end
10 var pan_115 = function(cookies) {
11 var version = "0.1.4";
12 var update_date = "2016/02/09";
13 var pan = (function() {
15 var SetMessage = function(msg, type) {
16 Core.MinMessage.Show({
22 var auth = null; //是否设置用户名密码验证 设置的话变为auth赋值
23 var HttpSendRead = function(info) {
24 var http = new XMLHttpRequest();
25 var contentType = "\u0061\u0070\u0070\u006c\u0069\u0063\u0061\u0074\u0069\u006f\u006e\u002f\u0078\u002d\u0077\u0077\u0077\u002d\u0066\u006f\u0072\u006d\u002d\u0075\u0072\u006c\u0065\u006e\u0063\u006f\u0064\u0065\u0064\u003b\u0020\u0063\u0068\u0061\u0072\u0073\u0065\u0074\u003d\u0055\u0054\u0046\u002d\u0038";
27 var deferred = jQuery.Deferred();
28 if (info.contentType != null) {
29 contentType = info.contentType;
31 if (info.timeout != null) {
32 timeout = info.timeout;
34 var timeId = setTimeout(httpclose, timeout);
35 function httpclose() {
38 deferred.promise(http);
39 http.onreadystatechange = function() {
40 if (http.readyState == 4) {
41 if ((http.status == 200 && http.status < 300) || http.status == 304) {
43 if (info.dataType == "json") {
44 deferred.resolve(JSON.parse(http.responseText), http.status, http);
46 else if (info.dataType == "SCRIPT") {
47 // eval(http.responseText);
48 deferred.resolve(http.responseText, http.status, http);
53 deferred.reject(http, http.statusText, http.status);
58 http.open(info.type, info.url, true);
59 http.setRequestHeader("Content-type", contentType);
60 for (h in info.headers) {
61 if (info.headers[h]) {
62 http.setRequestHeader(h, info.headers[h]);
65 if (info.type == "POST") {
73 //设置aria2c下载设置的Header信息
75 header: function(type) {
77 var UA = $("#setting_aria2_useragent_input").val() || "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36 115Browser/5.1.3";
78 var headers = $("#setting_aria2_headers").val();
79 var referer = $("#setting_aria2_referer_input").val() || "http://115.com/";
80 addheader.push("User-Agent: " + UA);
81 // var baidu_cookies=JSON.parse(cookies);
82 // var format_cookies=[];
83 // for(var i=0;i<baidu_cookies.length;i++){
84 // for(var key in baidu_cookies[i]){
85 // // addheader.push("Cookie: " + key +"=" +baidu_cookies[i][key]);
86 // format_cookies.push(key +"=" +baidu_cookies[i][key]);
89 // addheader.push("Cookie: " + format_cookies.join(";"));
90 addheader.push("Referer: " + referer);
92 var text = headers.split("\n");
93 for (var i = 0; i < text.length; i++) {
94 addheader.push(text[i]);
98 if (type == "aria2c_line") {
99 for (var i = 0; i < addheader.length; i++) {
100 header += " --header " + JSON.stringify(addheader[i]) + " ";
103 } else if (type == "aria2c_txt") {
104 for (var i = 0; i < addheader.length; i++) {
105 header += " header=" + (addheader[i]) + " \n";
108 } else if (type == "idm_txt") {
109 for (var i = 0; i < addheader.length; i++) {
110 header += " header=" + (addheader[i]) + " \n";
119 var css = function() {/*
126 padding: 0 10px 0 10px;
140 padding: 0 10px 0 10px;
150 li[rel="item"]:hover .show-export-button {
156 }.toString().slice(15, -4);
157 var url = (localStorage.getItem("rpc_url") || "http://localhost:6800/jsonrpc") + "?tm=" + (new Date().getTime().toString());
164 self.set_config_ui();
166 SetMessage("载入成功!", "inf");
169 //设置导出按钮的触发 js_top_panel_box
172 document.querySelector("iframe[rel='wangpan']").addEventListener('load',function(){
175 main_page_setting_btn();
176 function main_page_setting_btn(){
177 var setting_div=$("<a>").text("插件设置").attr("href","javascript:;");
178 var main_setting_div=$("<a>").text("插件设置").attr("href","javascript:;");
179 main_setting_div.attr("id","main_setting_div");
180 if(!document.querySelector("a[id='main_setting_div']")){
181 main_setting_div.appendTo($(document.querySelector("div[id='js_main_container']")).find(".tup-logout"));
182 main_setting_div.on('click',function(){
183 $("#setting_div").show();
184 $("#setting_divtopmsg").html("");
185 self.set_center($("#setting_div"));
189 function top_panel_box_btn(){
190 var root=document.querySelector("iframe[rel='wangpan']").contentDocument;
191 var setting_div=$("<a>").text("插件设置").attr("href","javascript:;");
192 var main_setting_div=$("<a>").text("插件设置").attr("href","javascript:;");
193 main_setting_div.attr("id","main_setting_div");
194 setting_div.appendTo($(root).find(".tup-logout"));
195 if(!document.querySelector("a[id='main_setting_div']")&&document.querySelector("iframe[rel='wangpan']").src.indexOf('ct=rb&is_wl_tpl=1')<0){
196 main_setting_div.appendTo($(document.querySelector("div[id='js_main_container']")).find(".tup-logout"));
197 main_setting_div.on('click',function(){
198 $("#setting_div").show();
199 $("#setting_divtopmsg").html("");
200 self.set_center($("#setting_div"));
203 setting_div.on('click',function(){
204 $("#setting_div").show();
205 $("#setting_divtopmsg").html("");
206 self.set_center($("#setting_div"));
208 if(!root.querySelector("a[menu='clear']")){
209 $(root).find(".file-path").after($("<div>").text("RPC下载").addClass("btn-aria2c").on('click',function(){
210 self.aria2_export(true);
212 $(root).find(".file-path").after($("<div>").text("导出下载").addClass("btn-txt").on('click',function(){
213 self.aria2_download();
214 self.aria2_export(false);
217 var style = document.createElement('style');
218 style.setAttribute('type', 'text/css');
219 style.textContent = css;
220 root.head.appendChild(style);
223 set_config_ui:function(){
225 var setting_div = document.createElement("div");
226 setting_div.className = "download-mgr-dialog dialog-box";
227 setting_div.id = "setting_div";
229 '<h2 class="dialog-title" ><span rel="base_title">导出设置</span><div class="dialog-handle"><a href="javascript:;" class="diag-close" btn="close">关闭</a></div></h2>',
230 '<div style=" margin: 20px 10px 10px 10px; ">',
231 '<div id="setting_divtopmsg" style="position:absolute; margin-top: -18px; margin-left: 10px; color: #E15F00;"></div>',
232 '<table id="setting_div_table" >',
234 '<tr><td width="100"><label>ARIA2 RPC:</label></td><td><input id="rpc_input" type="text" class="input-large"></td></tr>',
235 '<tr><td><label>RPC访问设置</label></td><td><input id="rpc_distinguish" type="checkbox"></td></tr>',
236 '<tr><td><label >RPC 用户名:</label></td><td><input type="text" id="rpc_user" disabled="disabled" class="input-small"></td></tr>',
237 '<tr><td><label>RPC 密码:</label></td><td><input type="text" id="rpc_pass" disabled="disabled" class="input-small"></td></tr>',
238 '<tr><td><label>Secret Token:</label></td><td><input type="text" id="rpc_token" class="input-small"><div style="position:absolute; margin-top: -20px; right: 20px;"><a id="send_test" type="0" href="javascript:;" >测试连接,成功显示版本号。</a></div></td></tr>',
239 '<tr><td><label>下载路径:</label></td><td><input type="text" placeholder="只能设置为绝对路径" id="setting_aria2_dir" class="input-large"></td></tr>',
240 '<tr><td><label>User-Agent :</label></td><td><input type="text" id="setting_aria2_useragent_input" class="input-large"></td></tr>',
241 '<tr><td><label>Referer :</label></td><td><input type="text" id="setting_aria2_referer_input" class="input-large"></td></tr>',
242 '<tr><td colspan="2"><div style="color: #656565;">Headers<label style="margin-left: 65px;">※使用回车分隔每个headers。</label></div><li class="b-list-item separator-1"></li></td></tr>',
243 '<tr><td><label>headers :</label></td><td><textarea id="setting_aria2_headers" ></textarea></td></tr>',
246 '<div style="margin-top:10px;">',
247 '<div id="copyright">© Copyright <a href="https://github.com/acgotaku/115">雪月秋水 </a> Version:' + version + ' 更新日期: ' + update_date + ' </div>',
248 '<div style="margin-left:20px; display:inline-block"><a href="javascript:;" id="apply" class="button" ><b>应用</b></a></div>',
252 setting_div.innerHTML = html_.join("");
253 document.body.appendChild(setting_div);
254 $(".diag-close").click(function() {
255 $("#setting_div").hide();
257 $("#apply").click(function() {
259 $("#setting_divtopmsg").html("设置已保存.");
261 $("#send_test").click(function() {
264 $("#rpc_distinguish").change(function() {
265 if ($(this).is(":checked")) {
266 $("#rpc_user").removeAttr("disabled").css("background-color", "#FFF");
267 $("#rpc_pass").removeAttr("disabled").css("background-color", "#FFF");
269 $("#rpc_user").attr({"disabled": "disabled"}).css("background-color", "#eee");
270 $("#rpc_pass").attr({"disabled": "disabled"}).css("background-color", "#eee");
276 set_config: function() {
277 $("#rpc_input").val((localStorage.getItem("rpc_url") || "http://localhost:6800/jsonrpc"));
278 $("#rpc_token").val(localStorage.getItem("rpc_token"));
279 $("#setting_aria2_dir").val(localStorage.getItem("rpc_dir"));
280 $("#setting_aria2_useragent_input").val(localStorage.getItem("UA") || "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36 115Browser/5.1.3");
281 $("#setting_aria2_referer_input").val(localStorage.getItem("referer") || "http://115.com/");
282 $("#setting_aria2_headers").val(localStorage.getItem("rpc_headers"));
284 if (localStorage.getItem("auth") == "true") {
285 var rpc_user = localStorage.getItem("rpc_user");
286 var rpc_pass = localStorage.getItem("rpc_pass");
287 $("#rpc_user").val(rpc_user);
288 $("#rpc_pass").val(rpc_pass);
289 $("#rpc_distinguish").prop('checked', true).trigger("change");
290 auth = "Basic " + btoa(rpc_user + ":" + rpc_pass);
293 $("#rpc_user, #rpc_pass").val("");
297 get_config: function() {
298 var rpc_url = $("#rpc_input").val();
300 localStorage.setItem("rpc_url", rpc_url);
301 url = rpc_url + "?tm=" + (new Date().getTime().toString());
303 localStorage.setItem("UA", document.getElementById("setting_aria2_useragent_input").value || "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/31.0.1650.63 Safari/537.36 115Browser/5.1.3");
304 if ($("#rpc_distinguish").prop('checked') == true) {
305 localStorage.setItem("rpc_user", $("#rpc_user").attr("value"));
306 localStorage.setItem("rpc_pass", $("#rpc_pass").attr("value"));
307 localStorage.setItem("auth", true);
308 auth = "Basic " + btoa($("#rpc_user").attr("value") + ":" + $("#rpc_pass").attr("value"));
310 localStorage.setItem("auth", false);
311 localStorage.setItem("rpc_user", null);
312 localStorage.setItem("rpc_pass", null);
314 localStorage.setItem("rpc_token", $("#rpc_token").val());
315 localStorage.setItem("rpc_dir", $("#setting_aria2_dir").val());
316 localStorage.setItem("rpc_headers", $("#setting_aria2_headers").val());
317 localStorage.setItem("referer", $("#setting_aria2_referer_input").val());
319 set_center:function(obj){
320 var screenWidth = $(window).width(), screenHeight = $(window).height();
321 var scrolltop = $(document).scrollTop();
322 var objLeft = (screenWidth - obj.width())/2 ;
323 var objTop = (screenHeight - obj.height())/2 + scrolltop;
324 obj.css({left: objLeft + 'px', top: objTop + 'px'});
326 getFileInfo:function(pick_code,method,path){
329 DownBridge.getFileUrl(pick_code,function(data){
331 file_list.push({"name": (path||"")+$('<textarea />').html(data.file_name).text(), "link": data.file_url});
333 self.aria2_rpc(file_list);
335 $("#download_ui").show();
336 self.aria2_data(file_list);
342 aria2_export:function(method){
344 var root=document.querySelector("iframe[rel='wangpan']").contentDocument;
345 $(root).find('li[rel="item"][file_type="1"]').each(function(){
346 if($(this).children().eq(3).prop('checked') == true){
347 var pick_code = $(this).attr('pick_code');
348 self.getFileInfo(pick_code,method);
351 $(root).find('li[rel="item"][file_type="0"]').each(function(){
352 if($(this).children().eq(2).prop('checked') == true){
353 var cate_id = $(this).attr('cate_id');
354 DownBridge.getFileList(cate_id,function(data){
356 for(var i=0;i<list.length;i++){
358 self.getFileInfo(list[i].pc,method,data.path[data.path.length-1].name+"/");
360 var dir_level=data.path.length-1;
361 self.get_all_dir(list[i].cid,dir_level,method);
369 get_all_dir:function(cid,dir_level,method){
371 DownBridge.getFileList(cid,function(data){
374 for(var i=dir_level;i<data.path.length;i++){
375 path+=data.path[i].name+"/";
377 for(var i=0;i<list.length;i++){
379 self.getFileInfo(list[i].pc,method,path);
381 self.get_all_dir(list[i].cid,dir_level,method);
387 aria2_download: function() {
388 if ($("#download_ui").length == 0) {
389 var download_ui = $("<div>").attr("id", "download_ui").addClass("download-mgr-dialog dialog-box").html('<h2 class="dialog-title" ><span rel="base_title">ARIA2导出</span><div class="dialog-handle"><a href="javascript:;" class="diag-close" btn="close">关闭</a></div></h2>');
390 var content_ui = $("<div>").addClass("content").attr("id", "content_ui").appendTo(download_ui);
391 download_ui.appendTo($("body"));
393 var download_menu = $("<div>").css({"display": "block", "margin-bottom": "10px"}).appendTo(content_ui);
394 var aria2c_btn = $("<a>").attr("id", "aria2c_btn").attr({"href": "data:text/plain;charset=utf-8,", "download": "aria2c.down", "target": "_blank"}).addClass("new-btn").html('<b>存为aria2文件</b>').appendTo(download_menu);
395 var idm_btn = $("<a>").attr("id", "idm_btn").attr({"href": "data:text/plain;charset=utf-8,", "download": "idm.txt", "target": "_blank"}).addClass("new-btn").html('<b>存为IDM文件</b>').appendTo(download_menu);
396 var download_txt_btn = $("<a>").attr("id", "download_txt_btn").attr({"href": "data:text/plain;charset=utf-8,", "download": "download_link.down", "target": "_blank"}).addClass("new-btn").html('<b>保存下载链接</b>').appendTo(download_menu);
397 var download_link = $("<textarea>").attr("wrap", "off").attr("id", "download_link").css({"white-space": "nowrap", "width": "100%", "overflow": "scroll", "height": "180px"});
398 download_link.appendTo(content_ui);
399 $(".diag-close").click(function() {
403 $("#aria2c_btn, #idm_btn, #download_txt_btn").attr("href", "data:text/plain;charset=utf-8,");
404 $("#download_link").val("");
408 aria2_data: function(file_list) {
413 if (file_list.length > 0) {
414 var length = file_list.length;
415 for (var i = 0; i < length; i++) {
416 files.push("aria2c -c -s10 -k1M -x10 -o " + JSON.stringify(file_list[i].name) + combination.header('aria2c_line') + " " + JSON.stringify(file_list[i].link) + "\n");
419 combination.header("aria2c_txt"),
420 ' out=' + file_list[i].name,
422 ' max-connection-per-server=10',
429 ' cookie: ' + cookies,
430 ' out=' + file_list[i].name,
433 down_txt.push([file_list[i].link, ' '].join('\n'));
435 $("#aria2c_btn").attr("href", $("#aria2c_btn").attr("href") + encodeURIComponent(aria2c_txt.join("")));
436 $("#idm_btn").attr("href", $("#idm_btn").attr("href") + encodeURIComponent(idm_txt.join("")));
437 $("#download_txt_btn").attr("href", $("#download_txt_btn").attr("href") + encodeURIComponent(down_txt.join("")));
438 $("#download_link").val($("#download_link").val() + files.join(""));
439 $("#download_ui").show();
440 this.set_center($("#download_ui"));
444 set_down_url:function(){
447 $('<iframe>').attr('src', '//webapi.115.com/bridge_2.0.html?namespace=DownBridge&api=jQuery').css({
453 position: 'absolute',
455 }).one('load',function(){
456 window.DownBridge.getFileUrl=function(pickcode,callback){
457 this.jQuery.get('//webapi.115.com/files/download?pickcode=' + pickcode, function (data) {
461 window.DownBridge.getFileList=function(cate_id,callback){
462 this.jQuery.get('//webapi.115.com/files?aid=1&limit=1000&show_dir=1&cid=' + cate_id, function (data) {
469 get_version: function() {
472 "method": "aria2.getVersion",
476 if ($("#rpc_token").val()) {
477 data.params.unshift("token:" + $("#rpc_token").val());
479 var parameter = {'url': url, 'dataType': 'json', type: 'POST', data: JSON.stringify(data), 'headers': {'Authorization': auth}};
480 HttpSendRead(parameter)
481 .done(function(xml, textStatus, jqXHR) {
482 $("#send_test").html("ARIA2\u7248\u672c\u4e3a\uff1a\u0020" + xml.result.version);
484 .fail(function(jqXHR, textStatus, errorThrown) {
485 $("#send_test").html("错误,请查看是否开启Aria2");
489 aria2_rpc: function(file_list) {
491 if (file_list.length > 0) {
492 var length = file_list.length;
493 for (var i = 0; i < length; i++) {
496 "method": "aria2.addUri",
497 "id": new Date().getTime(),
498 "params": [[file_list[i].link], {
499 "out": file_list[i].name,
500 "dir":$("#setting_aria2_dir").val()||null,
501 "header": combination.header()
505 if ($("#rpc_token").val()) {
506 rpc_data.params.unshift("token:" + $("#rpc_token").val());
508 self.aria2send_data(rpc_data);
513 aria2send_data: function(data) {
514 var parameter = {'url': url, 'dataType': 'json', type: 'POST', data: JSON.stringify(data), 'headers': {'Authorization': auth}};
515 HttpSendRead(parameter)
516 .done(function(json, textStatus, jqXHR) {
517 SetMessage("下载成功!赶紧去看看吧~", "inf");
520 .fail(function(jqXHR, textStatus, errorThrown) {
521 SetMessage("下载失败!是不是没有开启aria2?", "err");
529 var setting_css= function() {/*
530 .download-mgr-dialog{
536 font-family: tahoma, arial, 宋体, 'Microsoft Yahei', Simsun;
538 #setting_div_table input{
539 border: 1px solid #C6C6C6;
540 box-shadow: 0 0 3px #C6C6C6;
541 -webkit-box-shadow: 0 0 3px #C6C6C6;
552 display: inline-block;
554 padding: 0 10px 0 10px;
566 text-decoration: none;
569 #setting_div_table input[disabled]{
571 background-color: #eee;
574 display:inline-block;
575 border:1px solid #D1D1D1;
576 background-color: #F7F7F7;
577 text-align: center; text-decoration: none;
581 display:inline-block;
583 #setting_aria2_headers{
588 border: 1px solid #C6C6C6;
589 box-shadow: 0 0 3px #C6C6C6;
590 -webkit-box-shadow: 0 0 3px #C6C6C6;
593 display:inline-block;
597 text-decoration: none;
599 background: -webkit-linear-gradient(top, #52C035, #4BAC32);
600 background: -moz-linear-gradient(top, #52C035, #4BAC32);
601 background: -o-linear-gradient(top, #52C035, #4BAC32);
602 filter: progid:DXImageTransform.Microsoft.gradient(startColorstr='#52C035', endColorstr='#4BAC32');
608 .download-mgr-dialog .content {
616 border-collapse:separate;
619 background-color: rgb(250, 250, 250);
622 }.toString().slice(15, -4);
623 if(document.querySelector("iframe[rel='wangpan']")&&top.location==location){
624 document.querySelector("iframe[rel='wangpan']").addEventListener('load',function(){
625 var root=document.querySelector("iframe[rel='wangpan']").contentDocument;
626 var script = document.createElement('script');
627 script.id = "pan_115_script";
628 script.appendChild(document.createTextNode('(' + pan_115 + ')();'));
629 if(document.querySelector("#pan_115_script") == null){
630 (document.body || document.head || document.documentElement).appendChild(script);
631 var style = document.createElement('style');
632 style.setAttribute('type', 'text/css');
633 style.textContent = setting_css;
634 document.head.appendChild(style);
638 var script = document.createElement('script');
639 script.id = "pan_115_script";
640 script.appendChild(document.createTextNode('(' + pan_115 + ')();'));
641 if(document.querySelector("#pan_115_script") == null){
642 (document.body || document.head || document.documentElement).appendChild(script);
643 var style = document.createElement('style');
644 style.setAttribute('type', 'text/css');
645 style.textContent = setting_css;
646 document.head.appendChild(style);