V2EXcellent.js

A Better V2EX

当前为 2015-11-25 提交的版本,查看 最新版本

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