Styling for the menu
[provvstudienna.user.js] / provvstudienna.user.js
1 // ==UserScript==
2 // @name        Provveditorato Studi Enna Fix
3 // @description Fix the site from the Provveditorato agli Studi di Enna
4 // @namespace   http://oblomov.myopenid.com
5 // @include     http://provvstudienna.it/*
6 // @include     http://*.provvstudienna.it/*
7 // @author      Giuseppe "Oblomov" Bilotta
8 // @version     20100819
9 // ==/UserScript==
10
11 if (typeof(unsafeWindow) == 'undefined') unsafeWindow = window;
12
13 String.prototype.ltrim = function() { return this.replace(/^\s+/,'') };
14 String.prototype.rtrim = function() { return this.replace(/\s+$/,'') };
15 String.prototype.trim = function() { return this.ltrim().rtrim() };
16
17 (function () {
18
19  /* replace the non-standard startDownload() behavior with standard XMLHttpRequest */
20  var marq = unsafeWindow.document.getElementById('externalmarquee');
21  if (marq) {
22   var url = marq.getAttribute('src').replace(/\\/g,'/');
23   xhr = new XMLHttpRequest();
24   xhr.open("GET", url, true);
25   xhr.onreadystatechange = function() {
26    if (xhr.readyState == 4) {
27     if (xhr.status == 200) {
28      marq.innerHTML = xhr.responseText;
29     } else {
30      marq.innerHTML = 'impossibile caricare le news';
31     }
32    }
33   }
34   xhr.send();
35  }
36
37  /* Replace the unneeded Java menu applet with a standard menu */
38  var menu = unsafeWindow.document.getElementsByTagName('applet')[0];
39  if (typeof(menu) == 'object') {
40   var pnode = menu.parentNode;
41   var neib = menu.nextSibling;
42   while (neib) {
43    pnode.removeChild(neib);
44    neib = menu.nextSibling;
45   }
46   var nodes = menu.getElementsByTagName('param');
47   var ops = [];
48   /* The following code expects menu items to appear in sequence:
49      label1, sublabel1.1, url1.1, sublabel1.2, url1.2, label2, etc
50      (as they are on the processed site. If params are given off-sequence,
51      it will break. Beware if you port the code to other sites too */
52   for (var i = 0; i < nodes.length; ++i) {
53    var param = nodes[i];
54    var match = /^(\d+)-Label$/.exec(param.name);
55    if (match) {
56     ops[parseFloat(match[1])-1] = {label: param.value.trim(), entry: []};
57     continue;
58    }
59    match = /^(\d+)-Entry(\d+)$/.exec(param.name);
60    if (match) {
61     ops[parseFloat(match[1])-1].entry[parseFloat(match[2]-1)] = {label: param.value.trim()};
62     continue;
63    }
64    match = /^(\d+)-URL(\d+)$/.exec(param.name);
65    if (match) {
66     ops[parseFloat(match[1])-1].entry[parseFloat(match[2]-1)].url = param.value.replace(/\\/g, '/');
67     continue;
68    }
69   }
70   /* DEBUG
71   if (typeof(JSON) == 'object') {
72    alert(JSON.stringify(ops));
73   }
74   */
75   if (ops.length > 0) {
76    pnode.removeChild(menu);
77    menu = document.createElement('ul');
78    menu.id = 'hypermenu';
79    for (var i = 0; i < ops.length; ++i) {
80     menu.appendChild(document.createElement('li'));
81     var item = menu.lastChild;
82     item.id = ops[i].label.replace(/\./g,'').replace(/\W/g,'-').toLowerCase();
83     item.appendChild(document.createElement('p'));
84     item.lastChild.appendChild(document.createTextNode(ops[i].label));
85     item.appendChild(document.createElement('ul'));
86     item = item.lastChild;
87     for (var j = 0; j < ops[i].entry.length; ++j) {
88      item.appendChild(document.createElement('li'));
89      item.lastChild.appendChild(document.createElement('a'));
90      link = item.lastChild.lastChild;
91      link.href = ops[i].entry[j].url;
92      link.appendChild(document.createTextNode(ops[i].entry[j].label));
93     }
94    }
95    pnode.appendChild(menu);
96   }
97  }
98
99  /* Styling for the menu */
100  /* we could grab most of these from the params to the java applet, but I'm too lazy to write that code for now */
101  /* TODO: metal effect */
102  style  = '#hypermenu { font-family: Dialog, sans-serif; font-size: 12px; list-style-type: none; text-align: left; background-color: #e2e2e2; margin:0 auto; padding:0;width: 100%}';
103  style += '#hypermenu > li { margin:0; padding:0}';
104  style += '#hypermenu > li > p { font-weight: bold; background-color: #284828; margin:0; padding: 6px 0 6px 3px; }';
105  style += '#hypermenu > li > p:hover { color: #00FF00; }';
106  style += '#hypermenu ul { color:black; font-weight:normal; list-style-type: none; margin:0; padding:0; background-color: #C0C0C0}';
107  style += '#hypermenu ul > li { padding: 3px}';
108  style += '#hypermenu ul > li:hover { color: #CC0000; font-weight: bold}';
109  style += '#hypermenu a { color:inherit; text-decoration:none }' ;
110
111  /* rolling effect */
112  style += '#hypermenu > li > p { border: 1px solid gray }';
113  style += '#hypermenu > li > ul {display:none}';
114  style += '#hypermenu > li:hover > ul {display:block}';
115
116  stel = unsafeWindow.document.createElement('style');
117  stel.appendChild(document.createTextNode(style));
118  unsafeWindow.document.getElementsByTagName('head')[0].appendChild(stel);
119
120
121 })();