Plan B: inline footnotes

Lets you click footnotes in Earth Policy Institute's "Plan B" series of books to bring them to your eyes, rather than have you chase down the page to the reference, and back again afterward. Hypertext!

  1. // ==UserScript==
  2. // @name Plan B: inline footnotes
  3. // @namespace http://github.com/johan/
  4. // @description Lets you click footnotes in Earth Policy Institute's "Plan B" series of books to bring them to your eyes, rather than have you chase down the page to the reference, and back again afterward. Hypertext!
  5. // @include http://earth-policy.org/index.php?/books/pb*
  6. // @include http://www.earth-policy.org/index.php?/books/pb*
  7. // @version 0.0.1.20140419225641
  8. // ==/UserScript==
  9.  
  10. $x('//p/text()').filter(is_footnote).forEach(link_footnote);
  11.  
  12. function is_footnote(node) {
  13. return /^\s*\d+\.\D/.test(node.nodeValue || "");
  14. }
  15.  
  16. function link_footnote(text) {
  17. function inline(e) {
  18. e.preventDefault(); // don't go there this time -- just inline the footnote
  19. link.removeEventListener("click", inline, true); // but do, next time
  20.  
  21. inject_footnote(no, link);
  22. }
  23.  
  24. var no = text.nodeValue.match(/^\s*(\d+)\.\s*(.*)/), lead;
  25. if (no) {
  26. lead = no[2];
  27. no = parseInt(no[1], 10);
  28. }
  29. else
  30. return;
  31.  
  32. var refs = $x('preceding::text()[contains(.,"'+ no +'")]', text), ref, tail;
  33. while ((ref = refs.pop())) {
  34. if ((tail = ref.nodeValue.match("^(|.*\\D)"+ no + "\\s*$")))
  35. break;
  36. }
  37. if (tail) {
  38. tail = tail[1];
  39. } else
  40. return; // no link found in prior text
  41.  
  42. // content to footnote
  43. var link = A(no, "n-back", "n", ref.nextSibling, ref.parentNode);
  44. link.addEventListener("click", inline, true);
  45. ref.nodeValue = tail;
  46.  
  47. var anchor = A(no, "n", "n-back", text); // footnote to content
  48. text.nodeValue = " " + lead;
  49. }
  50.  
  51. function inject_footnote(no, at, ibid) {
  52. var tail = at.lastChild; // "]"
  53. if (!ibid) at.insertBefore(document.createTextNode("] "), tail);
  54.  
  55. var note = $X('id("f'+ no +'n")'), first = true;
  56. while (note) {
  57. note = note.nextSibling;
  58. if ($X('self::br | .//br', note)) break;
  59. var text = first && note.nodeName == "#text" && note.nodeValue, copy;
  60. if (text && (text = text.match(/^\s*ibid\.?(.*)/i))) {
  61. inject_footnote(no - 1, at, "ibidem"); // keep recursing back
  62. if (ibid) return; // and don't bother continuing when not found
  63.  
  64. copy = document.createTextNode(text[1]); // but proceed otherwise -- see
  65. // http://www.earth-policy.org/index.php?/books/pb4/PB4ch1_ss5#f58n-back
  66. } else {
  67. copy = note.cloneNode(true);
  68. }
  69. at.insertBefore(copy, tail);
  70. }
  71. }
  72.  
  73. function A(text, name, href, before, parent) {
  74. var span = document.createElement("span");
  75. span.style.color = "#999";
  76. span.textContent = "[";
  77.  
  78. var a = document.createElement("a");
  79. a.style.color = "#274980";
  80. a.textContent = text;
  81. span.id = 'f' + text + name;
  82. a.href = '#f' + text + href;
  83. span.appendChild(a);
  84.  
  85. span.appendChild(document.createTextNode("]"));
  86. (parent || before.parentNode).insertBefore(span, before);
  87.  
  88. // focus this one, if we've hotlinked to the anchor
  89. if (location.hash === "#" + span.id) {
  90. location.hash = "#";
  91. location.hash += span.id;
  92. }
  93.  
  94. return span;
  95. }
  96.  
  97. function $X( xpath, root ) {
  98. var got = $x( xpath, root );
  99. return got instanceof Array ? got[0] : got;
  100. }
  101.  
  102. function $x( xpath, root ) {
  103. var doc = root ? root.evaluate ? root : root.ownerDocument : document;
  104. var got = doc.evaluate( xpath, root||doc, null, 0, null ), next, result = [];
  105. switch (got.resultType) {
  106. case got.STRING_TYPE:
  107. return got.stringValue;
  108. case got.NUMBER_TYPE:
  109. return got.numberValue;
  110. case got.BOOLEAN_TYPE:
  111. return got.booleanValue;
  112. default:
  113. while ((next = got.iterateNext()))
  114. result.push( next );
  115. return result;
  116. }
  117. }