V2EXcellent.js

A Better V2EX

目前为 2017-04-15 提交的版本。查看 最新版本

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

QingJ © 2025

镜像随时可能失效,请加Q群300939539或关注我们的公众号极客氢云获取最新地址