V2EXcellent.js

A Better V2EX

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

  1. // ==UserScript==
  2. // @name V2EXcellent.js
  3. // @namespace http://vitovan.github.io/v2excellent.js/
  4. // @version 0.1.0.4
  5. // @description A Better V2EX
  6. // @author VitoVan
  7. // @include http*://*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. //Remove #reply42 from index
  59. $('span.item_title>a').attr("href",function(i,val){return val.replace(/#reply\d+/g,'');});
  60.  
  61. function fillComments(jqDom){
  62. jqDom.find('div[id^="r_"]').each(function(i,o){
  63. var cmno = parseInt($(o).find('span.no').text());
  64. comments[cmno] =
  65. {
  66. id: $(o).attr('id'),
  67. no: cmno,
  68. user: $(o).find('strong>a').text(),
  69. content: $(o).find('div.reply_content').text(),
  70. mentioned: (function(){
  71. var mentionedNames = [];
  72. $(o).find('div.reply_content>a[href^="/member/"]:not("dark")').each(function(i,o){
  73. mentionedNames.push(o.innerHTML);
  74. });
  75. return mentionedNames;
  76. }()),
  77. subComments: []
  78. };
  79. });
  80. }
  81.  
  82. //Enable Floor Specification Feature
  83. $('a[href="#;"]:has(img[alt="Reply"])').click(function(e){
  84. var floorNo = $(e.currentTarget).parent().find('span.no').text();
  85. replyContent = $("#reply_content");
  86. oldContent = replyContent.val().replace(/^r#\d+ /g,'');
  87. prefix = "r#" + floorNo + " ";
  88. newContent = ''
  89. if(oldContent.length > 0){
  90. if (oldContent != prefix) {
  91. newContent = prefix + oldContent;
  92. }
  93. } else {
  94. newContent = prefix
  95. }
  96. replyContent.focus();
  97. replyContent.val(newContent);
  98. moveEnd($("#reply_content"));
  99. });
  100.  
  101. //Enable Gift ClickOnce Feature
  102. $('a[href="/mission/daily"]').attr('id','gift_v2excellent').attr('href','#').click(function(){
  103. $('#gift_v2excellent').text('正在领取......');
  104. $.get('/mission/daily',function(result){
  105. var giftLink = $('<output>').append($.parseHTML(result)).
  106. find('input[value^="领取"]').
  107. attr('onclick').match(/\/mission\/daily\/redeem\?once=\d+/g)[0];
  108. $.get(giftLink,function(checkResult){
  109. var okSign = $('<output>').append($.parseHTML(checkResult)).find('li.fa.fa-ok-sign');
  110. if(okSign.length>0){
  111. $('#gift_v2excellent').text('已领取');
  112. setTimeout(function(){
  113. $('#Rightbar>.sep20:nth(1)').remove();
  114. $('#Rightbar>.box:nth(1)').remove();
  115. },2000);
  116. }
  117. });
  118. });
  119. return false;
  120. });
  121.  
  122. //Get comment's parent
  123. function findParentComment(comment){
  124. var parent = undefined;
  125. if(comment){
  126. var floorRegex = comment.content.match(/^r#\d+ /g);
  127. if(floorRegex && floorRegex.length>0){
  128. var floorNo = parseInt(floorRegex[0].match(/\d+/g)[0]);
  129. parent = comments[floorNo];
  130. }else{
  131. for(var i=comment.no-1;i>0;i--){
  132. var cc = comments[i];
  133. if(cc){
  134. if($.inArray(cc.user, comment.mentioned) !== -1 && parent === undefined){
  135. parent = cc;
  136. }
  137. //If they have conversation, then make them together.
  138. if(comment.mentioned.length>0 && cc.user === comment.mentioned[0] && cc.mentioned[0] === comment.user){
  139. parent = cc;
  140. break;
  141. }
  142. }
  143. }
  144. }
  145. }
  146. return parent;
  147. }
  148.  
  149. //Stack comments, make it a tree
  150. function stackComments(){
  151. for(var i=comments.length-1;i>0;i--){
  152. var parent = findParentComment(comments[i]);
  153. if(parent){
  154. parent.subComments.unshift(comments[i]);
  155. comments.splice(i,1);
  156. }
  157. }
  158. }
  159.  
  160. function getCommentDom(id){
  161. var commentDom = undefined;
  162. $.each(DOMS,function(i,o){
  163. var result = o.find('div[id="' + id + '"]');
  164. if(result.length>0){
  165. commentDom = result;
  166. }
  167. });
  168. return commentDom;
  169. }
  170.  
  171. function moveComment(comment,parent){
  172. if(comment){
  173. var commentDom = getCommentDom(comment.id);
  174. $.each(comment.subComments,function(i,o){
  175. moveComment(o,commentDom);
  176. });
  177. commentDom.appendTo(parent);
  178. }
  179. }
  180.  
  181. function showSpinner(){
  182. var commentBox = $('#Main>div.box:nth(1)');
  183. $('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>');
  184. $('<div class="spinner"><div class="double-bounce1"></div><div class="double-bounce2"></div></div>').insertBefore(commentBox);
  185. commentBox.hide();
  186. }
  187.  
  188. function reArrangeComments(){
  189. $('div.inner:has(a[href^="/t/"].page_normal)').remove();
  190. var commentBox = $('#Main>div.box:nth(1)');
  191. $.each(comments,function(i,o){
  192. moveComment(o,commentBox);
  193. });
  194. $('div[id^="r_"]>table>tbody>tr>td:first-child').attr('width','20');
  195. $('body').append('<style>.cell{border-bottom:none;}div[id^="r_"] img.avatar{width:20px;height:20px;border-radius:50%;}div[id^="r_"]>div{margin-left: 21px;}div.box>div[id^="r_"]{border-bottom: 1px solid #E2E2E2;}</style>');
  196. commentBox.show();
  197. //removeSpinner
  198. $('.spinner').remove();
  199. }
  200.  
  201. function enableUploadImg(){
  202. if(I_AM_A_CHROME_EXT){
  203. }else{
  204. $('div.cell:contains("添加一条新回复")').append('<div class="fr"><a href="http://upload.otar.im/" target="_blank"> 上传图片</a> - </div>');
  205. }
  206. }