123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115 |
- // ==UserScript==
- // @name eur-lex toc
- // @namespace eur-lex
- // @include http://eur-lex.europa.eu/*
- // @version 1
- // @grant none
- // ==/UserScript==
- var style = document.createElement('style');
- style.type = 'text/css';
- style.innerHTML = '\
- #the-toc-this-website-lacks {\
- position: fixed;\
- top: 0em;\
- left: 0em;\
- height: 100%;\
- overflow-y: auto;\
- direction: rtl;\
- white-space: nowrap;\
- }\
- #the-toc-this-website-lacks > button {\
- display: block;\
- }\
- #the-toc-this-website-lacks > div {\
- direction: ltr;\
- overflow-x: auto;\
- }\
- #the-toc-this-website-lacks li {\
- padding: 0.5em;\
- margin: 0.5em;\
- border-radius: 0.2em;\
- }\
- #the-toc-this-website-lacks h1 {\
- text-align: center;\
- font-size: x-large;\
- }\
- ';
- var toc = document.createElement('div');
- var toc_ltr = document.createElement('div');
- var fold_button = document.createElement('button');
- var h1_articles = document.createElement('h1');
- var ol_articles = document.createElement('ol');
- var h1_annexes = document.createElement('h1');
- var ol_annexes = document.createElement('ol');
- h1_articles.textContent = 'Articles';
- h1_annexes.textContent = 'Annexes';
- var article_titles = document.evaluate("//div[@lang='EN']/p[@class='ti-art'][starts-with(text(), 'Article')]",
- document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
- var annex_titles = document.evaluate("//div[@lang='EN']/div[@id]/p[1][@class='doc-ti'][starts-with(text(), 'ANNEX')]",
- document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null);
- /* TODO: there are non-article and non-annex sections that should go in the TOC too. */
- for (var i = 0; i < article_titles.snapshotLength; i++) {
- var id = article_titles.snapshotItem(i);
- var title = id.nextElementSibling;
-
- var n = id.textContent.replace(/Article /, '');
-
- id.id = 'article-' + n;
-
- var li = document.createElement('li');
- var a = document.createElement('a');
- a.href = '#' + id.id;
- a.textContent = title.textContent;
- li.appendChild(a);
- ol_articles.appendChild(li);
- }
- for (var i = 0; i < annex_titles.snapshotLength; i++) {
- var id = annex_titles.snapshotItem(i);
- var title = id.nextElementSibling;
- var n = id.textContent.replace(/ANNEX /, '');
- id.id = 'annex-' + n;
- var li = document.createElement('li');
- var a = document.createElement('a');
- a.href = '#' + id.id;
- a.textContent = title.textContent;
- li.appendChild(a);
- ol_annexes.appendChild(li);
- }
- fold_button.textContent = "(Un)Fold TOC";
- toc.id = 'the-toc-this-website-lacks';
- fold_button.onclick = (function() {
- var folded = false;
- return function() {
- var width = "";
- folded = ! folded;
- if (folded) {
- width = "width: 16em;";
- }
- else {
- width = "";
- }
- toc.style = width;;
- }
- })();
- toc_ltr.appendChild(fold_button);
- toc_ltr.appendChild(h1_articles);
- toc_ltr.appendChild(ol_articles);
- toc_ltr.appendChild(h1_annexes);
- toc_ltr.appendChild(ol_annexes);
- toc.appendChild(toc_ltr);
- document.getElementsByTagName('body')[0].appendChild(toc);
- document.getElementsByTagName('head')[0].appendChild(style);
|