// // Redirect to a Random Book // maintained by [[User:Darklama|darklama]] // // Gets a page from the wiki and runs function action on it function GetPage(page, action) { var doc = new XMLHttpRequest(); if (doc) { var url = wgArticlePath.replace(/\$1$/, page); doc.onreadystatechange = function() { action(doc); }; doc.open('GET', url+'?printable=true', true); if (doc.overrideMimeType) doc.overrideMimeType('text/xml'); else doc.setRequestHeader("Content-Type", "text/xml; charset=UTF-8"); doc.send(null); } } // Get a random page from Wikibooks:Alphabetical_classification function RandomBook() { function random_book(doc) { try { if (doc.readyState == 4 && doc.status == 200) { var wiki = doc.responseXML; if (wiki) { var books = wiki.getElementById('books').getElementsByTagName('li'); if (books && books.length > 0) { var page = books.item(Math.floor(Math.random() * books.length + 1)).firstChild; while (hasClass(page, "new") || hasClass(page, "external")) { page = books.item(Math.floor(Math.random() * books.length + 1)).firstChild; } window.location = page.href; } } } } catch (e) { } } GetPage("Wikibooks:Alphabetical_classification", random_book); } // Adds a random book link to the navigation toolbar and calls RandomBook when clicked function RandomBookLink() { var tb = document.getElementById('p-Navigation'); var link = document.createElement('a'); var li = document.createElement('li'); if (tb) { tb = tb.getElementsByTagName('ul')[0]; link.title = "Random Book"; link.href = "javascript:RandomBook();"; link.onmouseover = function() { window.status = "Random Book"; return true; } link.onmouseout = function() { window.status = ""; return true; } if (link.addEventListener) { link.addEventListener("click", RandomBook, false); } else if (link.attachEvent) { link.attachEvent("onclick", function() { RandomBook(); }); } else if (link.onclick) { link.onclick = function() { RandomBook(); } } link.appendChild(document.createTextNode("Random book")); li.id = "n-random-book"; li.appendChild(link); tb.appendChild(li); } } addOnloadHook( RandomBookLink );