User:Hmmmmike/monobook.js

/** * Generate a link in the toolbar and a portlet on the side, allowing to generate * a citation for the current revision of the wikipedia article. * * This script is based on the monobook.js example found at: *    http://en.wikibooks.org/wiki/User:Kellen/monobook.js * * Currently only BibTeX format is supported. * * by Mike Holenderski, August 2008 */

/** * makeLink * makes a link with the specified text and href * * param text: label text to give the link * param href: url for the link **/ function makeLink(text, href) { var link = document.createElement('A'); link.appendChild(document.createTextNode(text)); link.href = href; return link; } /** * makeNewToolsItem * makes a new item in the personal tools portlet with the specified id * * param id: id to give the new item * returns: a new item (li) on the personal tools list with specified id **/ function makeNewToolsItem(id) { var node = document.createElement('LI'); node.id = id; // add to list var preflist = document.getElementById('pt-userpage').parentNode; preflist.appendChild(node); return node; } /** * insertAfter * adds newElement to the DOM tree after the reference node * * param reference: the node to insert after * param newElement: the element to insert **/ function insertAfter(reference, newElement) { reference.parentNode.insertBefore(newElement,reference.nextSibling); } /** * makeNew Portlet * make a new sidebar portlet * * param id: id to give the containing div of the portlet * param label: visible label for the portlet, like "toolbox" * returns: an empty list (UL) to be filled with items (li's) **/ function makeNewPortlet(id, label) { var node = document.createElement('DIV'); node.id = id; node.className= 'portlet'; var title = document.createElement('H5'); title.appendChild(label); node.appendChild(title); var contentnode = document.createElement('DIV'); contentnode.className = 'pBody'; node.appendChild(contentnode); var listnode = document.createElement('UL'); contentnode.appendChild(listnode); /* by default inserts under the 'toolbox'; use a different id     in place of 'p-tb' to move where the portlet gets put */ insertAfter(document.getElementById('p-tb'), node); return listnode; } /** * addPortletItem * add an item to a portlet * * param portlet: a list (UL, usually) * param item: a node, most often a link (A), to add to the list */ function addPortletItem(portlet, item) { var listitem = document.createElement('LI'); listitem.appendChild(item); portlet.appendChild(listitem); } /** * Generate the link to the sever side script, which will return the citation. */ function citationLink { return 'http://www.stack.nl/~mike/tools/wikicitation/bibtex.php?title='+wgTitle+'&linkTitle='+wgPageName+'&revision='+wgCurRevisionId; } /* generates a bunch of citation-related interface bits */ function citation { if (!(wgIsArticle)) {return};

// make a citation item in the personal tools portlet citationnode = makeNewToolsItem('pt-citation'); // wrap "citation >" in a span so we can alter the style independently var citationlabel = document.createElement('SPAN'); citationlabel.id = 'pt-citation-label'; // add the links citationlabel.appendChild(document.createTextNode('Citation: ')); citationlabel.appendChild(makeLink('BibTeX', citationLink)); // append everything to the citation item citationnode.appendChild(citationlabel); // make citation portlet var citationtools = makeNewPortlet('p-citation', document.createTextNode('Citation')); // add a bunch of links to it addPortletItem(citationtools, makeLink('BibTeX', citationLink)); } /* run citation when a page is loaded */ if (window.addEventListener) window.addEventListener("load",citation,false); else if (window.attachEvent) window.attachEvent("onload",citation);