在github看到一个很长的文章,想迅速定位一下,于是动手写了一个简单提取工具,例子:
最终是这样的:
image.png
function craeteDiv(){
var div = document.createElement('div');
div.setAttribute('style', 'z-index: 10000; width: 200px; height: 100%; top: 0px; left:0px; padding: 10px; position: fixed; color: #000; font-size: 8pt; overflow: scroll;');
div.innerHTML = getToc();
div.className = "toc";
document.body.prepend(div);
}
function getToc(){
var list="\n",
toc="";
i = 0;
toc = document.querySelectorAll('h1,h2').forEach( e =>{
if(i === 0 | i === 1)e.innerText = "";
e.id = i;
console.log(e.tagName)
if(e.tagName === 'H1'){
list += '\n<p style="font-weight: bold;line-height: 1em; color: #000;"><a style="color: #000" href="#'+ i + '">' + e.innerText + '</a></p>\n'
} else if(e.tagName === 'H2'){
list += '\n<p>    - <a style="color: #000" href="#'+ i + '">' + e.innerText + '</a></p>\n'
} else if(e.tagName === 'H3') {
list += '\n<p>        - <a style="color: #000" href="#'+ i + '">' + e.innerText + '</a></p>\n'
}
i++;
})
//console.log(list)
return list;
}
craeteDiv();













网友评论