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.2";
12 var update_date
= "2015/06/04";
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() {/*
120 @media (min-width: 1240px){
126 padding: 0 10px 0 10px;
140 padding: 0 10px 0 10px;
150 @media (max-width: 1000px){
156 padding: 0 10px 0 10px;
170 padding: 0 10px 0 10px;
180 li[rel="item"]:hover .show-export-button {
186 }.toString().slice(15, -4);
187 var url
= (localStorage
.getItem("rpc_url") || "http://localhost:6800/jsonrpc") + "?tm=" + (new Date().getTime().toString());
194 self
.set_config_ui();
196 SetMessage("载入成功!", "inf");
199 //设置导出按钮的触发 js_top_panel_box
202 var root
=document
.querySelector("iframe[rel='wangpan']").contentDocument
;
204 document
.querySelector("iframe[rel='wangpan']").addEventListener('load',function(){
207 var setting_div
=$("<a>").text("插件设置").attr("href","javascript:;");
208 setting_div
.appendTo('.tup-logout');
209 setting_div
.on('click',function(){
210 $("#setting_div").show();
211 $("#setting_divtopmsg").html("");
212 self
.set_center($("#setting_div"));
214 function top_panel_box_btn(){
215 var root
=document
.querySelector("iframe[rel='wangpan']").contentDocument
;
216 $("<div>").text("RPC下载").addClass("btn-aria2c").on('click',function(){
217 self
.aria2_export(true);
218 }).appendTo($(root
).find("#js_top_panel_box"));
219 $("<div>").text("导出下载").addClass("btn-txt").on('click',function(){
220 self
.aria2_download();
221 self
.aria2_export(false);
222 }).appendTo($(root
).find("#js_top_panel_box"));
223 var style
= document
.createElement('style');
224 style
.setAttribute('type', 'text/css');
225 style
.textContent
= css
;
226 root
.head
.appendChild(style
);
229 set_config_ui:function(){
231 var setting_div
= document
.createElement("div");
232 setting_div
.className
= "download-mgr-dialog dialog-box";
233 setting_div
.id
= "setting_div";
235 '<h2 class="dialog-title" ><span rel="base_title">导出设置</span><div class="dialog-handle"><a href="javascript:;" class="diag-close" btn="close">关闭</a></div></h2>',
236 '<div style=" margin: 20px 10px 10px 10px; ">',
237 '<div id="setting_divtopmsg" style="position:absolute; margin-top: -18px; margin-left: 10px; color: #E15F00;"></div>',
238 '<table id="setting_div_table" >',
240 '<tr><td width="100"><label>ARIA2 RPC:</label></td><td><input id="rpc_input" type="text" class="input-large"></td></tr>',
241 '<tr><td><label>RPC访问设置</label></td><td><input id="rpc_distinguish" type="checkbox"></td></tr>',
242 '<tr><td><label >RPC 用户名:</label></td><td><input type="text" id="rpc_user" disabled="disabled" class="input-small"></td></tr>',
243 '<tr><td><label>RPC 密码:</label></td><td><input type="text" id="rpc_pass" disabled="disabled" class="input-small"></td></tr>',
244 '<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>',
245 '<tr><td><label>下载路径:</label></td><td><input type="text" placeholder="只能设置为绝对路径" id="setting_aria2_dir" class="input-large"></td></tr>',
246 '<tr><td><label>User-Agent :</label></td><td><input type="text" id="setting_aria2_useragent_input" class="input-large"></td></tr>',
247 '<tr><td><label>Referer :</label></td><td><input type="text" id="setting_aria2_referer_input" class="input-large"></td></tr>',
248 '<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>',
249 '<tr><td><label>headers :</label></td><td><textarea id="setting_aria2_headers" ></textarea></td></tr>',
252 '<div style="margin-top:10px;">',
253 '<div id="copyright">© Copyright <a href="https://github.com/acgotaku/115">雪月秋水 </a> Version:' + version
+ ' 更新日期: ' + update_date
+ ' </div>',
254 '<div style="margin-left:20px; display:inline-block"><a href="javascript:;" id="apply" class="button" ><b>应用</b></a></div>',
258 setting_div
.innerHTML
= html_
.join("");
259 document
.body
.appendChild(setting_div
);
260 $(".diag-close").click(function() {
261 $("#setting_div").hide();
263 $("#apply").click(function() {
265 $("#setting_divtopmsg").html("设置已保存.");
267 $("#send_test").click(function() {
270 $("#rpc_distinguish").change(function() {
271 if ($(this).is(":checked")) {
272 $("#rpc_user").removeAttr("disabled").css("background-color", "#FFF");
273 $("#rpc_pass").removeAttr("disabled").css("background-color", "#FFF");
275 $("#rpc_user").attr({"disabled": "disabled"}).css("background-color", "#eee");
276 $("#rpc_pass").attr({"disabled": "disabled"}).css("background-color", "#eee");
282 set_config: function() {
283 $("#rpc_input").val((localStorage
.getItem("rpc_url") || "http://localhost:6800/jsonrpc"));
284 $("#rpc_token").val(localStorage
.getItem("rpc_token"));
285 $("#setting_aria2_dir").val(localStorage
.getItem("rpc_dir"));
286 $("#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");
287 $("#setting_aria2_referer_input").val(localStorage
.getItem("referer") || "http://115.com/");
288 $("#setting_aria2_headers").val(localStorage
.getItem("rpc_headers"));
290 if (localStorage
.getItem("auth") == "true") {
291 var rpc_user
= localStorage
.getItem("rpc_user");
292 var rpc_pass
= localStorage
.getItem("rpc_pass");
293 $("#rpc_user").val(rpc_user
);
294 $("#rpc_pass").val(rpc_pass
);
295 $("#rpc_distinguish").prop('checked', true).trigger("change");
296 auth
= "Basic " + btoa(rpc_user
+ ":" + rpc_pass
);
299 $("#rpc_user, #rpc_pass").val("");
303 get_config: function() {
304 var rpc_url
= $("#rpc_input").val();
306 localStorage
.setItem("rpc_url", rpc_url
);
307 url
= rpc_url
+ "?tm=" + (new Date().getTime().toString());
309 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");
310 if ($("#rpc_distinguish").prop('checked') == true) {
311 localStorage
.setItem("rpc_user", $("#rpc_user").attr("value"));
312 localStorage
.setItem("rpc_pass", $("#rpc_pass").attr("value"));
313 localStorage
.setItem("auth", true);
314 auth
= "Basic " + btoa($("#rpc_user").attr("value") + ":" + $("#rpc_pass").attr("value"));
316 localStorage
.setItem("auth", false);
317 localStorage
.setItem("rpc_user", null);
318 localStorage
.setItem("rpc_pass", null);
320 localStorage
.setItem("rpc_token", $("#rpc_token").val());
321 localStorage
.setItem("rpc_dir", $("#setting_aria2_dir").val());
322 localStorage
.setItem("rpc_headers", $("#setting_aria2_headers").val());
323 localStorage
.setItem("referer", $("#setting_aria2_referer_input").val());
325 set_center:function(obj
){
326 var screenWidth
= $(window
).width(), screenHeight
= $(window
).height();
327 var scrolltop
= $(document
).scrollTop();
328 var objLeft
= (screenWidth
- obj
.width())/2 ;
329 var objTop
= (screenHeight
- obj
.height())/2 + scrolltop
;
330 obj
.css({left
: objLeft
+ 'px', top
: objTop
+ 'px'});
332 getFileInfo:function(pick_code
,method
,path
){
335 DownBridge
.getFileUrl(pick_code
,function(data
){
337 file_list
.push({"name": (path
||"")+$('<textarea />').html(data
.file_name
).text(), "link": data
.file_url
});
339 self
.aria2_rpc(file_list
);
341 $("#download_ui").show();
342 self
.aria2_data(file_list
);
348 aria2_export:function(method
){
350 var root
=document
.querySelector("iframe[rel='wangpan']").contentDocument
;
351 $(root
).find('li[rel="item"][file_type="1"]').each(function(){
352 if($(this).children().eq(3).prop('checked') == true){
353 var pick_code
= $(this).attr('pick_code');
354 self
.getFileInfo(pick_code
,method
);
357 $(root
).find('li[rel="item"][file_type="0"]').each(function(){
358 if($(this).children().eq(2).prop('checked') == true){
359 var cate_id
= $(this).attr('cate_id');
360 DownBridge
.getFileList(cate_id
,function(data
){
362 for(var i
=0;i
<list
.length
;i
++){
364 self
.getFileInfo(list
[i
].pc
,method
,data
.path
[data
.path
.length
-1].name
+"/");
366 var dir_level
=data
.path
.length
-1;
367 self
.get_all_dir(list
[i
].cid
,dir_level
,method
);
375 get_all_dir:function(cid
,dir_level
,method
){
377 DownBridge
.getFileList(cid
,function(data
){
380 for(var i
=dir_level
;i
<data
.path
.length
;i
++){
381 path
+=data
.path
[i
].name
+"/";
383 for(var i
=0;i
<list
.length
;i
++){
385 self
.getFileInfo(list
[i
].pc
,method
,path
);
387 self
.get_all_dir(list
[i
].cid
,dir_level
,method
);
393 aria2_download: function() {
394 if ($("#download_ui").length
== 0) {
395 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>');
396 var content_ui
= $("<div>").addClass("content").attr("id", "content_ui").appendTo(download_ui
);
397 download_ui
.appendTo($("body"));
399 var download_menu
= $("<div>").css({"display": "block", "margin-bottom": "10px"}).appendTo(content_ui
);
400 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
);
401 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
);
402 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
);
403 var download_link
= $("<textarea>").attr("wrap", "off").attr("id", "download_link").css({"white-space": "nowrap", "width": "100%", "overflow": "scroll", "height": "180px"});
404 download_link
.appendTo(content_ui
);
405 $(".diag-close").click(function() {
409 $("#aria2c_btn, #idm_btn, #download_txt_btn").attr("href", "data:text/plain;charset=utf-8,");
410 $("#download_link").val("");
414 aria2_data: function(file_list
) {
419 if (file_list
.length
> 0) {
420 var length
= file_list
.length
;
421 for (var i
= 0; i
< length
; i
++) {
422 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");
425 combination
.header("aria2c_txt"),
426 ' out=' + file_list
[i
].name
,
428 ' max-connection-per-server=10',
435 ' cookie: ' + cookies
,
436 ' out=' + file_list
[i
].name
,
439 down_txt
.push([file_list
[i
].link
, ' '].join('\n'));
441 $("#aria2c_btn").attr("href", $("#aria2c_btn").attr("href") + encodeURIComponent(aria2c_txt
.join("")));
442 $("#idm_btn").attr("href", $("#idm_btn").attr("href") + encodeURIComponent(idm_txt
.join("")));
443 $("#download_txt_btn").attr("href", $("#download_txt_btn").attr("href") + encodeURIComponent(down_txt
.join("")));
444 $("#download_link").val($("#download_link").val() + files
.join(""));
445 $("#download_ui").show();
446 this.set_center($("#download_ui"));
450 set_down_url:function(){
453 $('<iframe>').attr('src', '//webapi.115.com/bridge_2.0.html?namespace=DownBridge&api=jQuery').css({
459 position
: 'absolute',
461 }).one('load',function(){
462 window
.DownBridge
.getFileUrl=function(pickcode
,callback
){
463 this.jQuery
.get('//webapi.115.com/files/download?pickcode=' + pickcode
, function (data
) {
467 window
.DownBridge
.getFileList=function(cate_id
,callback
){
468 this.jQuery
.get('//webapi.115.com/files?aid=1&limit=1000&show_dir=1&cid=' + cate_id
, function (data
) {
475 get_version: function() {
478 "method": "aria2.getVersion",
482 if ($("#rpc_token").val()) {
483 data
.params
.unshift("token:" + $("#rpc_token").val());
485 var parameter
= {'url': url
, 'dataType': 'json', type
: 'POST', data
: JSON
.stringify(data
), 'headers': {'Authorization': auth
}};
486 HttpSendRead(parameter
)
487 .done(function(xml
, textStatus
, jqXHR
) {
488 $("#send_test").html("ARIA2\u7248\u672c\u4e3a\uff1a\u0020" + xml
.result
.version
);
490 .fail(function(jqXHR
, textStatus
, errorThrown
) {
491 $("#send_test").html("错误,请查看是否开启Aria2");
495 aria2_rpc: function(file_list
) {
497 if (file_list
.length
> 0) {
498 var length
= file_list
.length
;
499 for (var i
= 0; i
< length
; i
++) {
502 "method": "aria2.addUri",
503 "id": new Date().getTime(),
504 "params": [[file_list
[i
].link
], {
505 "out": file_list
[i
].name
,
506 "dir":$("#setting_aria2_dir").val()||null,
507 "header": combination
.header()
511 if ($("#rpc_token").val()) {
512 rpc_data
.params
.unshift("token:" + $("#rpc_token").val());
514 self
.aria2send_data(rpc_data
);
519 aria2send_data: function(data
) {
520 var parameter
= {'url': url
, 'dataType': 'json', type
: 'POST', data
: JSON
.stringify(data
), 'headers': {'Authorization': auth
}};
521 HttpSendRead(parameter
)
522 .done(function(json
, textStatus
, jqXHR
) {
523 SetMessage("下载成功!赶紧去看看吧~", "inf");
526 .fail(function(jqXHR
, textStatus
, errorThrown
) {
527 SetMessage("下载失败!是不是没有开启aria2?", "err");
535 if(document
.querySelector("iframe[rel='wangpan']")&&top
.location
==location
){
536 document
.querySelector("iframe[rel='wangpan']").addEventListener('load',function(){
537 var root
=document
.querySelector("iframe[rel='wangpan']").contentDocument
;
539 var script
= document
.createElement('script');
540 script
.id
= "pan_115_script";
541 script
.appendChild(document
.createTextNode('(' + pan_115
+ ')();'));
542 if(document
.querySelector("#pan_115_script") == null){
543 (document
.body
|| document
.head
|| document
.documentElement
).appendChild(script
);