User:Diberri/pubmed.js

// Mostly copied from User:Pilaf/livepreview.js function PubMedCiteInstall { try { netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead"); } catch(e) { alert("Could not install the PubMed citation formatter because additional privileges needed (UniversalBrowserRead) were denied."); return; }

copywarn = document.getElementById('editpage-copywarn'); if( copywarn != null ) { var pmcHtml = '  '; copywarn.innerHTML = pmcHtml + copywarn.innerHTML; } }

var PubMedCiteConverterURL = 'http://diberri.dyndns.org/pubmed.html?type=xml&amp;pmid='; var PubMedCiteReq = null;

function PubMedCiteAddCitation { var pmid = PubMedCiteGetSelection; if( !pmid || pmid == '' ) { pmid = prompt( 'Enter PubMed ID:' ); }

if( !pmid || pmid == '' ) { alert('No PubMed ID entered.'); return; }

var url = PubMedCiteConverterURL + pmid; PubMedCiteReq = new XMLHttpRequest; PubMedCiteReq.onreadystatechange = PubMedCiteReceiveCitation; PubMedCiteReq.open( 'GET', url, true ); PubMedCiteReq.send(null); }

function PubMedCiteReceiveCitation { if( PubMedCiteReq.readyState != 4 && PubMedCiteReq .status == 200 ) return; var el = document.getElementById('wpTextbox1'); var cite = PubMedCiteReq.responseXML.getElementsByTagName('citation-text')[0].firstChild.nodeValue; PubMedCiteInsertAtCursor( el, cite ); }

// http://www.alexking.org/blog/2003/06/02/inserting-at-the-cursor-using-javascript/ function PubMedCiteInsertAtCursor(myField, myValue) { //IE support if (document.selection) { myField.focus; sel = document.selection.createRange; sel.text = myValue; } //MOZILLA/NETSCAPE support else if (myField.selectionStart || myField.selectionStart == '0') { var startPos = myField.selectionStart; var endPos = myField.selectionEnd; myField.value = myField.value.substring(0, startPos) + myValue + myField.value.substring(endPos, myField.value.length); } else { myField.value += myValue; } }

// http://www.quirksmode.org/js/selected.html function PubMedCiteGetSelection { if(window.getSelection) { return window.getSelection; } else if(document.getSelection) { return document.getSelection; } else if(document.selection) { return document.selection.createRange.text; } }