User:Diberri/Template filler/pubmed.wtf.autolink.user.js

From Wikipedia, the free encyclopedia
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// ==UserScript==
// @name        Highlight PubMed IDs for Wikipedia Template Filler
// @namespace   http://diberri.dyndns.org/
// @description Links PMIDs found on PubMed pages to the Wikipedia Template Filler to generate a {{cite journal}} template.
// @include     http://www.ncbi.nlm.nih.gov*/sites/entrez*
// @include     http://www.ncbi.nlm.nih.gov*/pubmed/*
// ==/UserScript==

var pmid_conts = document.evaluate( "//div[@class='PMid'] | //p[@class='pmid'] | //span[@class='rprtid'] | //dd[@class='title']", document, null, XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE, null );

for( var i = 0; i < pmid_conts.snapshotLength; i++ ) {
  var cont = pmid_conts.snapshotItem(i);

  for( var j = 0; j < cont.childNodes.length; j++ ) {
    var child = cont.childNodes[j];
    var text_str = child.nodeValue;

    if( text_str && text_str.match( /PMID\:\s+(\d+)/ ) ) {
      var pmid = RegExp.lastParen;
      var wtf_url = "http://toolserver.org/~diberri/cgi-bin/templatefiller/index.cgi?ddb=&type=pubmed_id&id="+pmid+"&add_ref_tag=1&add_text_url=1&full_journal_title=1";

      var new_cont = document.createElement( cont.nodeName );
          new_cont.innerHTML = "<span class=\"wtflink\" style=\"font-size:10pt\">Fill template: <a style=\"font-weight:bold\" href=\""+wtf_url+"\">{{cite journal}}</a></span>";

      cont.parentNode.appendChild( new_cont );
    }
  }
}