v2ex-helper

v2ex小助手

  1. // ==UserScript==
  2. // @name v2ex-helper
  3. // @namespace http://tampermonkey.net/
  4. // @version 0.1
  5. // @description v2ex小助手
  6. // @author ycz0926
  7. // @match https://www.v2ex.com/*
  8. // @grant none
  9. // ==/UserScript==
  10.  
  11. (function () {
  12. 'use strict';
  13.  
  14. // Your code here...
  15. /////////////////////////////////////////
  16. // 1、回到顶端按钮
  17. /////////////////////////////////////////
  18. document.body.innerHTML += '<a href="#" id="back-to-top">回到顶端</a>';
  19. var top_button = document.getElementById('back-to-top');
  20.  
  21. //为按钮添加css样式
  22. top_button.style.position = 'fixed';
  23. top_button.style.bottom = '3em';
  24. top_button.style.right = '3em';
  25. top_button.style.textDecoration = 'none';
  26. top_button.style.color = '#EEEEEE';
  27. top_button.style.backgroundColor = 'rgba(0, 0, 0, 0.3)';
  28. top_button.style.fontSize = '12px';
  29. top_button.style.padding = '1em';
  30. top_button.style.display = 'none';
  31. top_button.style.borderRadius = '3px';
  32. top_button.style.border = '1px solid #CCCCCC';
  33.  
  34. // 滚动窗口来判断按钮显示或隐藏
  35. window.addEventListener('scroll', function () {
  36. if (window.scrollY > 1000) {
  37. document.getElementById('back-to-top').style.display = 'block';
  38. } else {
  39. document.getElementById('back-to-top').style.display = 'none';
  40. }
  41. });
  42.  
  43. // 点击回到顶部
  44. document.getElementById('back-to-top').addEventListener('click', function (event) {
  45. event.preventDefault();
  46.  
  47. window.scrollTo(0, 0);
  48. setTimeout(function () {
  49. document.getElementById('back-to-top').style.display = 'none';
  50. }, 100);
  51. });
  52.  
  53. /////////////////////////////////////////
  54. // 2、新标签页打开帖子
  55. /////////////////////////////////////////
  56. document.body.addEventListener('mousedown', function (e) {
  57. var a = e.target;
  58. var parent = a.parentElement;
  59. if (parent.className !== 'item_title') return;
  60. a.target = '_blank';
  61. });
  62.  
  63. /////////////////////////////////////////
  64. // 3、高亮作者回复
  65. /////////////////////////////////////////
  66. var author = document.getElementsByClassName('header')[0].children[0].children[0].getAttribute('href');
  67. var all_cell_div = document.getElementsByClassName('cell');
  68.  
  69. for (var i in all_cell_div) {
  70. if (all_cell_div[i].id !== "") {
  71. try {
  72. var href = all_cell_div[i].children[0].children[0].children[0].children[2].children[2].children[0].getAttribute('href');
  73. var box = all_cell_div[i].children[0].children[0].children[0].children[2];
  74. if (author == href) {
  75. box.style.backgroundColor = 'rgb(250, 255, 189)';
  76. }
  77. } catch (e) {
  78. //console.log(e);
  79. continue;
  80. }
  81. }
  82. }
  83. })();