modified: pixi.toml
[GalaxyCodeBases.git] / etc / Chrome / 115 / safari / 115.safariextension / script.js
bloba669e08c7a8cc754de74a651b5566a768040a506
1 // ==UserScript==
2 // @name 115网盘aria2导出工具
3 // @author acgotaku311
4 // @description 一个方便把115网盘导出到Aria2rpc的脚本。
5 // @encoding utf-8
6 // @include http://*.115.com/*
7 // @run-at document-end
8 // @version 0.1.2
9 // ==/UserScript==
10 var pan_115 = function(cookies) {
11 var version = "0.1.2";
12 var update_date = "2015/06/04";
13 var pan = (function() {
14 //type : inf err war
15 var SetMessage = function(msg, type) {
16 Core.MinMessage.Show({
17 text: msg,
18 type: type,
19 timeout: 2000
20 });
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";
26 var timeout = 3000;
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() {
36 http.abort();
38 deferred.promise(http);
39 http.onreadystatechange = function() {
40 if (http.readyState == 4) {
41 if ((http.status == 200 && http.status < 300) || http.status == 304) {
42 clearTimeout(timeId);
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);
51 else {
52 clearTimeout(timeId);
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") {
66 http.send(info.data);
68 else {
69 http.send();
71 return http;
73 //设置aria2c下载设置的Header信息
74 var combination = {
75 header: function(type) {
76 var addheader = [];
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]);
87 // }
88 // }
89 // addheader.push("Cookie: " + format_cookies.join(";"));
90 addheader.push("Referer: " + referer);
91 if (headers) {
92 var text = headers.split("\n");
93 for (var i = 0; i < text.length; i++) {
94 addheader.push(text[i]);
97 var header = "";
98 if (type == "aria2c_line") {
99 for (var i = 0; i < addheader.length; i++) {
100 header += " --header " + JSON.stringify(addheader[i]) + " ";
102 return header;
103 } else if (type == "aria2c_txt") {
104 for (var i = 0; i < addheader.length; i++) {
105 header += " header=" + (addheader[i]) + " \n";
107 return header;
108 } else if (type == "idm_txt") {
109 for (var i = 0; i < addheader.length; i++) {
110 header += " header=" + (addheader[i]) + " \n";
112 return header;
113 } else {
114 return addheader;
119 var css = function() {/*
120 @media (min-width: 1240px){
121 .btn-aria2c{
122 position: relative;
123 top: 8px;
124 float: right;
125 margin-right: 80px;
126 padding: 0 10px 0 10px;
127 line-height: 30px;
128 font-size: 14px;
129 color: white;
130 background: #2b91e3;
131 border-radius: 3px;
132 cursor: pointer;
133 z-index:100;
135 .btn-txt{
136 position: relative;
137 top: 8px;
138 float: right;
139 margin-right: 10px;
140 padding: 0 10px 0 10px;
141 line-height: 30px;
142 font-size: 14px;
143 color: white;
144 background: #2b91e3;
145 border-radius: 3px;
146 cursor: pointer;
147 z-index:100;
150 @media (max-width: 1000px){
151 .btn-aria2c{
152 position: relative;
153 top: 8px;
154 float: right;
155 margin-right: 20px;
156 padding: 0 10px 0 10px;
157 line-height: 30px;
158 font-size: 14px;
159 color: white;
160 background: #2b91e3;
161 border-radius: 3px;
162 cursor: pointer;
163 z-index:100;
165 .btn-txt{
166 position: relative;
167 top: 8px;
168 float: right;
169 margin-right: 10px;
170 padding: 0 10px 0 10px;
171 line-height: 30px;
172 font-size: 14px;
173 color: white;
174 background: #2b91e3;
175 border-radius: 3px;
176 cursor: pointer;
177 z-index:100;
180 li[rel="item"]:hover .show-export-button {
181 display: block;
182 cursor: pointer;
183 background:#FFD;
186 }.toString().slice(15, -4);
187 var url = (localStorage.getItem("rpc_url") || "http://localhost:6800/jsonrpc") + "?tm=" + (new Date().getTime().toString());
188 return {
189 //初始化按钮和一些事件
190 init: function() {
191 var self = this;
192 self.set_down_url();
193 self.set_btn();
194 self.set_config_ui();
195 self.set_config();
196 SetMessage("载入成功!", "inf");
198 set_btn:function(){
199 //设置导出按钮的触发 js_top_panel_box
200 //设置 设置按钮
201 var self = this;
202 var root=document.querySelector("iframe[rel='wangpan']").contentDocument;
203 top_panel_box_btn();
204 document.querySelector("iframe[rel='wangpan']").addEventListener('load',function(){
205 top_panel_box_btn();
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(){
230 var self = this;
231 var setting_div = document.createElement("div");
232 setting_div.className = "download-mgr-dialog dialog-box";
233 setting_div.id = "setting_div";
234 var html_ = [
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" >',
239 '<tbody>',
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>',
250 '</tbody>',
251 '</table>',
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>',
255 '</div>',
256 '</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() {
264 self.get_config();
265 $("#setting_divtopmsg").html("设置已保存.");
267 $("#send_test").click(function() {
268 self.get_version();
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");
274 } else {
275 $("#rpc_user").attr({"disabled": "disabled"}).css("background-color", "#eee");
276 $("#rpc_pass").attr({"disabled": "disabled"}).css("background-color", "#eee");
281 //填充已经设置的配置数据
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);
298 else {
299 $("#rpc_user, #rpc_pass").val("");
302 //保存配置数据
303 get_config: function() {
304 var rpc_url = $("#rpc_input").val();
305 if (rpc_url) {
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"));
315 } else {
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){
333 var self=this;
335 DownBridge.getFileUrl(pick_code,function(data){
336 var file_list=[];
337 file_list.push({"name": (path||"")+$('<textarea />').html(data.file_name).text(), "link": data.file_url});
338 if(method){
339 self.aria2_rpc(file_list);
340 }else{
341 $("#download_ui").show();
342 self.aria2_data(file_list);
345 });
347 //115下载核心功能 导出
348 aria2_export:function(method){
349 var self=this;
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){
361 var list =data.data;
362 for(var i=0;i<list.length;i++){
363 if(list[i].sha){
364 self.getFileInfo(list[i].pc,method,data.path[data.path.length-1].name+"/");
365 }else{
366 var dir_level=data.path.length-1;
367 self.get_all_dir(list[i].cid,dir_level,method);
374 //递归下载
375 get_all_dir:function(cid,dir_level,method){
376 var self=this;
377 DownBridge.getFileList(cid,function(data){
378 var list =data.data;
379 var path="";
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++){
384 if(list[i].sha){
385 self.getFileInfo(list[i].pc,method,path);
386 }else{
387 self.get_all_dir(list[i].cid,dir_level,method);
392 //aria2导出下载界面以及事件绑定
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"));
398 content_ui.empty();
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() {
406 download_ui.hide();
408 } else {
409 $("#aria2c_btn, #idm_btn, #download_txt_btn").attr("href", "data:text/plain;charset=utf-8,");
410 $("#download_link").val("");
413 //导出填充数据和显示数据
414 aria2_data: function(file_list) {
415 var files = [];
416 var aria2c_txt = [];
417 var idm_txt = [];
418 var down_txt = [];
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");
423 aria2c_txt.push([
424 file_list[i].link,
425 combination.header("aria2c_txt"),
426 ' out=' + file_list[i].name,
427 ' continue=true',
428 ' max-connection-per-server=10',
429 ' split=10',
430 '\n'
431 ].join('\n'));
432 idm_txt.push([
433 '<',
434 file_list[i].link,
435 ' cookie: ' + cookies,
436 ' out=' + file_list[i].name,
437 ' >'
438 ].join('\r\n'));
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(){
451 var self=this;
452 DownBridge={};
453 $('<iframe>').attr('src', '//webapi.115.com/bridge_2.0.html?namespace=DownBridge&api=jQuery').css({
454 width: 0,
455 height: 0,
456 border: 0,
457 padding: 0,
458 margin: 0,
459 position: 'absolute',
460 top: '-99999px'
461 }).one('load',function(){
462 window.DownBridge.getFileUrl=function(pickcode,callback){
463 this.jQuery.get('//webapi.115.com/files/download?pickcode=' + pickcode, function (data) {
464 callback(data);
465 }, 'json');
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) {
469 callback(data);
470 }, 'json');
472 }).appendTo('html');
474 //获取aria2c的版本号用来测试通信
475 get_version: function() {
476 var data = {
477 "jsonrpc": "2.0",
478 "method": "aria2.getVersion",
479 "id": 1,
480 "params": []
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");
494 //封装rpc要发送的数据
495 aria2_rpc: function(file_list) {
496 var self = this;
497 if (file_list.length > 0) {
498 var length = file_list.length;
499 for (var i = 0; i < length; i++) {
500 var rpc_data = {
501 "jsonrpc": "2.0",
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);
518 //和aria2c通信
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");
531 })();
532 pan.init();
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;
538 console.log(root);
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);
545 });