V2EXcellent.js

A Better V2EX

当前为 2017-12-25 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name V2EXcellent.js
  3. // @namespace http://vitovan.github.io/v2excellent.js/
  4. // @version 1.1.5
  5. // @description A Better V2EX
  6. // @author VitoVan
  7. // @include http*://*v2ex.com/*
  8. // @require //code.jquery.com/jquery-1.12.4.min.js
  9. // @grant none
  10. // ==/UserScript==
  11.  
  12. $(window).load(function() {
  13. window.loaded = true;
  14. });
  15.  
  16. var POST_PROCESS_FUNCS = [
  17. function done() {
  18. console.log('V2EXcellented!');
  19. },
  20. ];
  21.  
  22. // 图片链接自动转换成图片 代码来自caoyue@v2ex
  23. POST_PROCESS_FUNCS.push(function linksToImgs() {
  24. var links = document.links;
  25. for (var x in links) {
  26. var link = links[x];
  27. if (
  28. /^http.*\.(?:jpg|jpeg|jpe|bmp|png|gif)/i.test(link.href) &&
  29. !/<img\s/i.test(link.innerHTML)
  30. ) {
  31. link.innerHTML =
  32. "<img title='" + link.href + "' src='" + link.href + "' />";
  33. }
  34. }
  35. });
  36.  
  37. POST_PROCESS_FUNCS.push(function markOp() {
  38. //标记楼主
  39. uid = document
  40. .getElementById('Rightbar')
  41. .getElementsByTagName('a')[0]
  42. .href.split('/member/')[1]; //自己用户名
  43. if (location.href.indexOf('.com/t/') != -1) {
  44. var lzname = document
  45. .getElementById('Main')
  46. .getElementsByClassName('avatar')[0]
  47. .parentNode.href.split('/member/')[1];
  48. allname = '@' + lzname + ' ';
  49. all_elem = document.getElementsByClassName('dark');
  50. for (var i = 0; i < all_elem.length; i++) {
  51. if (all_elem[i].innerHTML == lzname) {
  52. var opWord = language === 'zh_CN' ? '楼主' : 'OP';
  53. all_elem[i].innerHTML += ' <font color=green>[' + opWord + ']</font>';
  54. }
  55. //为回复所有人做准备
  56. if (
  57. uid != all_elem[i].innerHTML &&
  58. all_elem[i].href.indexOf('/member/') != -1 &&
  59. all_elem[i].innerText == all_elem[i].innerHTML &&
  60. allname.indexOf('@' + all_elem[i].innerHTML + ' ') == -1
  61. ) {
  62. allname += '@' + all_elem[i].innerHTML + ' ';
  63. }
  64. }
  65. }
  66. });
  67.  
  68. function postProcess() {
  69. $(POST_PROCESS_FUNCS).each(function(i, f) {
  70. if (typeof f === 'function') {
  71. f();
  72. console.log('V2EXcellent Post Processing: ' + f.name);
  73. }
  74. });
  75. }
  76.  
  77. var language = window.navigator.userLanguage || window.navigator.language;
  78.  
  79. var currentLocation = location.href;
  80. //If this is the thread page
  81. if (currentLocation.match(/\/t\/\d+/g)) {
  82. //Enable Reply Directly Feature
  83. $('div.topic_buttons').append(
  84. '<a " href="#;" onclick="$(\'#reply_content\').focus();" class="tb">回复</a>'
  85. );
  86. //Enable Img Uploader Feature
  87. enableUploadImg();
  88. var comments = [];
  89. //loading
  90. showSpinner();
  91. //Get comments from current page
  92. fillComments($('body'));
  93. //Get other pages comments
  94. var CURRENT_PAGE_URLS = [];
  95. $('a[href].page_normal').each(function(i, o) {
  96. if (CURRENT_PAGE_URLS.indexOf(o.href) === -1) {
  97. CURRENT_PAGE_URLS.push(o.href);
  98. }
  99. });
  100. var LEFT_PAGES_COUNT = CURRENT_PAGE_URLS.length;
  101. var CURRENT_PAGE = 0;
  102. var DOMS = [$(document)];
  103. if (LEFT_PAGES_COUNT > 0) {
  104. $(CURRENT_PAGE_URLS).each(function(i, o) {
  105. $.get(o, function(result) {
  106. var resultDom = $('<output>').append($.parseHTML(result));
  107. DOMS.push(resultDom);
  108. fillComments(resultDom);
  109. CURRENT_PAGE++;
  110. //if all comments are sucked.
  111. if (CURRENT_PAGE === LEFT_PAGES_COUNT) {
  112. //stack'em
  113. stackComments();
  114. //reArrange
  115. reArrangeComments();
  116. // post process functions
  117. postProcess();
  118. }
  119. });
  120. });
  121. } else {
  122. stackComments();
  123. //reArrange
  124. reArrangeComments();
  125. // post process functions
  126. postProcess();
  127. }
  128. // Clear Default Pager
  129. $('a[href^="?p="]')
  130. .parents('div.cell')
  131. .remove();
  132. } else if (currentLocation.match(/\/new/)) {
  133. $(
  134. '<a href="https://imgur.com/upload" target="_blank" style="padding:0 5px;">上传图片</a>'
  135. ).insertAfter($('button[onclick="previewTopic();"]'));
  136. }
  137.  
  138. function jumpToReply() {
  139. var floorSpecArr = currentLocation.match(/#reply\d+/g);
  140. var floorSpec = floorSpecArr && floorSpecArr.length ? floorSpecArr[0] : false;
  141. if (floorSpec) {
  142. floorSpec = floorSpec.match(/\d+/g)[0];
  143. var specFloor = $('span.no').filter(function() {
  144. return $(this).text() === floorSpec;
  145. });
  146. var scrollFunc = function() {
  147. window.scrollTo(0, specFloor.offset().top - $('body').offset().top);
  148. };
  149. if (window.loaded) {
  150. scrollFunc();
  151. } else {
  152. window.onload = function() {
  153. setTimeout(function() {
  154. scrollFunc();
  155. }, 1);
  156. };
  157. }
  158. }
  159. }
  160.  
  161. //Remove #reply42 from index
  162. $('span.item_title>a').attr('href', function(i, val) {
  163. return val.replace(/#reply\d+/g, '');
  164. });
  165.  
  166. function fillComments(jqDom) {
  167. jqDom.find('div[id^="r_"]').each(function(i, o) {
  168. var cmno = parseInt(
  169. $(o)
  170. .find('span.no')
  171. .text()
  172. );
  173. comments[cmno] = {
  174. id: $(o).attr('id'),
  175. no: cmno,
  176. user: $(o)
  177. .find('strong>a')
  178. .text(),
  179. content: $(o)
  180. .find('div.reply_content')
  181. .text(),
  182. mentioned: (function() {
  183. var mentionedNames = [];
  184. $(o)
  185. .find('div.reply_content>a[href^="/member/"]:not("dark")')
  186. .each(function(i, o) {
  187. mentionedNames.push(o.innerHTML);
  188. });
  189. return mentionedNames;
  190. })(),
  191. subComments: [],
  192. };
  193. });
  194. }
  195.  
  196. //Enable Floor Specification Feature
  197. $('a[href="#;"]:has(img[alt="Reply"])').click(function(e) {
  198. var floorNo = $(e.currentTarget)
  199. .parent()
  200. .find('span.no')
  201. .text();
  202. replyContent = $('#reply_content');
  203. oldContent = replyContent.val().replace(/^#\d+ /g, '');
  204. postfix = ' ' + '#' + floorNo + ' ';
  205. newContent = '';
  206. if (oldContent.length > 0) {
  207. if (oldContent != postfix) {
  208. newContent = oldContent + postfix;
  209. }
  210. } else {
  211. newContent = postfix;
  212. }
  213. replyContent.focus();
  214. replyContent.val(newContent);
  215. moveEnd($('#reply_content'));
  216. });
  217.  
  218. //Enable Gift ClickOnce Feature
  219. $('a[href="/mission/daily"]')
  220. .attr('id', 'gift_v2excellent')
  221. .attr('href', '#')
  222. .click(function() {
  223. $('#gift_v2excellent').text('正在领取......');
  224. $.get('/mission/daily', function(result) {
  225. var giftLink = $('<output>')
  226. .append($.parseHTML(result))
  227. .find('input[value^="领取"]')
  228. .attr('onclick')
  229. .match(/\/mission\/daily\/redeem\?once=\d+/g)[0];
  230. $.get(giftLink, function(checkResult) {
  231. var okSign = $('<output>')
  232. .append($.parseHTML(checkResult))
  233. .find('li.fa.fa-ok-sign');
  234. if (okSign.length > 0) {
  235. $.get('/balance', function(result) {
  236. var amount = $('<output>')
  237. .append($.parseHTML(result))
  238. .find('table>tbody>tr:contains("每日登录(不可用)"):first>td:nth(2)')
  239. .text();
  240. $('#gift_v2excellent').html(
  241. '已领取 <strong>' + amount + '</strong> 铜币。'
  242. );
  243. setTimeout(function() {
  244. $('#Rightbar>.sep20:nth(1)').remove();
  245. $('#Rightbar>.box:nth(1)').remove();
  246. }, 2000);
  247. });
  248. }
  249. });
  250. });
  251. return false;
  252. });
  253.  
  254. //Get comment's parent
  255. function findParentComment(comment) {
  256. var parent;
  257. if (comment) {
  258. var floorRegex = comment.content.match(/#\d+ /g);
  259. if (floorRegex && floorRegex.length > 0) {
  260. var floorNo = parseInt(floorRegex[0].match(/\d+/g)[0]);
  261. parent = comments[floorNo];
  262. } else {
  263. for (var i = comment.no - 1; i > 0; i--) {
  264. var cc = comments[i];
  265. if (cc) {
  266. if (
  267. $.inArray(cc.user, comment.mentioned) !== -1 &&
  268. parent === undefined
  269. ) {
  270. parent = cc;
  271. }
  272. //If they have conversation, then make them together.
  273. if (
  274. comment.mentioned.length > 0 &&
  275. cc.user === comment.mentioned[0] &&
  276. cc.mentioned[0] === comment.user
  277. ) {
  278. parent = cc;
  279. break;
  280. }
  281. }
  282. }
  283. }
  284. }
  285. return parent;
  286. }
  287.  
  288. //Stack comments, make it a tree
  289. function stackComments() {
  290. for (var i = comments.length - 1; i > 0; i--) {
  291. var parent = findParentComment(comments[i]);
  292. if (parent) {
  293. parent.subComments.unshift(comments[i]);
  294. comments.splice(i, 1);
  295. }
  296. }
  297. }
  298.  
  299. function getCommentDom(id) {
  300. var commentDom;
  301. $.each(DOMS, function(i, o) {
  302. var result = o.find('div[id="' + id + '"]');
  303. if (result.length > 0) {
  304. commentDom = result;
  305. }
  306. });
  307. return commentDom;
  308. }
  309.  
  310. function moveComment(comment, parent) {
  311. if (comment) {
  312. var commentDom = getCommentDom(comment.id);
  313. $.each(comment.subComments, function(i, o) {
  314. moveComment(o, commentDom);
  315. });
  316. commentDom.appendTo(parent);
  317. }
  318. }
  319.  
  320. function getCommentBox() {
  321. var commentBox = $('#Main>div.box:nth(1)');
  322. if (commentBox.length === 0) {
  323. // Maybe using mobile
  324. commentBox = $('#Wrapper>div.content>div.box:nth(1)');
  325. if ($('#v2excellent-mobile-tip').length === 0) {
  326. $(
  327. '<div class="cell" id="v2excellent-mobile-tip" style="background: #CC0000;font-weight: bold;text-align: center;"><span><a style="color:white;text-decoration:underline;" target="_blank" href="https://github.com/VitoVan/v2excellent.js/issues/7#issuecomment-304674654">About V2EXcellent.js on Mobile</a></span></div>'
  328. ).insertBefore('#Wrapper>div.content>div.box:nth(1)>.cell:first');
  329. }
  330. }
  331. return commentBox;
  332. }
  333.  
  334. function showSpinner() {
  335. var commentBox = getCommentBox();
  336. $('body').append(
  337. '<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>'
  338. );
  339. $(
  340. '<div class="spinner"><div class="double-bounce1"></div><div class="double-bounce2"></div></div>'
  341. ).insertBefore(commentBox);
  342. commentBox.hide();
  343. }
  344.  
  345. function reArrangeComments() {
  346. $('div.inner:has(a[href^="/t/"].page_normal)').remove();
  347. var commentBox = getCommentBox();
  348. $.each(comments, function(i, o) {
  349. moveComment(o, commentBox);
  350. });
  351. $('div[id^="r_"]>table>tbody>tr>td:first-child').attr('width', '20');
  352. $('body').append(
  353. '<style>.cell{background-color: inherit;}.cell .cell{padding-bottom:0;border-bottom:none;min-width: 250px;padding-right:0;}div[id^="r_"] img.avatar{width:20px;height:20px;border-radius:50%;}div[id^="r_"]>div{margin-left: 5px;}</style>'
  354. );
  355. commentBox.show();
  356. //removeSpinner
  357. $('.spinner').remove();
  358. jumpToReply();
  359. }
  360.  
  361. function enableUploadImg() {
  362. $('div.cell:contains("添加一条新回复")').append(
  363. '<div class="fr"><a href="https://imgur.com/upload" target="_blank"> 上传图片</a> - </div>'
  364. );
  365. }

QingJ © 2025

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