知乎下一条

给知乎右下角添加"下一条"按钮,实现快速切换到下一个主题或评论。

  1. // ==UserScript==
  2. // @name 知乎下一条
  3. // @version 0.0.2
  4. // @description 给知乎右下角添加"下一条"按钮,实现快速切换到下一个主题或评论。
  5. // @author DH
  6. // @homepageURL https://denghao.me
  7. // @match https://*.zhihu.com/*
  8. // @require https://cdn.bootcss.com/jquery/2.2.1/jquery.min.js
  9. // @namespace https://gf.qytechs.cn/users/978718
  10. // ==/UserScript==
  11. this.$ = this.jQuery = jQuery.noConflict(true);
  12.  
  13. (function () {
  14. //公用方法
  15. const utils={
  16. throttle :function(func, wait) {
  17. let timeout;
  18. return function() {
  19. const context = this;
  20. const args = arguments;
  21. if (!timeout) {
  22. timeout = setTimeout(() => {
  23. timeout = null;
  24. func.apply(context, args)
  25. }, wait)
  26. }
  27. }
  28. },
  29. scrollTop: function (scrollTo, time= 100) {
  30. let scrollTop = document.documentElement.scrollTop || window.pageYOffset || document.body.scrollTop;
  31. let scrollFrom = parseInt(scrollTop),
  32. i = 0,
  33. step = 10;
  34. scrollTo = parseInt(scrollTo);
  35. time /= step;
  36. let interval = setInterval(function () {
  37. i++;
  38. let top = (scrollTo - scrollFrom) / time * i + scrollFrom;
  39. document.body.scrollTop = top;
  40. document.documentElement.scrollTop = top;
  41. if (i >= time) {
  42. clearInterval(interval);
  43. }
  44. }, step);
  45. }
  46. }
  47.  
  48. //主要逻辑
  49. const view = {
  50.  
  51. //知乎列表项位置集合
  52. zhihuAreas:[],
  53.  
  54. // 初始化
  55. init:function(){
  56. this.handleZhihuNextBtn();
  57. this.bindEvents();
  58. },
  59. // 知乎:下一个
  60. handleZhihuNextBtn:function(){
  61. //生成按钮
  62. const $wrap = $(".CornerButtons");
  63. const $btnNext = `<button id="zhihu-btn-next" data-tooltip="下一条" data-tooltip-position="left" data-tooltip-will-hide-on-click="true" class="Button CornerButton" style="margin-top:5px;padding: 0px;font-size: 14px;line-height: inherit;text-align: center;cursor: pointer;border: none;display: flex;align-items: center;justify-content: center;background: rgb(255, 255, 255);border-radius: 4px;width: 40px;height: 40px;color: rgb(133, 144, 166);box-shadow: rgb(18 18 18 / 10%) 0px 1px 3p"><svg width="24" height="24" viewBox="0 0 24 24" aria-hidden="true" style="transform:rotate(180deg);" fill="currentColor"><path fill-rule="evenodd" d="M13.204 3.107a1.75 1.75 0 0 0-2.408 0L3.806 9.73c-1.148 1.088-.378 3.02 1.204 3.02h2.24V20c0 .966.784 1.75 1.75 1.75h6A1.75 1.75 0 0 0 16.75 20v-7.25h2.24c1.582 0 2.353-1.932 1.204-3.02l-6.99-6.623Z" clip-rule="evenodd"></path></svg></button>`;
  64. $wrap.css({bottom:'40px'}).find('.CornerAnimayedFlex').css({height:'auto'}).append($btnNext);
  65. this.calcAreas()
  66. },
  67.  
  68. // 计算item-list位置
  69. calcAreas:function(){
  70. let $listItems=[];
  71. if($('.ListShortcut .TopstoryItem').length){
  72. $listItems=$('.ListShortcut .TopstoryItem');
  73. }else{
  74. $listItems=$('.ListShortcut .List-item');
  75. }
  76. let areas= [];
  77. $.each($listItems,(index,el)=>{
  78. const offset = 60;
  79. const start = $(el).offset().top-offset;
  80. const end = start + $(el).height()-offset;
  81. areas.push([start, end]);
  82. })
  83. this.zhihuAreas = areas;
  84. },
  85.  
  86. // 公用绑定事件
  87. bindEvents:function(){
  88. // 滚动计算位置
  89. $(window).on('scroll',utils.throttle(()=>{
  90. this.calcAreas();
  91. },300))
  92.  
  93. // 下一条
  94. $("#zhihu-btn-next").on('click',(el)=>{
  95. const winTop = $(document).scrollTop();
  96. const winH = $(document).height();
  97. const firstItem = this.zhihuAreas[0];
  98. if(firstItem && firstItem[0]>winTop){
  99. // 位置小于第一条,跳往第一条
  100. utils.scrollTop(firstItem[0]+5)
  101. }else{
  102. // 位置大于第一条,跳往下一条
  103. for(let i=0;i<=this.zhihuAreas.length;i++){
  104. const item = this.zhihuAreas[i];
  105. const nextItem = this.zhihuAreas[i+1];
  106. if(item && winTop>=item[0] && winTop<=item[1] && nextItem){
  107. utils.scrollTop(nextItem[0]+5)
  108. }
  109. }
  110. }
  111. })
  112. }
  113. }
  114.  
  115. //start
  116. view.init();
  117. })();

QingJ © 2025

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