为掘金小册添加目录

栏目: JavaScript · 发布时间: 6年前

内容简介:左键点击Tampermonkey, 出来如图所示选项号粘贴如下代码到编辑区

左键点击Tampermonkey, 出来如图所示选项

为掘金小册添加目录
点击 管理面板
为掘金小册添加目录
再点击 +

第三步

粘贴如下代码到编辑区

// ==UserScript==
// @name         掘金小册目录
// @description:zh-cn 自动生成掘金小册目录
// @namespace    https://juejin.im/book/*
// @version      1.0
// @description  create content
// @author       Simon
// @match        https://juejin.im/book/*
// @require      https://cdn.bootcss.com/jquery/3.3.1/jquery.min.js
// @grant        none
// ==/UserScript==
var menuIndex = 0;

(function() {
    'use strict';
    init();
})();

function init() {
    var a = setInterval(() => {
        var content = document.getElementsByClassName('article-content');
        if(content){
            window.clearInterval(a);
            initSidebar();
        }
    }, 1000);
}

function initSidebar() {

    let titles = $('.article-content').find('h1,h2,h3,h4,h5,h6');
    console.log(titles.length)
    if(titles.length === 0){
        return;
    }

    
    let contentHeight = window.innerHeight;
    let asideContent = '<aside id="sideMenu" style="position: fixed;padding: 0px 15px 20px 0px;top: 0;right: 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:200px;max-width:300px;"><h2 style="padding-left:30px">目录<h2></aside>';
    $('.book-content').prepend(asideContent);
    $('#sideMenu').append('<ol id="menu_nav_ol" style="list-style:none;margin:0px;padding:0px;">');
    
    titles.each(function(){
          let tagName = $(this)[0].tagName.toLocaleLowerCase();
          let content = $(this).text();
          
          let newTagId =$(this).attr('id');
          if(!$(this).attr('id')){
              newTagId = 'id_'+menuIndex;
              $(this).attr('id',newTagId);
              menuIndex++;
          }
          if(newTagId !=='id_0')
              appendMenuItem(tagName,newTagId,content);
    });

    $('#sideMenu').append('</ol>');
    
    $('#menu_nav_ol li').on('click',function(){
        let targetId = $(this).attr('class');
        $(this).first().children("a").first().css('color', 'red');
        $("#"+targetId)[0].scrollIntoView({behavior: "instant", block: "center", inline: "nearest"});
        $('#menu_nav_ol').children().each(function(){
            let otherId = $(this).attr('class');
            if (targetId != otherId) {
                 $(this).first().children("a").first().css('color', 'black');
            }
        });
    });
}

function appendMenuItem(tagName,id,content){
    let paddingLeft = tagName.substring(1) * 20;
    $('#menu_nav_ol').append('<li class="' + id +'" style="padding-left: '+ paddingLeft +'px;margin:10px 0"><a style="color:black;font-weight:bold">' + content + '</a></li>');
}
复制代码

然后点击 文件 => 保存

为掘金小册添加目录

再次刷新即可, 效果如下:

为掘金小册添加目录

最后

  • 有时候刷新一次出不来目录就多刷几次
  • 切换章节后, 如需显示当前章节目录, 仍然需要刷新

以上所述就是小编给大家介绍的《为掘金小册添加目录》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

算法神探

算法神探

[美] 杰瑞米·库比卡 / 啊哈磊、李嘉浩 / 电子工业出版社 / 2017-2 / 65

《算法神探:一部谷歌首席工程师写的CS小说》围绕程序设计典型算法,精心编织了一个扣人心弦又趣味横生的侦探缉凶故事。小说主人公运用高超的搜索技巧和精深的算法知识,最终识破阴谋、缉拿元凶。其间,用二分搜索搜查走私船、用搜索树跟踪间谍、用深度优先搜索逃离监狱、用优先队列开锁及用最佳优先搜索追寻线索等跌宕起伏又富含算法精要的情节,让读者在愉悦的沉浸式体验中快速提升境界,加深对程序世界的理解。《算法神探:一......一起来看看 《算法神探》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

HTML 编码/解码
HTML 编码/解码

HTML 编码/解码

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具