eur-lex_toc.user.js 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. // ==UserScript==
  2. // @name eur-lex toc
  3. // @namespace eur-lex
  4. // @include http://eur-lex.europa.eu/legal-content/EN/*
  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. width: 100%;\
  22. }\
  23. #the-toc-this-website-lacks > div {\
  24. direction: ltr;\
  25. overflow-x: auto;\
  26. background-color: antiquewhite;\
  27. resize: both;\
  28. }\
  29. #the-toc-this-website-lacks li {\
  30. padding: 0.5em;\
  31. margin: 0.5em;\
  32. border-radius: 0.2em;\
  33. }\
  34. #the-toc-this-website-lacks h1 {\
  35. text-align: center;\
  36. font-size: x-large;\
  37. margin: 0.5em;\
  38. }\
  39. ';
  40. var toc = document.createElement('div');
  41. var toc_ltr = document.createElement('div');
  42. var visibility_button = document.createElement('button');
  43. var h1_articles = document.createElement('h1');
  44. var ol_articles = document.createElement('ol');
  45. var h1_annexes = document.createElement('h1');
  46. var ol_annexes = document.createElement('ol');
  47. toc.style = "resize: both;";
  48. var has_css_resize = (toc.style !== "");
  49. toc.style = "";
  50. h1_articles.textContent = 'Articles';
  51. h1_annexes.textContent = 'Annexes';
  52. var article_titles = document.evaluate("//div[@lang='EN']/p[@class='ti-art'][starts-with(text(), 'Article')]",
  53. document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
  54. var annex_titles = document.evaluate("//div[@lang='EN']/div[@id]/p[1][@class='doc-ti'][starts-with(text(), 'ANNEX')]",
  55. document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
  56. /* TODO: there are non-article and non-annex sections that should go in the TOC too. */
  57. for (var i = 0; i < article_titles.snapshotLength; i++) {
  58. var id = article_titles.snapshotItem(i);
  59. var title = id.nextElementSibling;
  60. var n = id.textContent.replace(/Article /, '');
  61. id.id = 'article-' + n;
  62. var li = document.createElement('li');
  63. var a = document.createElement('a');
  64. a.href = '#' + id.id;
  65. a.textContent = title.textContent;
  66. li.appendChild(a);
  67. ol_articles.appendChild(li);
  68. }
  69. for (var i = 0; i < annex_titles.snapshotLength; i++) {
  70. var id = annex_titles.snapshotItem(i);
  71. var title = id.nextElementSibling;
  72. var n = id.textContent.replace(/ANNEX /, '');
  73. id.id = 'annex-' + n;
  74. var li = document.createElement('li');
  75. var a = document.createElement('a');
  76. a.href = '#' + id.id;
  77. a.textContent = title.textContent;
  78. li.appendChild(a);
  79. ol_annexes.appendChild(li);
  80. }
  81. visibility_button.textContent = "Toggle TOC";
  82. toc.id = 'the-toc-this-website-lacks';
  83. visibility_button.onclick = (function() {
  84. var visible = true;
  85. return function() {
  86. visible = ! visible;
  87. toc_ltr.style = visible ? "" : "height: 0px;";
  88. }
  89. })();
  90. if (! has_css_resize) {
  91. toc.appendChild(visibility_button);
  92. }
  93. if (article_titles.snapshotLength > 0) {
  94. toc_ltr.appendChild(h1_articles);
  95. toc_ltr.appendChild(ol_articles);
  96. }
  97. if (annex_titles.snapshotLength > 0) {
  98. toc_ltr.appendChild(h1_annexes);
  99. toc_ltr.appendChild(ol_annexes);
  100. }
  101. toc.appendChild(toc_ltr);
  102. document.getElementsByTagName('body')[0].appendChild(toc);
  103. document.getElementsByTagName('head')[0].appendChild(style);