Enhanced dmhy

为种子列表添加支持迅雷离线的磁链,用明确的文件名直接下载种子,批量导出磁链

目前为 2016-04-07 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Enhanced dmhy
  3. // @description 为种子列表添加支持迅雷离线的磁链,用明确的文件名直接下载种子,批量导出磁链
  4. // @namespace https://greasyfork.org/users/726
  5. // @include http*://share.dmhy.org/*
  6. // @include http*://dl.dmhy.org/*
  7. // @version 20151007
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. if(location.host=='dl.dmhy.org'){
  12.  
  13. // 用一个微妙的方法改变下载文件名,其实改名的基本思路就是使用download属性设置文件名,但是download属性似乎只支持同域名下载,所以这里需要在dl.dmhy.org这个域名下生成下载链接
  14. if(location.pathname=='/' && location.hash.indexOf('download')==1){
  15. var json = location.hash.substr(10);
  16. json = decodeURIComponent(json);
  17. var argv = JSON.parse(json);
  18. if(argv){
  19. var link = document.createElement('a');
  20. link.href = argv.link;
  21. if (typeof link.download === 'string') {
  22. link.download = argv.name;
  23. }
  24. link.innerHTML = '<style>*{margin:0;padding:0;}</style><img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAsAAAALCAYAAACprHcmAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAA0SURBVChTY0AG7HFL/qNjqBQmGCqKsSnAhYnWAFYIA9gUwDBUCSogWiEMEK0QBrArZGAAAJdCe+n9ocQtAAAAAElFTkSuQmCC">'; // 这里有一句粗糙的CSS Reset以及一个base64编码的箭头图片
  25. document.documentElement.innerHTML = link.outerHTML; // 替换掉整个页面
  26. }
  27. }
  28.  
  29. }else{
  30.  
  31. // 种子列表页面
  32. (function($){
  33. $('#topic_list>thead>tr>th:nth-child(4)').attr('width', '30px').after('<th width="30px"><span>离线</span></th><th width="30px"><span>种子</span></th>'); // 在表头中添加两列
  34.  
  35. var rows = [];
  36. function Row(i){
  37. this.id = i;
  38. this.tr = $('#topic_list>tbody>tr').eq(i);
  39.  
  40. this.tr.children('td:nth-child(4)').after('<td><a class="download-arrow arrow-thunder">&nbsp;</a></td><td><a class="download-arrow arrow-torrent">&nbsp;</a></td>'); // 添加两列
  41.  
  42. this.link_title = this.tr.find('td.title>a'); // 种子页面链接
  43. this.link_magnet = this.tr.find('.arrow-magnet'); // 原始磁链
  44. this.link_thunder = this.tr.find('.arrow-thunder'); // 离线磁链
  45. this.link_torrent = this.tr.find('.arrow-torrent'); // 种子链接
  46.  
  47. this.link_thunder.attr('href', this.link_magnet.attr('href').substr(0, 52)); // 将原来的磁链截短,只保留btih
  48.  
  49. this.link_torrent.css('opacity', '.2'); // 将种子链接设置为半透明,表示需要加载
  50.  
  51. // 当鼠标移入种子链接所在td时
  52. this.link_torrent.closest('td').mouseenter(function(){
  53. if(this.link_torrent_done)
  54. return;
  55. var href = this.link_torrent.attr('href');
  56. if(href){
  57. // 如果链接已经存在,直接通过iframe显示
  58. var iframe = $('<iframe width="12" height="12" frameborder="0"></iframe>');
  59. iframe.attr('src', href);
  60. this.link_torrent.replaceWith(iframe);
  61. this.link_torrent_done = true;
  62. }else{
  63. // 否则先加载数据
  64. this.loadData(0);
  65. }
  66. }.bind(this));
  67. }
  68. Row.prototype.loadData = function(recursive){
  69. if(recursive)
  70. $('.export_current').text(this.id);
  71. if(!this.tr.hasClass('loading')){
  72. this.tr.addClass('loading');
  73.  
  74. // 读取种子页面,以便获取种子的下载链接和文件名
  75. $.get(this.link_title.attr('href'), function(html){
  76. // 为了避免页面元素加载,这里使用正则提取数据而不是DOM
  77. var torrent_link = html.match(/dl\.dmhy\.org\/([^"]+)/)[1]; // 下载链接
  78.  
  79. // 获得文件名,这里需要能够处理包含文件夹的种子
  80. var torrent_name = html.match(/([^>]+?)\s*<span class="bt_file_size">/)[1];
  81. torrent_name = torrent_name.split('/');
  82. var torrent_with_dir = false;
  83. if(torrent_name.length>1)
  84. torrent_with_dir = true;
  85. this.link_magnet.data('dir', torrent_with_dir);
  86. this.link_thunder.data('dir', torrent_with_dir);
  87. torrent_name = torrent_name[0];
  88. var dn = 'dn=' + encodeURIComponent(torrent_name);
  89. this.link_thunder.attr('href', this.link_thunder.attr('href')+'&'+dn);
  90. this.link_magnet.attr('href', this.link_magnet.attr('href').replace(/dn=[^&]*/, dn));
  91.  
  92. torrent_name = torrent_name+'.torrent';
  93.  
  94. // 接下来是一些谜一样的代码
  95.  
  96. // 将下载链接和文件名放入一个对象中
  97. var argv = {
  98. link : torrent_link,
  99. name : torrent_name,
  100. };
  101. // 构造一个指向dl.dmhy.org链接,通过hash传递参数
  102. this.link_torrent.attr('href', 'https://dl.dmhy.org/#download#'+JSON.stringify(argv));
  103.  
  104. if(!recursive){
  105. this.link_torrent.mouseenter();
  106. }else{
  107. if(this.id+1<rows.length){
  108. setTimeout(function(){
  109. rows[this.id+1].loadData(1);
  110. }.bind(this), 200);
  111. }else{
  112. on_export_loaded();
  113. }
  114. }
  115.  
  116. }.bind(this));
  117. }else{
  118. if(this.id+1<rows.length){
  119. rows[this.id+1].loadData(1);
  120. }else{
  121. on_export_loaded();
  122. }
  123. }
  124. }
  125.  
  126. // 处理每一行
  127. for(var i=0;i<$('#topic_list>tbody>tr').length;++i){
  128. rows.push(new Row(i));
  129. }
  130.  
  131. if(rows.length){
  132. var export_loading = false;
  133. jQuery('.nav_title>.fl').append(' | <a download="dmhy_magnet_fast.txt" id="export_fast">快速导出磁链(可能不包含文件名)</a> | <a download="dmhy_magnet_full.txt" id="export_full" class="export_load" title="点击开始加载数据">导出磁链(包含文件名)</a> | <a download="dmhy_magnet_file.txt" id="export_file" class="export_load" title="点击开始加载数据">导出磁链(只导出单个文件的磁链)</a>');
  134.  
  135. function export_blob(file_only){
  136. var text = '';
  137. for(var i=0;i<rows.length;++i){
  138. var link = rows[i].link_thunder;
  139. if(file_only&&link.data('dir'))
  140. continue;
  141. text += link.attr('href') + '\r\n';
  142. }
  143. var blob = new Blob([text], {type: 'text/plain'});
  144. return window.URL.createObjectURL(blob);
  145. }
  146.  
  147. $('#export_fast').mouseenter(function(){
  148. $(this).attr('href', export_blob(0));
  149. });
  150.  
  151. $('.export_load').click(function(){
  152. if(export_loading)
  153. return;
  154. export_loading = true;
  155. $('.export_load').prepend('<span class="export_loading">(加载中 ... <span class="export_current"></span> / <span class="export_total"></span> )</span>');
  156. $('.export_total').text(rows.length);
  157. rows[0].loadData(1);
  158. });
  159.  
  160. function on_export_loaded(){
  161. $('.export_loading').remove();
  162. $('#export_full').attr('href', export_blob(0));
  163. $('#export_file').attr('href', export_blob(1));
  164. }
  165. }
  166.  
  167. })(jQuery);
  168.  
  169. }