V2EXcellent.js

A Better V2EX

当前为 2016-01-21 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name V2EXcellent.js
  3. // @namespace http://vitovan.github.io/v2excellent.js/
  4. // @version 0.1.0.9
  5. // @description A Better V2EX
  6. // @author VitoVan
  7. // @include http*://*v2ex.com/*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. var currentLocation = location.href;
  12. //If this is the thread page
  13. if(currentLocation.match(/\/t\/\d+/g)){
  14. //Enable Reply Directly Feature
  15. $('div.topic_buttons').append('<a " href="#;" onclick="$(\'#reply_content\').focus();" class="tb">回复</a>');
  16. //Enable Img Uploader Feature
  17. enableUploadImg();
  18. var comments = [];
  19. //loading
  20. showSpinner();
  21. //Get comments from current page
  22. fillComments($('body'));
  23. //Get other pages comments
  24. var PAGES_COUNT = $('div.inner>a[href^="/t/"].page_normal').length;
  25. var CURRENT_PAGE = 0;
  26. var DOMS = [$(document)];
  27. if(PAGES_COUNT>0){
  28. $('div.inner>a[href^="/t/"].page_normal').each(function(i,o){
  29. $.get(o.href,function(result){
  30. var resultDom = $('<output>').append($.parseHTML(result));
  31. DOMS.push(resultDom);
  32. fillComments(resultDom);
  33. CURRENT_PAGE ++;
  34. //if all comments are sucked.
  35. if(CURRENT_PAGE === PAGES_COUNT){
  36. //stack'em
  37. stackComments();
  38. //reArrange
  39. reArrangeComments();
  40. }
  41. });
  42. });
  43. }else{
  44. stackComments();
  45. //reArrange
  46. reArrangeComments();
  47. }
  48. }
  49.  
  50. function jumpToReply(){
  51. var floorSpecArr = currentLocation.match(/#reply\d+/g);
  52. var floorSpec = floorSpecArr && floorSpecArr.length ? floorSpecArr[0] : false;
  53. if(floorSpec){
  54. floorSpec = floorSpec.match(/\d+/g)[0];
  55. var specFloor = $('span.no').filter(function() {return $(this).text() === floorSpec;});
  56. $('body').scrollTop(specFloor.offset().top - $('body').offset().top);
  57. }
  58. }
  59.  
  60. //Remove #reply42 from index
  61. $('span.item_title>a').attr("href",function(i,val){return val.replace(/#reply\d+/g,'');});
  62.  
  63. function fillComments(jqDom){
  64. jqDom.find('div[id^="r_"]').each(function(i,o){
  65. var cmno = parseInt($(o).find('span.no').text());
  66. comments[cmno] =
  67. {
  68. id: $(o).attr('id'),
  69. no: cmno,
  70. user: $(o).find('strong>a').text(),
  71. content: $(o).find('div.reply_content').text(),
  72. mentioned: (function(){
  73. var mentionedNames = [];
  74. $(o).find('div.reply_content>a[href^="/member/"]:not("dark")').each(function(i,o){
  75. mentionedNames.push(o.innerHTML);
  76. });
  77. return mentionedNames;
  78. }()),
  79. subComments: []
  80. };
  81. });
  82. }
  83.  
  84. //Enable Floor Specification Feature
  85. $('a[href="#;"]:has(img[alt="Reply"])').click(function(e){
  86. var floorNo = $(e.currentTarget).parent().find('span.no').text();
  87. replyContent = $("#reply_content");
  88. oldContent = replyContent.val().replace(/^r#\d+ /g,'');
  89. prefix = "r#" + floorNo + " ";
  90. newContent = ''
  91. if(oldContent.length > 0){
  92. if (oldContent != prefix) {
  93. newContent = prefix + oldContent;
  94. }
  95. } else {
  96. newContent = prefix
  97. }
  98. replyContent.focus();
  99. replyContent.val(newContent);
  100. moveEnd($("#reply_content"));
  101. });
  102.  
  103. //Enable Gift ClickOnce Feature
  104. $('a[href="/mission/daily"]').attr('id','gift_v2excellent').attr('href','#').click(function(){
  105. $('#gift_v2excellent').text('正在领取......');
  106. $.get('/mission/daily',function(result){
  107. var giftLink = $('<output>').append($.parseHTML(result)).
  108. find('input[value^="领取"]').
  109. attr('onclick').match(/\/mission\/daily\/redeem\?once=\d+/g)[0];
  110. $.get(giftLink,function(checkResult){
  111. var okSign = $('<output>').append($.parseHTML(checkResult)).find('li.fa.fa-ok-sign');
  112. if(okSign.length>0){
  113. $.get('/balance',function(result){
  114. var amount = $('<output>').append($.parseHTML(result)).find('table>tbody>tr:contains("每日登录"):first>td:nth(2)').text();
  115. $('#gift_v2excellent').html('已领取 <strong>' + amount + '</strong> 铜币。' );
  116. setTimeout(function(){
  117. $('#Rightbar>.sep20:nth(1)').remove();
  118. $('#Rightbar>.box:nth(1)').remove();
  119. },2000);
  120. });
  121. }
  122. });
  123. });
  124. return false;
  125. });
  126.  
  127. //Get comment's parent
  128. function findParentComment(comment){
  129. var parent = undefined;
  130. if(comment){
  131. var floorRegex = comment.content.match(/^r#\d+ /g);
  132. if(floorRegex && floorRegex.length>0){
  133. var floorNo = parseInt(floorRegex[0].match(/\d+/g)[0]);
  134. parent = comments[floorNo];
  135. }else{
  136. for(var i=comment.no-1;i>0;i--){
  137. var cc = comments[i];
  138. if(cc){
  139. if($.inArray(cc.user, comment.mentioned) !== -1 && parent === undefined){
  140. parent = cc;
  141. }
  142. //If they have conversation, then make them together.
  143. if(comment.mentioned.length>0 && cc.user === comment.mentioned[0] && cc.mentioned[0] === comment.user){
  144. parent = cc;
  145. break;
  146. }
  147. }
  148. }
  149. }
  150. }
  151. return parent;
  152. }
  153.  
  154. //Stack comments, make it a tree
  155. function stackComments(){
  156. for(var i=comments.length-1;i>0;i--){
  157. var parent = findParentComment(comments[i]);
  158. if(parent){
  159. parent.subComments.unshift(comments[i]);
  160. comments.splice(i,1);
  161. }
  162. }
  163. }
  164.  
  165. function getCommentDom(id){
  166. var commentDom = undefined;
  167. $.each(DOMS,function(i,o){
  168. var result = o.find('div[id="' + id + '"]');
  169. if(result.length>0){
  170. commentDom = result;
  171. }
  172. });
  173. return commentDom;
  174. }
  175.  
  176. function moveComment(comment,parent){
  177. if(comment){
  178. var commentDom = getCommentDom(comment.id);
  179. $.each(comment.subComments,function(i,o){
  180. moveComment(o,commentDom);
  181. });
  182. commentDom.appendTo(parent);
  183. }
  184. }
  185.  
  186. function showSpinner(){
  187. var commentBox = $('#Main>div.box:nth(1)');
  188. $('body').append('<style>.spinner{width:40px;height:40px;position:relative;margin:100px auto}.double-bounce1,.double-bounce2{width:100%;height:100%;border-radius:50%;background-color:#333;opacity:.6;position:absolute;top:0;left:0;-webkit-animation:sk-bounce 2.0s infinite ease-in-out;animation:sk-bounce 2.0s infinite ease-in-out}.double-bounce2{-webkit-animation-delay:-1.0s;animation-delay:-1.0s}@-webkit-keyframes sk-bounce{0%,100%{-webkit-transform:scale(0.0)}50%{-webkit-transform:scale(1.0)}}@keyframes sk-bounce{0%,100%{transform:scale(0.0);-webkit-transform:scale(0.0)}50%{transform:scale(1.0);-webkit-transform:scale(1.0)}}</style>');
  189. $('<div class="spinner"><div class="double-bounce1"></div><div class="double-bounce2"></div></div>').insertBefore(commentBox);
  190. commentBox.hide();
  191. }
  192.  
  193. function reArrangeComments(){
  194. $('div.inner:has(a[href^="/t/"].page_normal)').remove();
  195. var commentBox = $('#Main>div.box:nth(1)');
  196. $.each(comments,function(i,o){
  197. moveComment(o,commentBox);
  198. });
  199. $('div[id^="r_"]>table>tbody>tr>td:first-child').attr('width','20');
  200. $('body').append('<style>.cell .cell{border-bottom:none;}div[id^="r_"] img.avatar{width:20px;height:20px;border-radius:50%;}div[id^="r_"]>div{margin-left: 5px;}</style>');
  201. commentBox.show();
  202. //removeSpinner
  203. $('.spinner').remove();
  204. jumpToReply();
  205. }
  206.  
  207. function enableUploadImg(){
  208. $('div.cell:contains("添加一条新回复")').append('<div class="fr"><a href="http://upload.otar.im/" target="_blank"> 上传图片</a> - </div>');
  209. }