AutoTOC

Automatically creates a table of contents for all HTML-headers on a web page.

目前为 2016-01-01 提交的版本。查看 最新版本

  1. // ==UserScript==
  2. // @name AutoTOC
  3. // @namespace http://runeskaug.com/greasemonkey
  4. // @description Automatically creates a table of contents for all HTML-headers on a web page.
  5. // @author Rune Skaug (greasemonkey@runeskaug.com)
  6. // @include http://*
  7. // @include https://*
  8. // @version 1.7.65
  9. // @released 2005-07-06
  10. // @updated 2006-12-25
  11. // @compatible Greasemonkey, Opera 8/9
  12. //
  13. // Change history (main changes):
  14. //
  15. // 1.0 - Basic version
  16. // 1.1 - Adds Hide TOC button
  17. // 1.2 - Only show visible headings (Firefox)
  18. // - Work around Firefox rendering bugs
  19. // - Adds Menu item: AutoTOC: Toggle display (Firefox)
  20. // 1.3 - Sets a session cookie for TOC hiding (per domain)
  21. // 1.4 - Disables adding of menu item.
  22. // - Choose your own string pattern to match (RXmatch)
  23. // 1.5 - Minor adjustments for GM 0.6/FF1.5
  24. // - Moved closebutton to the left
  25. // - Flash on select
  26. // 1.6 - Xpath search replaces treewalker, FF1,1.5,Opera9
  27. // 1.7 - Minor fixes, screen-only stylesheet
  28. //
  29. // @ujs:category browser: enhancements
  30. // @ujs:published 2005-07-06 13:03
  31. // @ujs:modified 2005-07-06 17:31
  32. // @ujs:documentation http://userjs.org/scripts/browser/enhancements/auto-toc
  33. // @ujs:download http://userjs.org/scripts/download/browser/enhancements/auto-toc.user.js
  34. // @ujs:download.gm http://userjs.org/scripts/download/browser/enhancements/auto-toc.user.js
  35. // ==/UserScript==
  36.  
  37.  
  38. /*
  39. * Creative Commons Attribution License
  40. * http://creativecommons.org/licenses/by/2.5/
  41. */
  42.  
  43. (function() {
  44. // text constants
  45. var fullTOCText = "Table of Contents";
  46. var hideBtnText = "\u00a0X\u00a0";
  47. var RXmatch = /^h[1-4]$/i; // regexp
  48. var XPmatch = "//h1|//h2|//h3|//h4"; // xpath
  49. //set the optional behaviour of the TOC box
  50. // if true, resets it to its initial state after you have selected a header - false does not reset it
  51. var resetSelect = true;
  52. // if true, shows a "Hide TOC"/close button on the left side of the bar
  53. var showHide = true;
  54. // if true, hides the TOC for all pages on current site
  55. var useCookie = true;
  56. // if true, adds menu item (Toggle TOC)
  57. var addMenuItem = true;
  58.  
  59. function f() {
  60. //only on (X)HTML pages containing at least one heading - excludes XML files, text files, plugins and images (displayed using minimal HTML)
  61. if (document.getElementsByTagName("html").length && ( document.getElementsByTagName('h1').length ||
  62. document.getElementsByTagName('h2').length ||
  63. document.getElementsByTagName('h3').length ||
  64. document.getElementsByTagName('h4').length )
  65. && (!useCookie || (useCookie && getCookie('autotoc_hide')!='true'))) {
  66. var aHs = getHTMLHeadings();
  67. if (aHs.length>1) { // HTML document, more than one heading.
  68. var body = document.getElementsByTagName('body')[0];
  69. body.style.marginBottom = "24px !important";
  70. addCSS(
  71. '@media print { #js-toc {display: none; visibility: hidden; }}\n'+
  72. '@media screen { #js-toc {position: fixed; left: 0; right: 0; top: auto; bottom: 0; width: 100%; display: block; border-top: 1px solid #777; background: #ddd; margin: 0; padding: 3px; z-index: 9999; }\n'+
  73. '#js-toc select { font: 8pt verdana, sans-serif; margin: 0; margin-left:5px; background: #fff; color: #000; float: left; padding: 0; vertical-align: bottom;}\n'+
  74. '#js-toc option { font: 8pt verdana, sans-serif; color: #000; }\n'+
  75. '#js-toc .hideBtn { font: bold 8pt verdana, sans-serif !important; float: left; margin-left: 2px; margin-right: 2px; padding: 1px; border: 1px solid #999; background: #e7e7e7; }\n'+
  76. '#js-toc .hideBtn a { color: #333; text-decoration: none; background: transparent;} #js-toc .hideBtn a:hover { color: #333; text-decoration: none; background: transparent;} }'
  77. );
  78. // Browser sniff++ - due to rendering bug(s) in FF1.0
  79. var toc = document.createElement(window.opera||showHide?'tocdiv':'div');
  80. toc.id = 'js-toc';
  81. if (showHide) {
  82. var hideDiv = document.createElement('div');
  83. hideDiv.setAttribute('class','hideBtn');
  84. var hideLink = document.createElement('a');
  85. hideLink.setAttribute("href","#");
  86. hideLink.setAttribute("onclick",useCookie?"document.getElementById('js-toc').style.display = 'none'; document.cookie = 'autotoc_hide=true; path=/'; return false;":"document.getElementById('js-toc').style.display = 'none';");
  87. hideLink.appendChild(document.createTextNode(hideBtnText));
  88. hideDiv.appendChild(hideLink);
  89. toc.appendChild(hideDiv);
  90. }
  91. tocSelect = document.createElement('select');
  92. tocSelect.setAttribute("onchange", "if(this.value){function flash(rep,delay) { for (var i=rep;i>0;i--) {window.setTimeout('el.style.background=\"#ff7\";',delay*i*2);window.setTimeout('el.style.background=elbg',delay*((i*2)+1));};}; elid = this.value; el=document.getElementById(elid); elbg=el.style.background; location.href='#'+elid; flash(5,100);"+(resetSelect?"this.selectedIndex=0;}":"}"));
  93. tocSelect.id = 'toc-select';
  94. tocEmptyOption = document.createElement('option');
  95. tocEmptyOption.setAttribute('value','');
  96. tocEmptyOption.appendChild(document.createTextNode(fullTOCText));
  97. tocSelect.appendChild(tocEmptyOption);
  98. toc.appendChild(tocSelect);
  99. document.body.appendChild(toc);
  100. for (var i=0,aH;aH=aHs[i];i++) {
  101. if (aH.offsetWidth) {
  102. op = document.createElement("option");
  103. op.appendChild(document.createTextNode(gs(aH.tagName)+getInnerText(aH).substring(0,100)));
  104. var refID = aH.id ? aH.id : aH.tagName+'-'+(i*1+1);
  105. op.setAttribute("value", refID);
  106. document.getElementById("toc-select").appendChild(op);
  107. aH.id = refID;
  108. }
  109. }
  110. }
  111. }
  112. };
  113. function autoTOC_toggleDisplay() {
  114. if (document.getElementById('js-toc')) {
  115. // toc-bar exists
  116. if (document.getElementById('js-toc').style.display == 'none') {
  117. document.getElementById('js-toc').style.display = 'block';
  118. if (useCookie) {document.cookie = 'autotoc_hide=; path=/';}
  119. }
  120. else {
  121. document.getElementById('js-toc').style.display = 'none';
  122. if (useCookie) {document.cookie = 'autotoc_hide=true; path=/';}
  123. };
  124. } else {
  125. // toc-bar not created yet, clear hide-cookie and run main script
  126. if (useCookie) {document.cookie = 'autotoc_hide=; path=/';}
  127. f();
  128. }
  129. }
  130. function getHTMLHeadings() {
  131. function acceptNode(node) {
  132. if (node.tagName.match(RXmatch)) { if (node.value+''!='') { return NodeFilter.FILTER_ACCEPT; } }
  133. return NodeFilter.FILTER_SKIP;
  134. }
  135. outArray = new Array();
  136. // XPath
  137. if (document.evaluate) {
  138. var nodes = document.evaluate(XPmatch, document, null, XPathResult.ANY_TYPE, null);
  139. var thisHeading = nodes.iterateNext();
  140. var j = 0;
  141. while (thisHeading) {
  142. if (thisHeading.textContent+''!='') {
  143. outArray[j++] = thisHeading;
  144. }
  145. thisHeading = nodes.iterateNext();
  146. }
  147. }
  148. // document.getElementsByTagName - slow! :)
  149. else {
  150. var els = document.getElementsByTagName("*");
  151. var j = 0;
  152. for (var i=0,el;el=els[i];i++) {
  153. if (el.tagName.match(RXmatch)) outArray[j++] = el;
  154. }
  155. }
  156. return outArray;
  157. }
  158. function addCSS(css) {
  159. var head, styleLink;
  160. head = document.getElementsByTagName('head')[0];
  161. if (!head) { return; }
  162. styleLink = document.createElement('link');
  163. styleLink.setAttribute('rel','stylesheet');
  164. styleLink.setAttribute('type','text/css');
  165. styleLink.setAttribute('href','data:text/css,'+escape(css));
  166. head.appendChild(styleLink);
  167. }
  168. function gs(s){
  169. s = s.toLowerCase();
  170. var ret = "";
  171. for (var i=1; i<(s.substring(1)*1);i++) {
  172. ret = ret + "\u00a0 \u00a0 ";
  173. }
  174. return ret;
  175. }
  176. function getInnerText(el) {
  177. var s='';
  178. for (var i=0,node; node=el.childNodes[i]; i++) {
  179. if (node.nodeType == 1) s += getInnerText(node);
  180. else if (node.nodeType == 3) s += node.nodeValue;
  181. }
  182. return s;
  183. }
  184. function getCookie(cname)
  185. {
  186. var namesep = cname + "=";
  187. var ca = document.cookie.split(';');
  188. for(var i=0, c; c=ca[i]; i++)
  189. {
  190. c = c.replace(/^\s*|\s*$/g,"");
  191. if (c.indexOf(namesep) == 0) {
  192. return c.substring(namesep.length,c.length);
  193. }
  194. }
  195. return null;
  196. }
  197. // main()
  198. if (!window.opera && addMenuItem) {
  199. GM_registerMenuCommand('AutoTOC: Toggle display', autoTOC_toggleDisplay);
  200. }
  201. f();
  202. })();