您需要先安装一个扩展,例如 篡改猴、Greasemonkey 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 暴力猴,之后才能安装此脚本。
您需要先安装一个扩展,例如 篡改猴 或 Userscripts ,之后才能安装此脚本。
您需要先安装一款用户脚本管理器扩展,例如 Tampermonkey,才能安装此脚本。
您需要先安装用户脚本管理器扩展后才能安装此脚本。
Generate a TOC of github readme.
当前为
// ==UserScript== // @name TOC of github readme // @namespace http://tampermonkey.net/ // @version 0.1 // @description Generate a TOC of github readme. // @author theme // @match https://github.com/* // @grant none // ==/UserScript== (function() { 'use strict'; // Your code here... function pre_iterate_node(tree_node, callback_f){ callback_f(tree_node); var children = tree_node.children; if ( children.length > 0 ) { var i; for (i = 0; i< children.length; i++){ pre_iterate_node(children[i], callback_f); } } } function create_toc_item(text){ var div = document.createElement("div"); div.innerText = text; return div; } function toc_item_from_el(c){ // check a element, populate a TOC item var toc_item = null; if (c.tagName == "H1") { toc_item = create_toc_item(c.innerText); // create a toc item of this title toc_item.style.backgroundColor = "rgba(204,255,204,0.5)"; toc_item.style.foneSize = "2em"; return toc_item; } else if ( c.tagName == "H2" ) { toc_item = create_toc_item(c.innerText); toc_item.style.backgroundColor = "rgba(221,255,221,0.5)"; toc_item.style.foneSize = "1.5em"; toc_item.style.textIndent = "1em"; return toc_item; } else if ( c.tagName == "H3" ) { toc_item = create_toc_item(c.innerText); toc_item.style.backgroundColor = "rgba(233,255,233,0.5)"; toc_item.style.foneSize = "1.17em"; toc_item.style.textIndent = "2em"; return toc_item; } else if (c.tagName == "P" && c.innerText.startsWith("# ")){ toc_item = create_toc_item(c.innerText); toc_item.style.backgroundColor = "rgba(255,255,255,0.5)"; toc_item.style.foneSize = "1em"; toc_item.style.textIndent = "3em"; return toc_item; } else if (c.tagName == "A" && (c.getAttribute("href").startsWith("# "))) { toc_item = create_toc_item(""); toc_item.innerHTML = '<a href="' + c.getAttribute("href") + '">' + c.innerText + '</a>'; toc_item.style.backgroundColor = "rgba(255,255,255,0.5)"; toc_item.style.foneSize = "1em"; toc_item.style.textIndent = "3em"; return toc_item; } else { return null; } } // logic var div_readme = document.getElementById("readme"); if (null !== div_readme) { // render TOC inside it var toc = document.createElement("div"); toc.setAttribute("id", "toc"); var css = toc.style; css.border = "1px solid"; css.position = "fixed"; css.display = "block"; css.top = "10%"; css.left = "10px"; css.margin = "5px"; //css.width = "20%"; document.body.append(toc); pre_iterate_node(div_readme, function(c){ // for every div under `div_readme` var toc_item = toc_item_from_el(c); if (null !== toc_item) { // append toc item to toc div toc.appendChild(toc_item); } }); } })();