eur-lex_toc.user.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // ==UserScript==
  2. // @name eur-lex toc
  3. // @namespace eur-lex
  4. // @include http://eur-lex.europa.eu/*
  5. // @version 1
  6. // @grant none
  7. // ==/UserScript==
  8. var style = document.createElement('style');
  9. style.type = 'text/css';
  10. style.innerHTML = '\
  11. #the-toc-this-website-lacks {\
  12. position: fixed;\
  13. top: 0em;\
  14. left: 0em;\
  15. height: 100%;\
  16. overflow-y: auto;\
  17. direction: rtl;\
  18. white-space: nowrap;\
  19. }\
  20. #the-toc-this-website-lacks > button {\
  21. display: block;\
  22. }\
  23. #the-toc-this-website-lacks > div {\
  24. direction: ltr;\
  25. overflow-x: auto;\
  26. }\
  27. #the-toc-this-website-lacks li {\
  28. padding: 0.5em;\
  29. margin: 0.5em;\
  30. border-radius: 0.2em;\
  31. }\
  32. #the-toc-this-website-lacks h1 {\
  33. text-align: center;\
  34. font-size: x-large;\
  35. }\
  36. ';
  37. var toc = document.createElement('div');
  38. var toc_ltr = document.createElement('div');
  39. var fold_button = document.createElement('button');
  40. var h1_articles = document.createElement('h1');
  41. var ol_articles = document.createElement('ol');
  42. var h1_annexes = document.createElement('h1');
  43. var ol_annexes = document.createElement('ol');
  44. h1_articles.textContent = 'Articles';
  45. h1_annexes.textContent = 'Annexes';
  46. var article_titles = document.evaluate("//div[@lang='EN']/p[@class='ti-art'][starts-with(text(), 'Article')]",
  47. document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
  48. var annex_titles = document.evaluate("//div[@lang='EN']/div[@id]/p[1][@class='doc-ti'][starts-with(text(), 'ANNEX')]",
  49. document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
  50. /* TODO: there are non-article and non-annex sections that should go in the TOC too. */
  51. for (var i = 0; i < article_titles.snapshotLength; i++) {
  52. var id = article_titles.snapshotItem(i);
  53. var title = id.nextElementSibling;
  54. var n = id.textContent.replace(/Article /, '');
  55. id.id = 'article-' + n;
  56. var li = document.createElement('li');
  57. var a = document.createElement('a');
  58. a.href = '#' + id.id;
  59. a.textContent = title.textContent;
  60. li.appendChild(a);
  61. ol_articles.appendChild(li);
  62. }
  63. for (var i = 0; i < annex_titles.snapshotLength; i++) {
  64. var id = annex_titles.snapshotItem(i);
  65. var title = id.nextElementSibling;
  66. var n = id.textContent.replace(/ANNEX /, '');
  67. id.id = 'annex-' + n;
  68. var li = document.createElement('li');
  69. var a = document.createElement('a');
  70. a.href = '#' + id.id;
  71. a.textContent = title.textContent;
  72. li.appendChild(a);
  73. ol_annexes.appendChild(li);
  74. }
  75. fold_button.textContent = "(Un)Fold TOC";
  76. toc.id = 'the-toc-this-website-lacks';
  77. fold_button.onclick = (function() {
  78. var folded = false;
  79. return function() {
  80. var width = "";
  81. folded = ! folded;
  82. if (folded) {
  83. width = "width: 16em;";
  84. }
  85. else {
  86. width = "";
  87. }
  88. toc.style = width;;
  89. }
  90. })();
  91. toc_ltr.appendChild(fold_button);
  92. toc_ltr.appendChild(h1_articles);
  93. toc_ltr.appendChild(ol_articles);
  94. toc_ltr.appendChild(h1_annexes);
  95. toc_ltr.appendChild(ol_annexes);
  96. toc.appendChild(toc_ltr);
  97. document.getElementsByTagName('body')[0].appendChild(toc);
  98. document.getElementsByTagName('head')[0].appendChild(style);