display ed2k links on verycd

display ed2k links on verycd.com

当前为 2014-09-13 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name display ed2k links on verycd
  3. // @description display ed2k links on verycd.com
  4. // @namespace JaHIY
  5. // @author JaHIY
  6. // @version 0.2.1
  7. // @match http://www.verycd.com/topics/*
  8. // ==/UserScript==
  9.  
  10. (function(){
  11.  
  12. 'use strict';
  13.  
  14. /*
  15. * DOMParser HTML extension
  16. * 2012-09-04
  17. *
  18. * By Eli Grey, http://eligrey.com
  19. * Public domain.
  20. * NO WARRANTY EXPRESSED OR IMPLIED. USE AT YOUR OWN RISK.
  21. */
  22. /*! @source https://gist.github.com/1129031 */
  23. /*global document, DOMParser*/
  24. (function(DOMParser) {
  25. "use strict";
  26.  
  27. var DOMParser_proto = DOMParser.prototype,
  28. real_parseFromString = DOMParser_proto.parseFromString;
  29.  
  30. // Firefox/Opera/IE throw errors on unsupported types
  31. try {
  32. // WebKit returns null on unsupported types
  33. if ((new DOMParser).parseFromString("", "text/html")) {
  34. // text/html parsing is natively supported
  35. return;
  36. }
  37. } catch (ex) {}
  38.  
  39. DOMParser_proto.parseFromString = function(markup, type) {
  40. if (/^\s*text\/html\s*(?:;|$)/i.test(type)) {
  41. var doc = document.implementation.createHTMLDocument("");
  42. if (markup.toLowerCase().indexOf('<!doctype') > -1) {
  43. doc.documentElement.innerHTML = markup;
  44. }
  45. else {
  46. doc.body.innerHTML = markup;
  47. }
  48. return doc;
  49. } else {
  50. return real_parseFromString.apply(this, arguments);
  51. }
  52. };
  53. }(DOMParser));
  54.  
  55. document.getElementById('iptcomED2K').children.length === 1 &&
  56. GM_xmlhttpRequest({
  57. method: 'GET',
  58. url: 'http://www.icili.com/emule/download/' + window.location.pathname.match(/^\/topics\/(\d+)\/?$/)[1],
  59. headers: {
  60. 'User-Agent': 'Mozilla/5.0',
  61. 'Cache-Control': 'no-cache',
  62. 'Origin': 'http://www.icili.com',
  63. 'Referer': 'http://www.icili.com'
  64. },
  65. onload: function (res) {
  66. var parser = new DOMParser(),
  67. children = parser.parseFromString(res.responseText, 'text/html').getElementById('emuleFile').firstElementChild.children,
  68. files = [],
  69. wrapper = document.getElementById('iptcomED2K'),
  70. docfrag = document.createDocumentFragment();
  71. for (let i=1, l=children.length-1, child; i<l; i++) {
  72. child = children[i].children[1].firstElementChild;
  73. files.push({
  74. 'title': child.title.trim(),
  75. 'href': child.href,
  76. 'size': children[i].children[2].textContent.trim()
  77. });
  78. }
  79. {
  80. let emuletop = document.createElement('div'),
  81. emulemain = emuletop.cloneNode(false),
  82. tbl = document.createElement('table'),
  83. random_code = 'EM51da5b7de03b7';
  84.  
  85. emuletop.className = 'emuletop';
  86. emuletop.textContent = '电驴资源';
  87. docfrag.appendChild(emuletop);
  88.  
  89. emulemain.className = 'emulemain';
  90. docfrag.appendChild(emulemain);
  91.  
  92. tbl.cellPadding = '2';
  93. tbl.cellSpacing = '1';
  94. tbl.width = '100%';
  95. emulemain.appendChild(tbl);
  96. {
  97. let row = tbl.insertRow(-1),
  98. cell = row.insertCell(-1),
  99. archor = document.createElement('a');
  100. cell.className = 'needemule';
  101. cell.colSpan = '2';
  102. archor.href = 'http://www.easymule.com/';
  103. archor.textContent = '下面是用户共享的文件列表,安装电驴后,您可以点击这些文件名进行下载';
  104. cell.appendChild(archor);
  105. }
  106. for (let i=0, l=files.length, row, cell_filename, cell_size, input, archor; i<l; i++) {
  107. row = tbl.insertRow(-1);
  108. cell_filename = row.insertCell(-1);
  109. cell_size = row.insertCell(-1);
  110. input = document.createElement('input');
  111. archor = document.createElement('a');
  112. cell_filename.className = cell_size.className = (i%2 === 0) ? 'post2' : 'post';
  113. input.className = 'forminput';
  114. input.type = 'checkbox';
  115. input.name = random_code;
  116. input.value = files[i]['href'];
  117. input.defaultChecked = true;
  118. input.setAttribute('onclick', 'em_size("' + random_code + '");');
  119. archor.href = archor.ed2k = files[i]['href'];
  120. archor.textContent = files[i]['title'];
  121. cell_filename.appendChild(input);
  122. cell_filename.appendChild(archor);
  123. cell_size.align = 'center';
  124. cell_size.textContent = files[i]['size'];
  125. }
  126. {
  127. let row = tbl.insertRow(-1),
  128. cell = row.insertCell(-1),
  129. input_checkbox = document.createElement('input'),
  130. input_button = input_checkbox.cloneNode(false),
  131. label = document.createElement('label'),
  132. span = document.createElement('span'),
  133. script = document.createElement('script');
  134. cell.align = 'left';
  135. cell.className = (files.length%2 === 0) ? 'post2' : 'post';
  136. input_checkbox.className = 'forminput';
  137. input_checkbox.type = 'checkbox';
  138. input_checkbox.id = 'checkall_' + random_code;
  139. input_checkbox.defaultChecked = true;
  140. input_checkbox.setAttribute('onclick', 'checkAll("' + random_code + '",this.checked)');
  141. label.htmlFor = 'checkall_' + random_code;
  142. input_button.type = 'button';
  143. input_button.className = 'button downall';
  144. input_button.value = '下载选中的文件';
  145. input_button.setAttribute('onclick', 'download("' + random_code + '",0,1)');
  146. span.id = 'updateflash' + random_code;
  147. script.type = 'text/javascript';
  148. script.textContent = 'changeUpdateFlash("' + random_code + '");new MultiCheckBox("' + random_code + '");';
  149. cell.appendChild(input_checkbox);
  150. cell.appendChild(label);
  151. cell.appendChild(input_button);
  152. cell.appendChild(span);
  153. cell.appendChild(script);
  154. }
  155. }
  156. wrapper.replaceChild(docfrag, wrapper.firstElementChild);
  157. }
  158. });
  159. })();