User:Austin J. Che/Extensions/history.js: Difference between revisions
From OpenWetWare
Jump to navigationJump to search
No edit summary |
m (User:Austin/Extensions/history.js moved to User:Austin J. Che/Extensions/history.js: Automatically moved page while renaming the user "Austin" to "Austin J. Che") |
(No difference)
|
Revision as of 15:49, 14 May 2007
// <pre> // At http://openwetware.org/wiki/User:Austin/Extensions/history.js // Author: Austin Che // // This only works with MediaWiki > 1.8 (which defines wgPageName as Javascript variable) // Add the following line to User:YOURNAME/monobook.js // document.write('<script type="text/javascript" src="http://openwetware.org/index.php?title=User:Austin/Extensions/history.js&action=raw&ctype=text/javascript"></script>'); if (document.getElementById && wgPageName && addOnloadHook) addOnloadHook(initVisitHistory); var historyNumSaved = 20; var historyExpiration = 30; // days var historyCookieName = "wikihist"; var historyList = new Array(); function setCookie(name, value, days) { var expires = ""; if (days) { var date = new Date(); // current time date.setTime(date.getTime() + (days*24*60*60*1000)); expires = "; expires=" + date.toGMTString(); } document.cookie = name + "=" + escape(value) + expires + "; path=/"; } function getCookie(name) { // returns the value of the cookie with name if (! document.cookie.length) return null; // search for cookie var search = name + "="; var offset = document.cookie.indexOf(search); if (offset == -1) return null; // no such cookie // set offset to the beginning of the cookie value offset += search.length; var end = document.cookie.indexOf(";", offset); // find end if (end == -1) end = document.cookie.length; return unescape(document.cookie.substring(offset, end)); } function loadSaveHistory() { var cookie = getCookie(historyCookieName); // using | as separator as it is not valid in wiki page name if (cookie) historyList = cookie.split('|'); for (var i = 0; i < historyList.length; i++) { if (historyList[i] == wgPageName) { historyList.splice(i, 1); break; } } // add current page to beginning of history list if (historyList.length == 0 || historyList[0] != wgPageName) historyList.unshift(wgPageName); if (historyList.length > historyNumSaved) historyList = historyList.slice(0, historyNumSaved); setCookie(historyCookieName, historyList.join('|'), historyExpiration); } function jumpToHistoryPage() { var hist = document.getElementById('visithistory'); var page = hist.options[hist.selectedIndex].text; var s = new String(wgArticlePath); s = s.replace(/\$1/, page); var url = wgServer + encodeURI(s.toString()); window.location = url; } function addOption(text) { var o = document.createElement('option'); o.appendChild(document.createTextNode(text)); return o; } function showHistory() { if (!historyList || !historyList.length) return; var hist = document.createElement('select'); hist.id = 'visithistory'; // without removing text-transform, page names are lowercased hist.style.textTransform = "none"; hist.style.width = "25%"; hist.onchange = "jumpToHistoryPage()"; var o = addOption(historyList[0]); o.selected = true; hist.appendChild(o); for (var i = 1; i < historyList.length; i++) { hist.appendChild(addOption(historyList[i])); } var f = document.createElement('form'); f.appendChild(hist); var li = document.createElement('li'); li.appendChild(f); var personal = document.getElementById('p-personal').getElementsByTagName('ul')[0]; personal.appendChild(li); } function initVisitHistory() { loadSaveHistory(); showHistory(); } // </pre>