plinks.js 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /* Copyright 2005 Alex Schroeder <alex@emacswiki.org>
  2. based on http://simon.incutio.com/archive/2004/05/30/plinks#p-13
  3. Copyright 2004 Simon Willison
  4. This script adds purple numbers to paragraphs.
  5. https://oddmuse.org/wiki/Purple_Numbers_Extension
  6. */
  7. function plinkHighlight() {
  8. if (/#[0-9]+$/.test(document.location)) {
  9. // The user arrived via a plink
  10. var plink_id = document.location.href.split('#')[1];
  11. var para = document.getElementById("p" + plink_id);
  12. para.className = 'plink';
  13. }
  14. }
  15. function addpLinks() {
  16. /* Only show plinks on ordinary pages: They either use path_info
  17. * or keywords in the URL, not parameters. */
  18. if (/=/.test(document.location.href)) {
  19. return;
  20. }
  21. // find the content div: why is there no xpath?
  22. var elem = document.getElementsByTagName('div');
  23. var content;
  24. div:
  25. for (var i = 0; i < elem.length; i++) {
  26. var classes = elem[i].getAttribute('class').split(" ");
  27. for (var j = 0; j < classes.length; j++) {
  28. if (classes[j] == 'content') {
  29. content = elem[i];
  30. break div;
  31. }
  32. }
  33. }
  34. // identify all p and li items
  35. var items = new Array;
  36. elem = content.getElementsByTagName('p');
  37. for (var i = 0; i < elem.length; i++) {
  38. items.push(elem[i]);
  39. }
  40. elem = content.getElementsByTagName('li');
  41. for (var i = 0; i < elem.length; i++) {
  42. items.push(elem[i]);
  43. }
  44. // add named anchors at the beginning and a link to that anchor at
  45. // the end of all items
  46. for (var i = 0; i < items.length; i++) {
  47. var current = items[i];
  48. current.setAttribute("id", "p" + i);
  49. var anchor = document.createElement('a');
  50. anchor.name = i;
  51. current.insertBefore(anchor, current.firstChild);
  52. var plink = document.createElement('a');
  53. plink.href = document.location.href.split('#')[0] + '#' + i;
  54. plink.className = 'plink';
  55. plink.appendChild(document.createTextNode(' #'));
  56. current.appendChild(plink);
  57. }
  58. }
  59. function addLoadEvent(func) {
  60. var oldonload = window.onload;
  61. if (typeof window.onload != 'function') {
  62. window.onload = func;
  63. } else {
  64. window.onload = function() {
  65. oldonload();
  66. func();
  67. }
  68. }
  69. }
  70. addLoadEvent(addpLinks);
  71. addLoadEvent(plinkHighlight);