快速为网页中的文章添加目录(侵入式)

  • A+
所属分类:Javascript 前端
window.onload=function(){
  document.body.appendChild((_ => {
    const div = document.createElement('div')
    Object.assign(div.style,{
      position  : 'fixed',
      top       : '0px',
      left      : '0px',
      color     : 'white',
      background: '#4f81bd',
      fontSize  : '12px',
      lineHeight: '1.1',
      height    : '100%',
      width     : document.body.style.paddingLeft = '300px',
      overflow  : 'auto',
    })
    div.innerHTML = Array
      .from( document.querySelectorAll('h1,h2,h3') )
      .reduce( (o,e,i) => o + e.innerText + '\n', '' )
    // \n 替换成br标签
    return div
  })())
}
楚廉

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: