new file: pixi.toml
[GalaxyCodeBases.git] / js / ThunderLixianExporter / ThunderLixianExporter.lx3.js
blobcbd390c6aa26cb983aa45ba3894f12f92376d30b
1 // vim: set et sw=2 ts=2 sts=2 ff=unix fenc=utf8:
2 // Author: Binux<i@binux.me>
3 // http://binux.me
4 // Created on 2013-12-27 23:00:34
6 TLE = {};
8 TLE.exporter = {
9 '复制链接': function(todown) {
10 //console.log(todown);
11 var str = '<ul style="max-height: 300px; overflow-y: scroll; overflow-x: hidden;">';
12 $.each(todown.tasklist, function(n, task) {
13 $.each(task.filelist, function(l, file) {
14 if (!file.downurl) return;
15 str += '<li><a href="'+TLE.url_rewrite(file.downurl, TLE.safe_title(file.title))+'" target="_blank">'+file.title+'</a></li>';
16 });
17 });
18 str += "</ul>";
19 TLE.window_pop('复制选中的链接 &gt; <a href="'+"data:text/html;charset=utf-8,"+encodeURIComponent(str)+'" target="_blank">在新窗口中打开</a>', str);
21 'Aria2': function(todown) {
22 //console.log(todown);
23 var str = "";
24 $.each(todown.tasklist, function(n, task) {
25 $.each(task.filelist, function(l, file) {
26 if (!file.downurl) return;
27 var filepath = TLE.safe_title(file.title);
28 if (task.tasktype === 0 && task.filelist.length > 1)
29 filepath = TLE.safe_title(task.taskname) + "/" + TLE.safe_title(file.title.replace(/\\+\*?/g,"/"));
30 str += "aria2c -c -s10 -x10 --out "+TLE.escape_command(filepath)+" --header 'Cookie: gdriveid="+todown.gdriveid+";' '"+file.downurl+"'\n";
31 });
32 });
33 TLE.text_pop("aria2 download command", str);
35 'wget': function(todown) {
36 //console.log(todown);
37 var str = "";
38 $.each(todown.tasklist, function(n, task) {
39 $.each(task.filelist, function(l, file) {
40 if (!file.downurl) return;
41 str += "wget -c -O "+TLE.escape_command(TLE.safe_title(file.title))+" --header 'Cookie: gdriveid="+todown.gdriveid+";' '"+file.downurl+"'\n";
42 });
43 });
44 TLE.text_pop("wget download command", str);
46 "YAAW": function(todown) {
47 if (TLE.getConfig("TLE_aria2_jsonrpc")) {
48 TLE.tip("添加中...到YAAW界面查看是否添加成功");
49 var aria2 = new ARIA2(TLE.getConfig("TLE_aria2_jsonrpc"));
50 $.each(todown.tasklist, function(n, task) {
51 $.each(task.filelist, function(l, file) {
52 if (!file.downurl) return;
53 var filepath = TLE.safe_title(file.title);
54 if (task.tasktype === 0 && task.filelist.length > 1)
55 filepath = TLE.safe_title(task.taskname) + "/" + TLE.safe_title(file.title.replace(/\\+\*?/g,"/"));
56 aria2.addUri(file.downurl, {out: filepath, header: 'Cookie: gdriveid='+todown.gdriveid});
57 });
58 });
59 TLE.hide_tip();
60 } else {
61 TLE.tip("尚未设置Aria2 JSONRPC地址", 5);
64 'Aria2导出': function(todown) {
65 //console.log(todown);
66 var str = "";
67 $.each(todown.tasklist, function(n, task) {
68 $.each(task.filelist, function(l, file) {
69 if (!file.downurl) return;
70 var filepath = TLE.safe_title(file.title);
71 if (task.tasktype === 0 && task.filelist.length > 1)
72 filepath = TLE.safe_title(task.taskname) + "/" + TLE.safe_title(file.title.replace(/\\+\*?/g,"/"));
73 str += file.downurl+'\r\n out='+filepath+'\r\n header=Cookie: gdriveid='+todown.gdriveid+'\r\n continue=true\r\n max-connection-per-server=5\r\n split=10\r\n parameterized-uri=true\r\n\r\n';
74 });
75 });
76 TLE.file_pop("Aria2导出文件下载", str, "aria2.down");
78 'IDM导出': function(todown) {
79 //console.log(todown);
80 var str = "";
81 $.each(todown.tasklist, function(n, task) {
82 $.each(task.filelist, function(l, file) {
83 if (!file.downurl) return;
84 str += '<\r\n'+TLE.url_rewrite(file.downurl, TLE.safe_title(file.title))+'\r\ncookie: gdriveid='+todown.gdriveid+'\r\n>\r\n';
85 });
86 });
87 TLE.file_pop("IDM导出文件下载", str, "idm.ef2");
89 'Orbit导出': function(todown) {
90 //console.log(todown);
91 var str = "";
92 $.each(todown.tasklist, function(n, task) {
93 $.each(task.filelist, function(l, file) {
94 if (!file.downurl) return;
95 str += TLE.url_rewrite(file.downurl, TLE.safe_title(file.title))+'|'+TLE.safe_title(file.title.replace("|", "_"))+'||gdriveid='+todown.gdriveid+'\r\n';
96 });
97 });
98 TLE.file_pop("Orbit导出文件下载", str, "orbit.olt");
100 'eagleget导出': function(todown) {
101 var ret = {tasks: []};
102 $.each(todown.tasklist, function(n, task) {
103 $.each(task.filelist, function(l, file) {
104 if (!file.downurl) return;
105 ret.tasks.push({
106 cookie: 'gdriveid='+todown.gdriveid,
107 fname: TLE.safe_title(file.title),
108 url: TLE.url_rewrite(file.downurl, TLE.safe_title(file.title))
112 TLE.file_pop("Eagleget导出文件下载(test)", JSON.stringify(ret), "eagleget.eg");
116 (function(TLE) {
117 function get_taskinfo(taskid) {
118 return {
119 taskname: $("#tr_c"+taskid+" .title .w_title").attr("title"),
120 title: $("#tr_c"+taskid+" .title .w_title").attr("title"),
121 f_url: $("#f_url"+taskid).val(),
122 downurl: $("#dl_url"+taskid).val(),
123 cid: $("#dcid"+taskid).val(),
124 gcid: $("#gcid"+taskid).val(),
125 size: parseInt($("#ysfilesize"+taskid).val()),
126 tasktype: parseInt($("#d_tasktype"+taskid).val()),
127 status: $("#d_status"+taskid).val(),
130 function get_bt_taskinfo(taskid) {
131 return {
132 title: $("#bt_taskname"+taskid).val(),
133 f_url: $("#bturl"+taskid).val(),
134 downurl: $("#btdownurl"+taskid).val(),
135 cid: $("#btcid"+taskid).val(),
136 gcid: $("#btgcid"+taskid).val(),
137 size: $("#bt_filesize"+taskid).val(),
138 status: $("#btd_status"+taskid).val(),
142 function build_bt_taskinfo(info, rdata) {
143 var taskinfo = {
144 'taskname': info.taskname,
145 'f_url': info.f_url,
146 'cid': info.dcid,
147 'size': parseInt(info.ysfilesize),
148 'tasktype': parseInt(info.d_tasktype),
149 'status': info.d_status,
151 var filelist = [];
152 $.each(rdata, function(n, e) {
153 filelist.push({
154 'title': e.title,
155 'f_url': e.url,
156 'downurl': e.downurl,
157 'cid': e.cid,
158 'gcid': e.gcid,
159 'size': parseInt(e.filesize),
162 taskinfo['filelist'] = filelist;
163 return taskinfo;
166 TLE.safe_title = function safe_title(title) {
167 return title.replace(/[\\\|\:\*\"\?\<\>]/g,"_");
170 TLE.bt_down = function(_this, _do) {
171 var ck = document.getElementsByName("bt_list_ck");
172 var files = [];
173 $.each(ck, function(n, e) {
174 if (e.checked == false) return;
175 var fid = e.getAttribute("_i");
176 var file = {
177 'title': $("#bt_taskname"+fid).val(),
178 'url': $("#bturl"+fid).val(),
179 'downurl': $("#btdownurl"+fid).val(),
180 'cid': $("#btcid"+fid).val(),
181 'gcid': $("#btgcid"+fid).val(),
182 'filesize': $("#bt_filesize"+fid).val(),
184 files.push(file);
186 var taskid = $("#view_bt_taskid").val();
187 var info = get_taskinfo($("#tr_c"+taskid));
189 var todown = {};
190 todown.gdriveid = $("#cok").val() || getCookie("gdriveid");
191 todown.tasklist = {};
192 todown.tasklist[taskid] = build_bt_taskinfo(info, files);
193 //console.log(todown);
195 _do(todown);
197 //console.log("bt_down");
200 TLE.text_pop = function(title, content) {
201 content = $('<div></div>').text(content).html()
202 content = '<textarea style="width: 100%; height: 260px;">'+content+'</textarea>'
203 $("#TLE_text_pop").tpl("TLE_text_tpl", {'title': title, 'content': content}).show().pop({
204 //onHide: function() { $(document.body).click(); },
207 TLE.file_pop = function(title, content, filename) {
208 var url = "data:text/html;charset=utf-8,"+encodeURIComponent(content);
209 if (isChrome) {
210 $('<a href="'+url+'" target="_blank" style="display:none;" download="'+filename+'"></a>').appendTo('body').get(0).click();
211 } else {
212 var content = '<div style="width: 100%; height: 100px;">'
213 +'<div style="padding: 30px 0 0 30%;">'
214 +'<a href="'+url+'" target="_blank" title="右键另存为" class="pop_btn" download="'+filename+'"><span><em class="TLE_icdwlocal">导出文件</em></span></a>'
215 +(isChrome ? '' : '(右键另存为'+filename+')')
216 +'</div>'
217 +'</div>'
218 $("#TLE_text_pop").tpl("TLE_text_tpl", {'title': title, 'content': content}).show().pop({
219 //onHide: function() { $(document.body).click(); },
223 TLE.window_pop = function(title, content) {
224 $("#TLE_text_pop").tpl("TLE_text_tpl", {'title': title, 'content': content}).show().pop({
225 //onHide: function() { $(document.body).click(); },
228 TLE.tip = function(content, time) {
229 TS2.show(content, time);
231 TLE.hide_tip = function() {
232 TS2.hide();
235 TLE.multiple_server_fix = function(url) {
236 return "'"+url.replace("gdl", "'{gdl,dl.{f,g,h,i,twin}}'")+"'";
239 function encode_utf8(s) {
240 return unescape( encodeURIComponent( s ) );
242 function to_hex(num) {
243 var s = num.toString(16);
244 if (s.length == 1)
245 return '0'+s;
246 else
247 return s;
249 var thunder_filename_mask = [0x61, 0x31, 0xe4, 0x5f, 0x00, 0x00, 0x00, 0x00];
250 function thunder_filename_encode(filename) {
251 var result = ["01", ];
252 $.each(encode_utf8(filename), function(i, n) {
253 result.push(to_hex(n.charCodeAt(0)^thunder_filename_mask[i%8]).toUpperCase())
255 while (result.length % 8 != 1) {
256 result.push(to_hex(thunder_filename_mask[(result.length-1)%8]).toUpperCase());
258 return result.join("");
261 TLE.url_rewrite = function(url, filename) {
262 url = url.replace(/&n=\w+/, "&n="+thunder_filename_encode(filename));
263 return url;
266 var alpha = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
267 TLE.escape_command = function(str) {
268 var result = "";
269 for (var i = 0; i < str.length; i++) {
270 if (alpha.indexOf(str[i]) == -1)
271 result += "\\"+str[i];
272 else
273 result += str[i];
275 return result;
278 //setting
279 TLE.getConfig = function(key) {
280 if (window.localStorage) {
281 return window.localStorage.getItem(key) || "";
282 } else {
283 return getCookie(key);
286 TLE.setConfig = function(key, value) {
287 if (window.localStorage) {
288 window.localStorage.setItem(key, value);
289 } else {
290 setGdCookie(key, value, 86400*365);
293 //set default config
294 if (TLE.getConfig("TLE_exporter") == "") {
295 var exporters = [];
296 for (var key in TLE.exporter) {
297 exporters.push(key);
299 TLE.setConfig("TLE_exporter", exporters.join("|"));
302 function init() {
303 //css
304 $("head").append('<style>'
305 +'#TLE_setting { padding: 10px 20px 20px 20px; }'
306 +'#TLE_setting span { padding-left: 1em; }'
307 +'#TLE_setting li { margin-top: 0.5em; }'
308 +'#TLE_exporter_select { padding: 10px 20px 20px 20px; }'
309 +'</style>');
310 //pop
311 $("body").append('<div id="TLE_text_pop" class="lixian_pop_wp pop_w400" style="display:none;margin:0;"></div>');
312 $("body").append('<textarea id="TLE_text_tpl" style="display: none;"></textarea>');
313 $("#TLE_text_tpl").text('<div class="lixian_pop">'
314 +'<div class="l_p_hd">'
315 +'<h3>$[title]</h3>'
316 +'</div>'
317 +'<div class="l_p_bd">'
318 +'$[content]'
319 +'</div>'
320 +'<a class="pop_close" title="关闭浮层" close="1" id="close">关闭浮层</a>'
321 +'</div>');
322 //setting
323 $("#view_down_bar ul").prepend('<li> <a href="#" id="TLE_setting_anchor">TLE设置</a></li>');
324 $("#TLE_setting_anchor").on("click", function() {
325 var content = '<div id="TLE_setting"><ul>'
326 +'<li><b>启用以下导出器</b></li>'
327 +'<li>'+(function(){
328 var enabled_exporter = TLE.getConfig("TLE_exporter").split("|");
329 var str = '';
330 for (var name in TLE.exporter) {
331 str += '<span class="rw_col"><input type="checkbox" class="TLE_setting_ck" name="TLE_ck_'+name+'" '+(enabled_exporter.indexOf(name) == -1 ? "" : "checked")+' />'+name+'</span>';
333 return str;
334 })()+'</li>'
335 +'<li><b>Aria2 JSON-RPC Path</b></li>'
336 +'<li>Path: <input type="text" id="TLE_aria2_jsonrpc" style="width: 350px" value="'+TLE.getConfig("TLE_aria2_jsonrpc")+'"/></li>'
337 +'</ul>'
338 +'</div>'
339 +'</div>'
340 +'<div class="l_p_ft"><div class="btn_area"><a id="TLE_setting_ok" class="pop_btn">确定</a> <a close="1" class="pop_btn pop_btn_cancel">取消</a></div></div>';
341 $("#TLE_text_pop").tpl("TLE_text_tpl", {title: "Thunder Lixian Exporter 设定", content: content}).show().pop();
342 $("#TLE_setting_ok").on("click", function() {
343 var enabled_exporter = [];
344 $(".TLE_setting_ck").each(function(n, e) {
345 if (e.checked) enabled_exporter.push(e.name.replace(/^TLE_ck_/, ""));
347 var config_str = (enabled_exporter.length == 0) ? "_" : enabled_exporter.join("|");
348 var jsonrpc_path = $("#TLE_aria2_jsonrpc").val();
349 if (TLE.getConfig("TLE_exporter") != config_str || TLE.getConfig("TLE_aria2_jsonrpc") != jsonrpc_path) {
350 TLE.setConfig("TLE_exporter", config_str);
351 TLE.setConfig("TLE_aria2_jsonrpc", jsonrpc_path);
353 $("a.pop_close:visible").click();
354 TLE.tip("配置已保存", 5);
357 //download binding
358 function exporter_anchors() {
359 var enabled_exporter = TLE.getConfig("TLE_exporter").split("|");
360 var str = '';
361 $.each(TLE.exporter, function(n, f) {
362 if (enabled_exporter.indexOf(n) == -1) return;
363 str+=('<li><a href="#" title="'+n+'" onclick="TLE.exporter[\''+n+'\'](TLE.todown);return false;">'+n+'</a></li>');
365 return str;
367 function show_exporter_selector() {
368 $("#TLE_text_pop").tpl("TLE_text_tpl", {title: "您正在使用Thunder Lixian Exporter",
369 content: '<ul id="TLE_exporter_select">'
370 +exporter_anchors()
371 +'</ul>'}).pop();
374 TLE.todown = {};
375 window.thunder_download = function(taskid, type) {
376 TLE.todown = {};
377 TLE.todown.gdriveid = $("#cok").val() || getCookie("gdriveid");
378 if (type === 1) {
379 // bt_down_one
380 var taskinfo = {
381 taskname: $("#bt_info_list .title .w").text(),
382 f_url: null,
383 cid: null,
384 size: null,
385 tasktype: 0,
386 status: 2,
388 var filelist = [];
389 filelist.push(get_bt_taskinfo(taskid));
390 taskinfo['filelist'] = filelist;
391 TLE.todown.tasklist = {};
392 TLE.todown.tasklist['0'] = taskinfo;
393 } else {
394 // down
395 var taskinfo = get_taskinfo(taskid);
396 var filelist = [];
397 filelist.push(get_taskinfo(taskid));
398 taskinfo['filelist'] = filelist;
399 TLE.todown.tasklist = {};
400 TLE.todown.tasklist[taskid] = taskinfo;
402 show_exporter_selector();
404 window.bt_task_down = function(cid, taskid) {
405 // bt_down
406 batch_down_all_f([taskid, ]);
408 window.batch_down_all_f = function(taskids) {
409 // batch_down
410 if (!taskids) {
411 taskids = [];
412 $("span[name=ck][checked]").each(function(n, e) {
413 taskids.push($(e).attr("value"));
417 var bt_task_list = [], normal_task_list = [];
418 $.each(taskids, function(n, taskid) {
419 var d_status = $("#d_status"+taskid).val();
420 var d_tasktype = parseInt($("#d_tasktype"+taskid).val());
421 var d_flag = $("#dflag"+taskid).val();
422 if (d_flag != 4 && d_status == 2) {
423 if (d_tasktype == 0) {
424 bt_task_list.push(taskid);
425 } else {
426 normal_task_list.push(taskid);
431 if (bt_task_list.length) {
432 TLE.tip("载入中...");
433 $.getJSON(INTERFACE_URL+"/fill_bt_list?tid="+bt_task_list.join(",")+"&g_net="+G_section+"&uid="+G_USERID+"&callback=?", function(data) {
434 TLE.hide_tip();
435 var todown = {};
436 todown.gdriveid = $("#cok").val() || getCookie("gdriveid");
437 todown.tasklist = {};
438 $.each(data['Result'], function(n, e) {
439 var taskinfo = get_taskinfo(n);
440 var filelist = [];
441 $.each(e, function(n, e) {
442 filelist.push({
443 title: e.title,
444 f_url: e.url,
445 downurl: e.downurl,
446 cid: e.cid,
447 gcid: e.gcid,
448 size: parseInt(e.filesize),
451 taskinfo.filelist = filelist;
452 todown.tasklist[n] = taskinfo;
454 $.each(normal_task_list, function(n, e) {
455 var taskinfo = get_taskinfo(e);
456 taskinfo['filelist'] = taskinfo;
457 todown.tasklist[e] = taskinfo;
459 TLE.todown = todown;
460 show_exporter_selector();
462 } else {
463 var todown = {};
464 todown.gdriveid = $("#cok").val() || getCookie("gdriveid");
465 todown.tasklist = {};
466 $.each(normal_task_list, function(n, e) {
467 var taskinfo = get_taskinfo(e);
468 taskinfo['filelist'] = taskinfo;
469 todown.tasklist[e] = taskinfo;
471 TLE.todown = todown;
472 show_exporter_selector();
475 window.batch_down_bt = function() {
476 var taskids = [];
477 $("span[name=bt_list_ck][checked]").each(function(n, e) {
478 var taskid = $(e).attr("value");
479 if ($("#btd_status"+taskid).val() == 2)
480 taskids.push(taskid);
483 TLE.todown = {};
484 TLE.todown.gdriveid = $("#cok").val() || getCookie("gdriveid");
485 var taskinfo = {
486 taskname: $("#bt_info_list .title .w").text(),
487 f_url: null,
488 cid: null,
489 size: null,
490 tasktype: 0,
491 status: 2,
493 var filelist = [];
494 $.each(taskids, function(n, e) {
495 filelist.push(get_bt_taskinfo(e));
497 taskinfo['filelist'] = filelist;
498 TLE.todown.tasklist = {};
499 TLE.todown.tasklist['0'] = taskinfo;
500 show_exporter_selector();
504 init();
505 })(TLE);
507 var ARIA2 = (function() {
508 var jsonrpc_version = '2.0';
510 function get_auth(url) {
511 return url.match(/^(?:(?![^:@]+:[^:@\/]*@)[^:\/?#.]+:)?(?:\/\/)?(?:([^:@]*(?::[^:@]*)?)?@)?/)[1];
514 function request(jsonrpc_path, method, params) {
515 var xhr = new XMLHttpRequest();
516 var auth = get_auth(jsonrpc_path);
517 jsonrpc_path = jsonrpc_path.replace(/^((?![^:@]+:[^:@\/]*@)[^:\/?#.]+:)?(\/\/)?(?:(?:[^:@]*(?::[^:@]*)?)?@)?(.*)/, '$1$2$3'); // auth string not allowed in url for firefox
519 var request_obj = {
520 jsonrpc: jsonrpc_version,
521 method: method,
522 id: (new Date()).getTime().toString(),
524 if (params) request_obj['params'] = params;
525 if (auth && auth.indexOf('token:') == 0) params.unshift(auth);
527 xhr.open("POST", jsonrpc_path+"?tm="+(new Date()).getTime().toString(), true);
528 xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
529 if (auth && auth.indexOf('token:') != 0) {
530 xhr.setRequestHeader("Authorization", "Basic "+btoa(auth));
532 xhr.send(JSON.stringify(request_obj));
535 return function(jsonrpc_path) {
536 this.jsonrpc_path = jsonrpc_path;
537 this.addUri = function (uri, options) {
538 request(this.jsonrpc_path, 'aria2.addUri', [[uri, ], options]);
540 return this;
542 })();