Wikipedia Inline Footnotes

Shows footnotes inline when clicked.

目前為 2014-04-19 提交的版本,檢視 最新版本

  1. // ==UserScript==
  2. // @name Wikipedia Inline Footnotes
  3. // @version 1.2
  4. // @namespace http://github.com/johan
  5. // @description Shows footnotes inline when clicked.
  6. // @include http://*.wikipedia.org/wiki/*
  7. // ==/UserScript==
  8.  
  9. $x('//a[contains(@href,"#cite_note-")]').forEach(function(a) {
  10. a.addEventListener('click', inline_footnote, false);
  11. });
  12.  
  13. function inline_footnote(e) {
  14. var a = $X('ancestor-or-self::a[1]', e.target);
  15. var id = a.hash.slice(1);
  16. var tag = document.createElement('span');
  17. var copy = document.getElementById(id).cloneNode(true);
  18. var sup = $X('ancestor::sup[1]', a);
  19.  
  20. $x('a[sup] | *[.="^"]', copy).forEach(remove);
  21. tag.innerHTML = " ["+ copy.innerHTML.replace(/^[\s^]*/, '') +"]";
  22.  
  23. a.parentNode.replaceChild(tag, a);
  24. if (sup) {
  25. sup.parentNode.replaceChild(tag, sup);
  26. tag.className = 'citation';
  27. tag.id = sup.id;
  28. }
  29.  
  30. e.preventDefault();
  31. e.stopPropagation();
  32. }
  33.  
  34. function remove(node) {
  35. node.parentNode.removeChild(node);
  36. }
  37.  
  38. function $x( xpath, root ) {
  39. var doc = root ? root.evaluate ? root : root.ownerDocument : document;
  40. var got = doc.evaluate( xpath, root||doc, null, 0, null ), next, result = [];
  41. switch (got.resultType) {
  42. case got.STRING_TYPE:
  43. return got.stringValue;
  44. case got.NUMBER_TYPE:
  45. return got.numberValue;
  46. case got.BOOLEAN_TYPE:
  47. return got.booleanValue;
  48. default:
  49. while ((next = got.iterateNext()))
  50. result.push( next );
  51. return result;
  52. }
  53. }