Beautify Apache server-status page

Apache HTTP server can have mod_status enabled and can

  1. // ==UserScript==
  2. // @name Beautify Apache server-status page
  3. // @namespace http://denilson.sa.nom.br/
  4. // @author Denilson Sá
  5. // @grant none
  6. // @version 1.5
  7. // @description Apache HTTP server can have mod_status enabled and can
  8. // display some server status via HTTP/HTML (there must be some
  9. // uncommented lines in httpd.conf). However, the page sent to
  10. // browser is a bit raw, completely unstyled, and hard to read.
  11. // This script tries to make that page much prettier and more
  12. // readable. It has been tested with Apache 2.0.55 and 2.2.21 and
  13. // should work on most versions, unless the generated HTML changes,
  14. // which will gradually degrade the script.
  15. // @include http://*/server-status
  16. // @include https://*/server-status
  17. // ==/UserScript==
  18.  
  19.  
  20. //////////////////////////////////////////////////////////////////////
  21. // Including code from:
  22. // http://www.kryogenix.org/code/browser/sorttable/
  23. // fetched on 2011-10-18
  24. // Slightly modified in order to fix bugs
  25.  
  26.  
  27. // Denilson has moved to the top of the script instead of the bottom.
  28. // Dean's forEach: http://dean.edwards.name/base/forEach.js
  29. /*
  30. forEach, version 1.0
  31. Copyright 2006, Dean Edwards
  32. License: http://www.opensource.org/licenses/mit-license.php
  33. */
  34.  
  35. // array-like enumeration
  36. if (!Array.forEach) { // mozilla already supports this
  37. Array.forEach = function(array, block, context) {
  38. for (var i = 0; i < array.length; i++) {
  39. block.call(context, array[i], i, array);
  40. }
  41. };
  42. }
  43.  
  44. // generic enumeration
  45. Function.prototype.forEach = function(object, block, context) {
  46. for (var key in object) {
  47. if (typeof this.prototype[key] == "undefined") {
  48. block.call(context, object[key], key, object);
  49. }
  50. }
  51. };
  52.  
  53. // character enumeration
  54. String.forEach = function(string, block, context) {
  55. Array.forEach(string.split(""), function(chr, index) {
  56. block.call(context, chr, index, string);
  57. });
  58. };
  59.  
  60. // globally resolve forEach enumeration
  61. var forEach = function(object, block, context) {
  62. if (object) {
  63. var resolve = Object; // default
  64. if (object instanceof Function) {
  65. // functions have a "length" property
  66. resolve = Function;
  67. } else if (object.forEach instanceof Function) {
  68. // the object implements a custom forEach method so use that
  69. object.forEach(block, context);
  70. return;
  71. } else if (typeof object == "string") {
  72. // the object is a string
  73. resolve = String;
  74. } else if (typeof object.length == "number") {
  75. // the object is array-like
  76. resolve = Array;
  77. }
  78. resolve.forEach(object, block, context);
  79. }
  80. };
  81.  
  82.  
  83.  
  84. /*
  85. SortTable
  86. version 2
  87. 7th April 2007
  88. Stuart Langridge, http://www.kryogenix.org/code/browser/sorttable/
  89. Instructions:
  90. Download this file
  91. Add <script src="sorttable.js"></script> to your HTML
  92. Add class="sortable" to any table you'd like to make sortable
  93. Click on the headers to sort
  94. Thanks to many, many people for contributions and suggestions.
  95. Licenced as X11: http://www.kryogenix.org/code/browser/licence.html
  96. This basically means: do what you want with it.
  97. */
  98.  
  99. // Denilson has changed this to always false.
  100. var stIsIE = false;
  101.  
  102. sorttable = {
  103. init: function() {
  104. // quit if this function has already been called
  105. if (arguments.callee.done) return;
  106. // flag this function so we don't do the same thing twice
  107. arguments.callee.done = true;
  108.  
  109. // Denilson removed this because the timer is not even set anymore
  110. // kill the timer
  111. //if (_timer) clearInterval(_timer);
  112. if (!document.createElement || !document.getElementsByTagName) return;
  113. sorttable.DATE_RE = /^(\d\d?)[\/\.-](\d\d?)[\/\.-]((\d\d)?\d\d)$/;
  114. forEach(document.getElementsByTagName('table'), function(table) {
  115. if (table.className.search(/\bsortable\b/) != -1) {
  116. sorttable.makeSortable(table);
  117. }
  118. });
  119. },
  120. makeSortable: function(table) {
  121. if (table.getElementsByTagName('thead').length == 0) {
  122. // table doesn't have a tHead. Since it should have, create one and
  123. // put the first table row in it.
  124. the = document.createElement('thead');
  125. the.appendChild(table.rows[0]);
  126. table.insertBefore(the,table.firstChild);
  127. }
  128. // Safari doesn't support table.tHead, sigh
  129. if (table.tHead == null) table.tHead = table.getElementsByTagName('thead')[0];
  130. if (table.tHead.rows.length != 1) return; // can't cope with two header rows
  131. // Sorttable v1 put rows with a class of "sortbottom" at the bottom (as
  132. // "total" rows, for example). This is B&R, since what you're supposed
  133. // to do is put them in a tfoot. So, if there are sortbottom rows,
  134. // for backwards compatibility, move them to tfoot (creating it if needed).
  135. sortbottomrows = [];
  136. for (var i=0; i<table.rows.length; i++) {
  137. if (table.rows[i].className.search(/\bsortbottom\b/) != -1) {
  138. sortbottomrows[sortbottomrows.length] = table.rows[i];
  139. }
  140. }
  141. if (sortbottomrows) {
  142. if (table.tFoot == null) {
  143. // table doesn't have a tfoot. Create one.
  144. tfo = document.createElement('tfoot');
  145. table.appendChild(tfo);
  146. }
  147. for (var i=0; i<sortbottomrows.length; i++) {
  148. tfo.appendChild(sortbottomrows[i]);
  149. }
  150. delete sortbottomrows;
  151. }
  152. // work through each column and calculate its type
  153. headrow = table.tHead.rows[0].cells;
  154. for (var i=0; i<headrow.length; i++) {
  155. // manually override the type with a sorttable_type attribute
  156. if (!headrow[i].className.match(/\bsorttable_nosort\b/)) { // skip this col
  157. mtch = headrow[i].className.match(/\bsorttable_([a-z0-9]+)\b/);
  158. if (mtch) { override = mtch[1]; }
  159. if (mtch && typeof sorttable["sort_"+override] == 'function') {
  160. headrow[i].sorttable_sortfunction = sorttable["sort_"+override];
  161. } else {
  162. headrow[i].sorttable_sortfunction = sorttable.guessType(table,i);
  163. }
  164. // make it clickable to sort
  165. headrow[i].sorttable_columnindex = i;
  166. headrow[i].sorttable_tbody = table.tBodies[0];
  167. dean_addEvent(headrow[i],"click", function(e) {
  168.  
  169. if (this.className.search(/\bsorttable_sorted\b/) != -1) {
  170. // if we're already sorted by this column, just
  171. // reverse the table, which is quicker
  172. sorttable.reverse(this.sorttable_tbody);
  173. this.className = this.className.replace('sorttable_sorted',
  174. 'sorttable_sorted_reverse');
  175. this.removeChild(document.getElementById('sorttable_sortfwdind'));
  176. sortrevind = document.createElement('span');
  177. sortrevind.id = "sorttable_sortrevind";
  178. sortrevind.innerHTML = stIsIE ? '&nbsp<font face="webdings">5</font>' : '&nbsp;&#x25B4;';
  179. this.appendChild(sortrevind);
  180. return;
  181. }
  182. if (this.className.search(/\bsorttable_sorted_reverse\b/) != -1) {
  183. // if we're already sorted by this column in reverse, just
  184. // re-reverse the table, which is quicker
  185. sorttable.reverse(this.sorttable_tbody);
  186. this.className = this.className.replace('sorttable_sorted_reverse',
  187. 'sorttable_sorted');
  188. this.removeChild(document.getElementById('sorttable_sortrevind'));
  189. sortfwdind = document.createElement('span');
  190. sortfwdind.id = "sorttable_sortfwdind";
  191. sortfwdind.innerHTML = stIsIE ? '&nbsp<font face="webdings">6</font>' : '&nbsp;&#x25BE;';
  192. this.appendChild(sortfwdind);
  193. return;
  194. }
  195. // remove sorttable_sorted classes
  196. theadrow = this.parentNode;
  197. forEach(theadrow.childNodes, function(cell) {
  198. if (cell.nodeType == 1) { // an element
  199. cell.className = cell.className.replace('sorttable_sorted_reverse','');
  200. cell.className = cell.className.replace('sorttable_sorted','');
  201. }
  202. });
  203. sortfwdind = document.getElementById('sorttable_sortfwdind');
  204. if (sortfwdind) { sortfwdind.parentNode.removeChild(sortfwdind); }
  205. sortrevind = document.getElementById('sorttable_sortrevind');
  206. if (sortrevind) { sortrevind.parentNode.removeChild(sortrevind); }
  207. this.className += ' sorttable_sorted';
  208. sortfwdind = document.createElement('span');
  209. sortfwdind.id = "sorttable_sortfwdind";
  210. sortfwdind.innerHTML = stIsIE ? '&nbsp<font face="webdings">6</font>' : '&nbsp;&#x25BE;';
  211. this.appendChild(sortfwdind);
  212.  
  213. // build an array to sort. This is a Schwartzian transform thing,
  214. // i.e., we "decorate" each row with the actual sort key,
  215. // sort based on the sort keys, and then put the rows back in order
  216. // which is a lot faster because you only do getInnerText once per row
  217. row_array = [];
  218. col = this.sorttable_columnindex;
  219. rows = this.sorttable_tbody.rows;
  220. for (var j=0; j<rows.length; j++) {
  221. row_array[row_array.length] = [sorttable.getInnerText(rows[j].cells[col]), rows[j]];
  222. }
  223. /* If you want a stable sort, uncomment the following line */
  224. //sorttable.shaker_sort(row_array, this.sorttable_sortfunction);
  225. /* and comment out this one */
  226. row_array.sort(this.sorttable_sortfunction);
  227. tb = this.sorttable_tbody;
  228. for (var j=0; j<row_array.length; j++) {
  229. tb.appendChild(row_array[j][1]);
  230. }
  231. delete row_array;
  232. });
  233. }
  234. }
  235. },
  236. guessType: function(table, column) {
  237. // guess the type of a column based on its first non-blank row
  238. sortfn = sorttable.sort_alpha;
  239. for (var i=0; i<table.tBodies[0].rows.length; i++) {
  240. text = sorttable.getInnerText(table.tBodies[0].rows[i].cells[column]);
  241. if (text != '') {
  242. if (text.match(/^-?[£$¤]?[\d,.]+%?$/)) {
  243. return sorttable.sort_numeric;
  244. }
  245. // check for a date: dd/mm/yyyy or dd/mm/yy
  246. // can have / or . or - as separator
  247. // can be mm/dd as well
  248. possdate = text.match(sorttable.DATE_RE)
  249. if (possdate) {
  250. // looks like a date
  251. first = parseInt(possdate[1]);
  252. second = parseInt(possdate[2]);
  253. if (first > 12) {
  254. // definitely dd/mm
  255. return sorttable.sort_ddmm;
  256. } else if (second > 12) {
  257. return sorttable.sort_mmdd;
  258. } else {
  259. // looks like a date, but we can't tell which, so assume
  260. // that it's dd/mm (English imperialism!) and keep looking
  261. sortfn = sorttable.sort_ddmm;
  262. }
  263. }
  264. }
  265. }
  266. return sortfn;
  267. },
  268. getInnerText: function(node) {
  269. // gets the text we want to use for sorting for a cell.
  270. // strips leading and trailing whitespace.
  271. // this is *not* a generic getInnerText function; it's special to sorttable.
  272. // for example, you can override the cell text with a customkey attribute.
  273. // it also gets .value for <input> fields.
  274. hasInputs = (typeof node.getElementsByTagName == 'function') &&
  275. node.getElementsByTagName('input').length;
  276. if (node.getAttribute("sorttable_customkey") != null) {
  277. return node.getAttribute("sorttable_customkey");
  278. }
  279. else if (typeof node.textContent != 'undefined' && !hasInputs) {
  280. return node.textContent.replace(/^\s+|\s+$/g, '');
  281. }
  282. else if (typeof node.innerText != 'undefined' && !hasInputs) {
  283. return node.innerText.replace(/^\s+|\s+$/g, '');
  284. }
  285. else if (typeof node.text != 'undefined' && !hasInputs) {
  286. return node.text.replace(/^\s+|\s+$/g, '');
  287. }
  288. else {
  289. switch (node.nodeType) {
  290. case 3:
  291. if (node.nodeName.toLowerCase() == 'input') {
  292. return node.value.replace(/^\s+|\s+$/g, '');
  293. }
  294. case 4:
  295. return node.nodeValue.replace(/^\s+|\s+$/g, '');
  296. break;
  297. case 1:
  298. case 11:
  299. var innerText = '';
  300. for (var i = 0; i < node.childNodes.length; i++) {
  301. innerText += sorttable.getInnerText(node.childNodes[i]);
  302. }
  303. return innerText.replace(/^\s+|\s+$/g, '');
  304. break;
  305. default:
  306. return '';
  307. }
  308. }
  309. },
  310. reverse: function(tbody) {
  311. // reverse the rows in a tbody
  312. newrows = [];
  313. for (var i=0; i<tbody.rows.length; i++) {
  314. newrows[newrows.length] = tbody.rows[i];
  315. }
  316. for (var i=newrows.length-1; i>=0; i--) {
  317. tbody.appendChild(newrows[i]);
  318. }
  319. delete newrows;
  320. },
  321. /* sort functions
  322. each sort function takes two parameters, a and b
  323. you are comparing a[0] and b[0] */
  324. sort_numeric: function(a,b) {
  325. aa = parseFloat(a[0].replace(/[^0-9.-]/g,''));
  326. if (isNaN(aa)) aa = 0;
  327. bb = parseFloat(b[0].replace(/[^0-9.-]/g,''));
  328. if (isNaN(bb)) bb = 0;
  329. return aa-bb;
  330. },
  331. sort_alpha: function(a,b) {
  332. if (a[0]==b[0]) return 0;
  333. if (a[0]<b[0]) return -1;
  334. return 1;
  335. },
  336. sort_ddmm: function(a,b) {
  337. mtch = a[0].match(sorttable.DATE_RE);
  338. y = mtch[3]; m = mtch[2]; d = mtch[1];
  339. if (m.length == 1) m = '0'+m;
  340. if (d.length == 1) d = '0'+d;
  341. dt1 = y+m+d;
  342. mtch = b[0].match(sorttable.DATE_RE);
  343. y = mtch[3]; m = mtch[2]; d = mtch[1];
  344. if (m.length == 1) m = '0'+m;
  345. if (d.length == 1) d = '0'+d;
  346. dt2 = y+m+d;
  347. if (dt1==dt2) return 0;
  348. if (dt1<dt2) return -1;
  349. return 1;
  350. },
  351. sort_mmdd: function(a,b) {
  352. mtch = a[0].match(sorttable.DATE_RE);
  353. y = mtch[3]; d = mtch[2]; m = mtch[1];
  354. if (m.length == 1) m = '0'+m;
  355. if (d.length == 1) d = '0'+d;
  356. dt1 = y+m+d;
  357. mtch = b[0].match(sorttable.DATE_RE);
  358. y = mtch[3]; d = mtch[2]; m = mtch[1];
  359. if (m.length == 1) m = '0'+m;
  360. if (d.length == 1) d = '0'+d;
  361. dt2 = y+m+d;
  362. if (dt1==dt2) return 0;
  363. if (dt1<dt2) return -1;
  364. return 1;
  365. },
  366. shaker_sort: function(list, comp_func) {
  367. // A stable sort function to allow multi-level sorting of data
  368. // see: http://en.wikipedia.org/wiki/Cocktail_sort
  369. // thanks to Joseph Nahmias
  370. var b = 0;
  371. var t = list.length - 1;
  372. var swap = true;
  373.  
  374. while(swap) {
  375. swap = false;
  376. for(var i = b; i < t; ++i) {
  377. if ( comp_func(list[i], list[i+1]) > 0 ) {
  378. var q = list[i]; list[i] = list[i+1]; list[i+1] = q;
  379. swap = true;
  380. }
  381. } // for
  382. t--;
  383.  
  384. if (!swap) break;
  385.  
  386. for(var i = t; i > b; --i) {
  387. if ( comp_func(list[i], list[i-1]) < 0 ) {
  388. var q = list[i]; list[i] = list[i-1]; list[i-1] = q;
  389. swap = true;
  390. }
  391. } // for
  392. b++;
  393.  
  394. } // while(swap)
  395. }
  396. }
  397.  
  398. /* ******************************************************************
  399. Supporting functions: bundled here to avoid depending on a library
  400. ****************************************************************** */
  401.  
  402. // Dean Edwards/Matthias Miller/John Resig
  403.  
  404. // Denilson has disabled the code below. I'm manually calling sorttable.init instead.
  405. // // for Mozilla/Opera9
  406. // if (document.addEventListener) {
  407. // document.addEventListener("DOMContentLoaded", sorttable.init, false);
  408. // }
  409. //
  410. // // for Internet Explorer
  411. // // Removed
  412. //
  413. // // for Safari
  414. // if (/WebKit/i.test(navigator.userAgent)) { // sniff
  415. // var _timer = setInterval(function() {
  416. // if (/loaded|complete/.test(document.readyState)) {
  417. // sorttable.init(); // call the onload handler
  418. // }
  419. // }, 10);
  420. // }
  421. //
  422. // // for other browsers
  423. // //window.onload = sorttable.init;
  424.  
  425.  
  426. // written by Dean Edwards, 2005
  427. // with input from Tino Zijdel, Matthias Miller, Diego Perini
  428.  
  429. // http://dean.edwards.name/weblog/2005/10/add-event/
  430.  
  431. function dean_addEvent(element, type, handler) {
  432. if (element.addEventListener) {
  433. element.addEventListener(type, handler, false);
  434. } else {
  435. // assign each event handler a unique ID
  436. if (!handler.$$guid) handler.$$guid = dean_addEvent.guid++;
  437. // create a hash table of event types for the element
  438. if (!element.events) element.events = {};
  439. // create a hash table of event handlers for each element/event pair
  440. var handlers = element.events[type];
  441. if (!handlers) {
  442. handlers = element.events[type] = {};
  443. // store the existing event handler (if there is one)
  444. if (element["on" + type]) {
  445. handlers[0] = element["on" + type];
  446. }
  447. }
  448. // store the event handler in the hash table
  449. handlers[handler.$$guid] = handler;
  450. // assign a global event handler to do all the work
  451. element["on" + type] = handleEvent;
  452. }
  453. };
  454. // a counter used to create unique IDs
  455. dean_addEvent.guid = 1;
  456.  
  457. function removeEvent(element, type, handler) {
  458. if (element.removeEventListener) {
  459. element.removeEventListener(type, handler, false);
  460. } else {
  461. // delete the event handler from the hash table
  462. if (element.events && element.events[type]) {
  463. delete element.events[type][handler.$$guid];
  464. }
  465. }
  466. };
  467.  
  468. function handleEvent(event) {
  469. var returnValue = true;
  470. // grab the event object (IE uses a global event object)
  471. event = event || fixEvent(((this.ownerDocument || this.document || this).parentWindow || window).event);
  472. // get a reference to the hash table of event handlers
  473. var handlers = this.events[event.type];
  474. // execute each event handler
  475. for (var i in handlers) {
  476. this.$$handleEvent = handlers[i];
  477. if (this.$$handleEvent(event) === false) {
  478. returnValue = false;
  479. }
  480. }
  481. return returnValue;
  482. };
  483.  
  484. function fixEvent(event) {
  485. // add W3C standard event methods
  486. event.preventDefault = fixEvent.preventDefault;
  487. event.stopPropagation = fixEvent.stopPropagation;
  488. return event;
  489. };
  490. fixEvent.preventDefault = function() {
  491. this.returnValue = false;
  492. };
  493. fixEvent.stopPropagation = function() {
  494. this.cancelBubble = true;
  495. };
  496.  
  497. // Denilson has moved "Dean's forEach" to the top of the script
  498.  
  499. // End of code from sorttable.js
  500. //////////////////////////////////////////////////////////////////////
  501.  
  502.  
  503.  
  504.  
  505. (function() {
  506.  
  507. // Configuration.
  508.  
  509. // Colors:
  510. var table_head_bg = "#525d76";
  511. var table_head_fg = "white";
  512.  
  513. var table_row0_bg = "#d6d7e9"; // Old 1.0 color: #ffffff
  514. var table_row0_fg = "black";
  515. var table_row1_bg = "#eaebff"; // Old 1.0 color: #d6d7e9
  516. var table_row1_fg = "black";
  517.  
  518. var table_row0_hover_bg = "white";
  519. var table_row0_hover_fg = "black";
  520. var table_row1_hover_bg = "white";
  521. var table_row1_hover_fg = "black";
  522.  
  523. var table_border = "black";
  524. var td_border = "#AAA";
  525.  
  526. var pre_bg = "#eaebff";
  527. var pre_fg = "black";
  528. var pre_border = "black";
  529.  
  530. //
  531. // DO NOT EDIT BELOW THIS LINE!
  532. //
  533.  
  534. // Main code below.
  535. var head = document.getElementsByTagName('head')[0];
  536. if (head) {
  537. // Adding the style sheet
  538. var style = document.createElement('style');
  539. style.setAttribute('type', 'text/css');
  540. style.appendChild(document.createTextNode(
  541. 'html,body { background: white; color: black; padding: 0; margin: 0;}\n' +
  542. 'body { padding: 1em; }\n' +
  543. 'dt { margin-top: 1ex; font-weight: bold; }\n' +
  544. 'pre { border: 2px solid ' + pre_border + '; padding: 1ex; margin: 1em 0; background: ' + pre_bg + '; color: ' + pre_fg + '; }\n' +
  545. 'table { border: 2px solid ' + table_border + '; border-collapse: collapse; margin: 1em 0; }\n' +
  546. 'td,th { border: 1px solid ' + td_border + '; border-width: 0 1px; padding: 0.1em 0.5ex; white-space: nowrap; }\n' +
  547. 'th { background: ' + table_head_bg + '; color: ' + table_head_fg + '; text-align: center; font-weight: bold; }\n' +
  548. '#connectionsheadtable th { background: transparent; color: inherit; text-align: right; font-weight: bold; }\n' +
  549. '#scoreboardtable th { background: transparent; color: inherit; text-align: center; font-weight: bold; }\n' +
  550. '#scoreboardtable, #connectionsheadtable { float: left; margin: 0 1em 0 0;}\n' +
  551. 'hr { float: none; clear: both; }\n' +
  552. 'tr:nth-child(even) { background: ' + table_row0_bg + '; color: ' + table_row0_fg + '; }\n' +
  553. 'tr:nth-child(odd) { background: ' + table_row1_bg + '; color: ' + table_row1_fg + '; }\n' +
  554. 'tr:nth-child(even):hover { background: ' + table_row0_hover_bg + '; color: ' + table_row0_hover_fg + '; }\n' +
  555. 'tr:nth-child(odd):hover { background: ' + table_row1_hover_bg + '; color: ' + table_row1_hover_fg + '; }\n'
  556. ));
  557. head.appendChild(style);
  558.  
  559. // Finding and naming some tables (putting IDs).
  560. var pidtable = null;
  561. var connectionstable = null;
  562. var connectionsheadtable = null;
  563. Array.prototype.forEach.call(
  564. document.getElementsByTagName('table'),
  565. function(table) {
  566. var heading_text = table.getElementsByTagName('tr')[0].textContent;
  567. if (heading_text.indexOf('Connections') > -1) {
  568. pidtable = table;
  569. pidtable.setAttribute('id', 'pidtable');
  570. } else if (heading_text.indexOf('Request') > -1) {
  571. connectionstable = table;
  572. connectionstable.setAttribute('id', 'connectionstable');
  573. } else if (heading_text.indexOf('Child Server number') > -1) {
  574. connectionsheadtable = table;
  575. connectionsheadtable.setAttribute('id', 'connectionsheadtable');
  576. }
  577. }
  578. );
  579.  
  580. // Removing incorrect P element. (It is written <p />
  581. // at source, it was supposed to be a close-tag, I guess.)
  582. var temp = document.getElementsByTagName('p');
  583. if (temp.length==2) {
  584. temp = temp[1];
  585. while (temp.hasChildNodes()) {
  586. temp.parentNode.insertBefore(temp.childNodes[0], temp);
  587. }
  588. temp.parentNode.removeChild(temp);
  589. }
  590. temp = null;
  591.  
  592. // Removing an HR element between the two tables.
  593. if (connectionsheadtable) {
  594. var temp = connectionsheadtable.previousElementSibling;
  595. if (temp && temp.tagName.toLowerCase() == 'hr') {
  596. temp.parentNode.removeChild(temp);
  597. }
  598. }
  599.  
  600. function add_column_alignment_to_table(table, alignments) {
  601. if (!table) {
  602. return;
  603. }
  604. var firstchild = table.firstChild;
  605. alignments.forEach(function(align) {
  606. var colgroup=document.createElement('colgroup');
  607. colgroup.setAttribute('align', align);
  608. table.insertBefore(colgroup, firstchild);
  609. });
  610. }
  611.  
  612. // Trying to style each column separately.
  613. add_column_alignment_to_table(pidtable, [
  614. 'right', // PID
  615. 'right', // total
  616. 'center', // accepting
  617. 'right', // busy
  618. 'right', // idle
  619. 'right', // writing
  620. 'right', // keep-alive
  621. 'right' // closing
  622. ]);
  623. add_column_alignment_to_table(connectionstable, [
  624. 'left', // Srv
  625. 'right', // PID
  626. 'center', // Acc
  627. 'center', // M
  628. 'right', // CPU
  629. 'right', // SS
  630. 'right', // Req
  631. 'right', // Conn
  632. 'right', // Child
  633. 'right', // Slot
  634. 'right', // Client
  635. 'center', // VHost
  636. 'left' // Request
  637. ]);
  638.  
  639. function thead_tfoot_and_sortable(table) {
  640. if (!table) {
  641. return;
  642. }
  643.  
  644. // Creating/getting the THEAD and TFOOT.
  645. var firstchild = table.firstChild;
  646. var thead = table.getElementsByTagName('thead')[0];
  647. if (!thead) {
  648. thead = document.createElement('thead');
  649. table.insertBefore(thead, firstchild);
  650. }
  651. var tfoot = table.getElementsByTagName('tfoot')[0];
  652. if(!tfoot) {
  653. tfoot = document.createElement('tfoot');
  654. table.insertBefore(tfoot, firstchild);
  655. }
  656.  
  657. // Moving headers to THEAD, and copying headers to TFOOT.
  658. var first_tr = table.getElementsByTagName('tr')[0];
  659. var second_tr = table.getElementsByTagName('tr')[1];
  660. thead.appendChild(first_tr);
  661. tfoot.appendChild(first_tr.cloneNode(true));
  662.  
  663. if (second_tr.getElementsByTagName('th').length > 2) {
  664. thead.appendChild(second_tr);
  665. tfoot.appendChild(second_tr.cloneNode(true));
  666. }
  667.  
  668. // Making it sortable.
  669. table.setAttribute('class', 'sortable');
  670.  
  671. }
  672. thead_tfoot_and_sortable(pidtable);
  673. thead_tfoot_and_sortable(connectionstable);
  674.  
  675. // Transforming a paragraph in a table.
  676. if (connectionstable) {
  677. var scoreboardtable = null;
  678. var scoreboard = connectionstable.previousElementSibling;
  679. if (scoreboard && scoreboard.tagName.toLowerCase() == 'p') {
  680. var text = scoreboard.textContent;
  681.  
  682. // Removing header text "Scoreboard Key:".
  683. text = text.replace(/^[^:]+:/, '');
  684. // Removing newlines.
  685. text = text.replace(/[\r\n]/g, '');
  686.  
  687. var elements = text.split(',');
  688.  
  689. var scoreboardtable = document.createElement('table');
  690. var tbody = document.createElement('tbody');
  691. scoreboardtable.appendChild(tbody);
  692. scoreboardtable.setAttribute('id', 'scoreboardtable');
  693.  
  694. var regex = /"(.)" +(.*[^ ]) */
  695. elements.forEach(function(elem) {
  696. var tr = document.createElement('tr');
  697. var th = document.createElement('th');
  698. var td = document.createElement('td');
  699. var match = regex.exec(elem);
  700. if (match) {
  701. th.appendChild(document.createTextNode(match[1]));
  702. td.appendChild(document.createTextNode(match[2]));
  703. } else {
  704. td.appendChild(document.createTextNode(elem));
  705. }
  706. tr.appendChild(th);
  707. tr.appendChild(td);
  708. tbody.appendChild(tr);
  709. });
  710. scoreboard.parentNode.insertBefore(scoreboardtable, connectionsheadtable);
  711. scoreboard.parentNode.removeChild(scoreboard);
  712. }
  713. }
  714.  
  715. // Splitting <dt> into <dt> and <dd>.
  716. Array.prototype.forEach.call(
  717. document.getElementsByTagName('dl'),
  718. function(dl) {
  719. if (dl.getElementsByTagName('dd').length > 0) {
  720. return;
  721. }
  722. // Converting HTMLCollection to an Array, so that we can modify the elements without changing the array.
  723. var dts = Array.prototype.slice.call(dl.getElementsByTagName('dt'));
  724. dts.forEach(function(dt) {
  725. var match = /([^:]+): +(.*)/.exec(dt.textContent);
  726. if (match) {
  727. var dd = document.createElement('dd');
  728. dd.textContent = match[2];
  729. dt.textContent = match[1];
  730. dt.parentNode.insertBefore(dd, dt.nextSibling);
  731. } else {
  732. var dd = document.createElement('dd');
  733. dd.textContent = dt.textContent;
  734. dt.parentNode.insertBefore(dd, dt);
  735. dt.parentNode.removeChild(dt);
  736. }
  737. });
  738. }
  739. );
  740.  
  741. // Finally, calling sorttable.init()
  742. sorttable.init();
  743. }
  744. })();