简书阅读增强脚本

生成简书侧边栏目录、去除侧边广告、滚动隐藏顶部导航栏

  1. // ==UserScript==
  2. // @name 简书阅读增强脚本
  3. // @namespace https://github.com/pcy190/TamperMonkeyScript
  4. // @version 1.2
  5. // @description 生成简书侧边栏目录、去除侧边广告、滚动隐藏顶部导航栏
  6. // @author HAPPY
  7. // @match http://www.jianshu.com/p/*
  8. // @match https://www.jianshu.com/p/*
  9. // @grant none
  10. // @require http://code.jquery.com/jquery-latest.js
  11. // ==/UserScript==
  12. var menuIndex = 0; //初始化标题索引
  13.  
  14. // 在侧边栏中添加目录项
  15. function appendMenuItem(tagName,id,content){
  16. console.log(tagName+" "+tagName.substring(1));
  17. let paddingLeft = tagName.substring(1) * 10; //添加标题缩进 tagName.substring(1) * 30
  18. $('#menu_nav_ol').append('<li class="' + id +'" style="padding-left: '+ paddingLeft +'px;cursor:pointer;transform:translate(0,-10px);"><b>' + content + '</b></li>');
  19. }
  20.  
  21. (function() {
  22. 'use strict';
  23. //去除广告#fixed-ad-container
  24. document.getElementById("fixed-ad-container").style.display="none";
  25. // 使文章区域宽度适配屏幕
  26. let wider = $('.note').width() - 300;
  27. let oriWidth = $('.post').width();
  28. console.log(wider);
  29. console.log(oriWidth);
  30. if (wider < oriWidth){
  31. wider = oriWidth;
  32. }
  33. // 适配宽度
  34. $('.post').width(wider);
  35.  
  36. // 保存标题元素
  37. let titles = $('body').find('h1,h2,h3,h4,h5,h6');
  38. if(titles.length === 0){
  39. return;
  40. }
  41. // 将文章内容右移
  42. $('.post').css('padding-left','150px');
  43. // 在 body 标签内部添加 aside 侧边栏,用于显示文档目录
  44. let contentHeight = window.innerHeight; //设置目录高度
  45. let asideContent = '<aside id="sideMenu" style="position: fixed;padding: 80px 15px 20px 15px;top: 0;left: 0;margin-bottom:20px;background-color: #eee;background-color: #eee;z-index: 810;overflow: scroll;max-height:'+contentHeight+'px;min-height:'+contentHeight+'px;min-width:100px;max-width:250px;"><h3>目录<h3></aside>';
  46. $('.show-content').prepend(asideContent);
  47. $('#sideMenu').append('<ol id="menu_nav_ol" style="list-style:none;margin:0px;padding:0px;">');// 不显示 li 项前面默认的点标志, 也不使用默认缩进
  48.  
  49. // 遍历文章中的所有标题行, 按需添加id值, 并增加记录到目录列表中
  50. titles.each(function(){
  51. let tagName = $(this)[0].tagName.toLocaleLowerCase();
  52. let content = $(this).text();
  53. // 若标题的id不存在,则使用新id
  54. let newTagId =$(this).attr('id');
  55. if(!$(this).attr('id')){
  56. newTagId = 'id_'+menuIndex;
  57. $(this).attr('id',newTagId);
  58. menuIndex++;
  59. }
  60. if(newTagId !=='id_0') //忽略标题
  61. appendMenuItem(tagName,newTagId,content);
  62. });
  63.  
  64. $('#sideMenu').append('</ol>');
  65. // 绑定目录li点击事件,点击时跳转到对应的位置
  66. $('#menu_nav_ol li').on('click',function(){
  67. let targetId = $(this).attr('class');
  68. $("#"+targetId)[0].scrollIntoView({
  69. behavior: "smooth", //behavior: "auto" | "instant" | "smooth"
  70. block: "center", //block: "start" | "center" | "end" | "nearest"
  71. inline: "nearest", //inline: "start" | "center" | "end" | "nearest"
  72. });
  73. });
  74. })();
  75.  
  76.  
  77. var style = document.createElement('style');
  78. style.id="__web-inspector-hide-shortcut-style__";
  79. style.type = 'text/css';
  80. style.innerHTML=".__web-inspector-hide-shortcut__, .__web-inspector-hide-shortcut__ *, \.__web-inspector-hidebefore-shortcut__::before, .__web-inspector-hideafter-shortcut__::after\
  81. {\
  82. visibility: hidden !important;\
  83. }";
  84. document.getElementsByTagName('HEAD').item(0).appendChild(style);
  85.  
  86.  
  87.  
  88. let top_nav=document.evaluate("/html/body/nav", document).iterateNext();
  89.  
  90. let scrollFunc = function (e) {
  91. e = e || window.event;
  92. if (e.wheelDelta) { //判断浏览器IE,谷歌滑轮事件
  93. if (e.wheelDelta > 0) {
  94. top_nav.className="navbar navbar-default navbar-fixed-top";
  95. }
  96. if (e.wheelDelta < -100) { //当滑轮向下滚动时 < 0
  97. top_nav.className="navbar navbar-default navbar-fixed-top __web-inspector-hide-shortcut__";
  98.  
  99. }
  100. } else if (e.detail) { //Firefox滑轮事件
  101. if (e.detail> 0) { //当滑轮向上滚动时
  102. top_nav.className="navbar navbar-default navbar-fixed-top";
  103. }
  104. if (e.detail< -100) { //当滑轮向下滚动时
  105. top_nav.className="navbar navbar-default navbar-fixed-top __web-inspector-hide-shortcut__";
  106. }
  107. }
  108. }
  109.  
  110. //绑定滚动事件
  111. window.onmousewheel = document.onmousewheel = scrollFunc; //滚动滑轮触发scrollFunc方法 //ie 谷歌
  112. if (document.addEventListener) {//firefox
  113. document.addEventListener('DOMMouseScroll', scrollFunc, false);
  114. }

QingJ © 2025

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