GO官网文档目录

GO官网文档目录, 解析cmd和doc两种路径文档

// ==UserScript==
// @name                GO官网文档目录
// @namespace           http://tampermonkey.net/
// @version             0.4
// @description         GO官网文档目录, 解析cmd和doc两种路径文档
// @author              pogusanqian
// @match               https://golang.google.cn/doc/*
// @match               https://golang.google.cn/cmd/*
// @icon                data:image/gif;base64,R0lGODlhAQABAAAAACH5BAEKAAEALAAAAAABAAEAAAICTAEAOw==
// @grant               none
// @license             MIT
// ==/UserScript==

(function () {
  // 创建目录节点
  const listWapper = document.body.appendChild(document.createElement('div'));
  const list = listWapper.appendChild(document.createElement('ul'));

  // 往目录节点中添加数据 h2表示的时doc路径下的路径, h3表示的时cmd下的路径
  const dataNode = location.pathname.startsWith('/doc') ? document.querySelectorAll('article h2') : document.querySelectorAll('article h3');
  for (const item of dataNode) {
    list.insertAdjacentHTML('beforeend', `<li style="list-style-type: none;"><a href="#${item.id}">${item.innerHTML}</a></li>`);
  }
  const totalListLink = `<a href="https://golang.google.cn/doc/">总目录</a>`;
  list.insertAdjacentHTML('afterbegin', `<li style="list-style-type: none;">${totalListLink}</li>`);

  // 设置样式
  list.style.cssText = `position: fixed; top: 50px; width: 300px; max-height: 800px; overflow: auto; resize: horizontal;`;
  window.onscroll = () => list.style.top = document.documentElement.scrollTop >= 50 ? "0px" : "50px";
})();