User:Conrad.Irwin/iwiki.js

/*jshint undef:true, strict:true */ /*global mw, jQuery, languageContainers, tabbedLanguages, langname2code, langcode2name, langcode2FLname */

function add_prominent_interwikis{ //Can be caused by delay in timing if(window.langcode2name === void(0)){ if(! document.callbacks_langcode ) document.callbacks_langcode = {}; document.callbacks_langcode['iwiki'] = add_prominent_interwikis; return false; } if(!window.languageContainers){ var heads = document.getElementsByTagName('h2');

for(var i=0;i<heads.length;i++){ var code = search_for_text(heads[i],langname2code); if(code && check_interwiki(code)) create_link(heads[i],code); } }  else{ for(var i=0;i<languageContainers.length;i++){ var code = langname2code(tabbedLanguages[i]); if(code && check_interwiki(code)) create_link(languageContainers[i].firstChild,code); } }  function search_for_text(node,test){ if(node.nodeType == 3){ return test(node.nodeValue); }else if(node.childNodes){ for(var i in node.childNodes){ var res = search_for_text(node.childNodes[i],test); if(res) return res; }   }    return false; } var iwikis = false; function check_interwiki(code){ if(! iwikis){ iwikis = {}; var plang = document.getElementById('p-lang'); if(!plang) return false; var iwls = plang.getElementsByTagName('li'); for(var i=0;i<iwls.length;i++){ var guess = iwls[i].className.replace(/^interwiki-(.*)$/,"$1"); iwikis[guess] = true; }    }    return iwikis[code]; } function create_link(head,code){ var div = document.createElement('div'); var lnk = document.createElement('a'); var word = mw.config.get( "wgTitle" ); var langname = langcode2name(code); div.style.cssText = 'font-size: 90%; padding-top: 0px;margin-left:15px; '+(window.languageContainers?'':'margin-top: -10px;'); lnk.className = 'extiw'; lnk.href = mw.config.get( "wgArticlePath" ).replace('$1',code+':'+word); lnk.setAttribute('title',''+langcode2FLname(code)+': '+word); lnk.appendChild(document.createTextNode(mw.config.get( "wgTitle" ))); div.appendChild(lnk); div.appendChild(document.createTextNode(' is in the '+langname+' Wiktionary'));

head.parentNode.insertBefore(div,head.nextSibling); } }

if (!mw.config.get('wgNamespaceNumber')) jQuery(document).ready(add_prominent_interwikis);