// ==UserScript== // @name markdown自动生成简书目录 // @namespace create-jianshuList // @version 1.0 // @author YIN // @description 给简书增加目录功能 // @match http://www.jianshu.com/p/* // @match https://www.jianshu.com/p/* // @grant none // @downloadURL none // ==/UserScript== //去除字符串所有空格 function trim (str, is_global) { var result; result = str.replace(/(^\s+)|(\s+$)/g, ""); if (is_global&&is_global.toLowerCase() == "g") { result = result.replace(/\s/g, ""); } return result; } //转义尖括号 function toTxt(str) { var RexStr = /\<|\>/g str = str.replace(RexStr, function(MatchStr) { switch (MatchStr) { case "<": return "<"; break; case ">": return ">"; break; default: break; } }) return str; } var menuIndex = 0; //初始化标题索引 // 在侧边栏中添加目录项 function appendMenuItem(tagName,id,content) { console.log(tagName+" "+tagName.substring(1)); let paddingLeft = tagName.substring(1) * 20; //添加标题缩进 $('#menu_nav_ol').append('
'); } (function() { // 获取标题元素 let titles = $('body').find('h1,h2,h3,h4,h5,h6'); if(titles.length === 0) { return; } // 将文章内容右移 $('.post').css({padding:'0 150px 0 400px',width:'100%'}); // 在 body 标签内部添加 aside 侧边栏,用于显示文档目录 let asideContent = ''; $('.show-content').prepend(asideContent); $('#sideMenu').append(' '); // 绑定目录li点击事件,点击时跳转到对应的位置 $('#menu_nav_ol li').on('click',function() { let targetId = $(this).attr('class'); var _top=$("#"+targetId).offset().top-$("#"+targetId).outerHeight(true)-10 $('html,body').animate({ scrollTop:_top }, 300); }); })();