WeiPhoneDownloadHelper

批量下载威锋论坛的电子书

目前为 2014-11-27 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name WeiPhoneDownloadHelper
  3. // @namespace https://github.com/ywzhaiqi
  4. // @author ywzhaiqi
  5. // @version 1.3.2
  6. // @description 批量下载威锋论坛的电子书
  7. // @homepageURL https://greasyfork.org/scripts/668/
  8.  
  9. // @include http://bbs.feng.com/read-htm-tid-*.html
  10. // @include http://bbs.feng.com/thread-htm-fid-*.html
  11. // @include http://bbs.feng.com/forum.php*
  12. // @run-at document-end
  13. // @grant none
  14. // ==/UserScript==
  15.  
  16. var $ = jQuery;
  17.  
  18. var RES = getMStr(function(){
  19. var html;
  20. /*
  21. <div>
  22. <button id="downloadButton">批量下载</button>
  23. <div id="batchPublish" style="display: none;">
  24. <div id="batchHeader">
  25. <a id="closeButton" class="aui_close" href="javascript:;">×</a>
  26. </div>
  27. <div id="batchContent">
  28. <pre id="batchedlink"></pre>
  29. </div>
  30. </div>
  31. <div id="batchNotice" style="display:none">
  32. </div>
  33. </div>
  34. */
  35. var cssText;
  36. /*
  37. #downloadButton {
  38. position:fixed;
  39. top:80px;
  40. right:8px;
  41. z-index: 100;
  42. }
  43. #batchPublish {
  44. position:fixed;
  45. z-index:1001;
  46. top:40%;
  47. left:35%;
  48. width: 530px;
  49. background:white;
  50. border: 3px solid #AAA;
  51. }
  52. #batchedlink {
  53. height: 250px;
  54. overflow: scroll;
  55. }
  56. #batchNotice {
  57. position:fixed;
  58. z-index:1001;
  59. top:10%;
  60. left:35%;
  61. background: #F9EDBE;
  62. box-shadow: 1px 2px 5px rgba(0, 0, 0, 0.5);
  63. border: 1px solid #FBDA91;
  64. padding: 5px;
  65. }
  66. #batchNotice > b {
  67. margin-left: 5px;
  68. }
  69. */
  70. });
  71.  
  72.  
  73. var locationHref = location.href;
  74.  
  75. locationHref.match(/thread-htm-fid|mod=forumdisplay/) && (function(){
  76.  
  77. $('#bbs_top_news, #forum_rules_224').hide();
  78.  
  79. var hideText = [
  80. "网易《迷你西游》手游公测",
  81. "3D纯正中国风《水浒英雄》",
  82. "【软件】感恩父母,免费下载:【快速问医生】",
  83. "【软件】随时随地 移动视频!酷6视频软件介绍",
  84. "威锋手游控:玩《龙纹三国》",
  85. "《放开那三国》威锋版火爆上线",
  86. "《天天爱萌仙》",
  87. "炉石传说",
  88. "同步推",
  89. "苹果手游",
  90. "苹果助手",
  91. "兔兔助手",
  92. "同步推",
  93. "赢iPad mini",
  94. "得iPhone",
  95. "iPad mini2大奖",
  96. "删档封测火爆开启"
  97. ];
  98.  
  99. // 隐藏置顶广告的行
  100. $('tbody[id^="stickthread_"]').each(function(row){
  101. if ($(this).find('span[id^="rushtimer_"]').length) {
  102. $(this).hide();
  103. return;
  104. }
  105.  
  106. var text = $(this).text();
  107. for (var i = 0, l = hideText.length; i < l; i++) {
  108. if (text.indexOf(hideText[i]) != -1) {
  109. $(this).hide();
  110. return;
  111. }
  112. }
  113. });
  114. })()
  115.  
  116. locationHref.match(/read-htm-tid|mod=viewthread/) && (function(){
  117.  
  118. var preUrl = location.origin,
  119. attachSelector = '.attnm > a, span[id^="attach_"] > a',
  120. $attachs = $(attachSelector);
  121.  
  122. if ($attachs.size() == 0) {
  123. return;
  124. }
  125.  
  126. $('<style>').html(RES.cssText).appendTo('head');
  127.  
  128. $(RES.html).appendTo('body');
  129.  
  130. $('#closeButton').click(function(){
  131. $('#batchPublish').hide();
  132. })
  133.  
  134. $('#downloadButton').attr('title', '共 ' + $attachs.size() + ' 个附件').click(function(){
  135. var links = $.makeArray($(attachSelector))
  136. $batchNotice = $('#batchNotice');
  137. var downUrls = [];
  138.  
  139. $batchNotice.html('正在获取中...' + '<b>1/' + links.length + '</b>')
  140. .show();
  141.  
  142. function handleResult() {
  143. if (links.length > 0) {
  144. $batchNotice.html('正在获取中...' + '<b>' + downUrls.length + '/' + links.length + '</b>');
  145. getDownloadLink();
  146. } else {
  147. var urls = downUrls.join('\n');
  148. // console.log(urls);
  149.  
  150. $batchNotice.hide();
  151. $('#batchedlink').html(urls);
  152. $('#batchPublish').show();
  153.  
  154. // 高亮选中文本
  155. var selection = window.getSelection();
  156. var range = document.createRange();
  157. range.selectNodeContents($('#batchedlink')[0]);
  158. selection.removeAllRanges();
  159. selection.addRange(range);
  160. }
  161. }
  162.  
  163. function getDownloadLink() {
  164. var link = links.shift();
  165. if (!link) return;
  166.  
  167. var url = link.href;
  168. if (url.indexOf('aid=') > 0) {
  169. downUrls.push(url);
  170. handleResult();
  171. } else {
  172. var m = link.getAttribute('onclick').match(/jQuery.get\('(.*?)',/);
  173. if (m) {
  174. var url = m[1];
  175. $.get(url, {}, function(data){
  176. var downUrl = $('<div>').html(data).find('a:first').attr('href');
  177. downUrl = preUrl + downUrl;
  178. downUrls.push(downUrl)
  179.  
  180. handleResult();
  181. });
  182. }
  183. }
  184. }
  185.  
  186. getDownloadLink()
  187. });
  188.  
  189. })()
  190.  
  191.  
  192.  
  193. // 从函数中获取多行注释的字符串
  194. function getMStr(fn) {
  195. var fnSource = fn.toString();
  196. var ret = {};
  197. fnSource = fnSource.replace(/^[^{]+/, '');
  198. // console.log(fnSource);
  199. var matched;
  200. var reg = /var\s+([$\w]+)[\s\S]*?\/\*([\s\S]+?)\*\//g;
  201. while (matched = reg.exec(fnSource)) {
  202. // console.log(matched);
  203. ret[matched[1]] = matched[2];
  204. };
  205.  
  206. return ret;
  207. }