User:Firilacroco/modern.js

/* Pagină folosită pentru codul ce permite vizualizarea diacriticelor cu sedilă de către cei cu browsere vechi * și salvarea paginilor folosind diacriticele cu virgulă * Va fi introdusă o referință către ea în MediaWiki:Common.js * Autor ro:Utilizator:Strainu * Pe lângă licențele obligatorii pe Wikipedia, puteți considera codul ca fiind */ /** Partea 1: Folosirea diacriticelor corecte la salvarea și paginii, precum și la crearea de pagini noi*/ function all_commas(str) {  str=str.replace(/ţ/g,"ț"); str=str.replace(/Ţ/g,"Ț"); str=str.replace(/ş/g,"ș"); str=str.replace(/Ş/g,"Ș"); return str; } saveNewDiacritics = function { var fd_form=document.getElementById('editform'); var rename_form=document.getElementById('movepage'); var upload_form=document.getElementById('mw-upload-form'); /*excluding JS files for practical purposes*/ if(mw.config.get( 'wgTitle' ).indexOf(".js") > -1) return; if(mw.config.get( 'wgPageName' ) ==="Special:Căutare") {   var allLinkTags=document.getElementsByTagName("a"); for (i=0; i<allLinkTags.length; i++) { if (allLinkTags[i].className=="new") { allLinkTags[i].href=allLinkTags[i].href.replace(/ţ/g,"ț"); allLinkTags[i].href=allLinkTags[i].href.replace(/Ţ/g,"Ț"); allLinkTags[i].href=allLinkTags[i].href.replace(/ş/g,"ș"); allLinkTags[i].href=allLinkTags[i].href.replace(/Ş/g,"Ș"); allLinkTags[i].href=allLinkTags[i].href.replace(/%C5%A3/gi,"ț"); allLinkTags[i].href=allLinkTags[i].href.replace(/%C5%A2/gi,"Ț"); allLinkTags[i].href=allLinkTags[i].href.replace(/%C5%9F/gi,"ș"); allLinkTags[i].href=allLinkTags[i].href.replace(/%C5%9E/gi,"Ș"); }   }  }  if(mw.config.get( 'wgPageName' )==="Special:Mută_pagina" && rename_form) {   rename_form.onsubmit=function { var ta=document.getElementById('wpNewTitle'); var desc=document.getElementById('wpReason'); if(!ta || !desc){ return true; }     if(desc.value.search("{{titlu corect") > -1){//we found the template, do not replace return true; }     ta.value=all_commas(ta.value); //alert(ta.value); return true; } }  /*if (document.getElementsByName('createbox').length) {   var i=0; for(i=0; i < document.getElementsByName('createbox').length; i++){ document.getElementsByName('createbox')[i].onsubmit=function { var ta=document.getElementsByName('createbox')[i].getElementsByName('name')[0]; if(!ta){ return true; }       ta.value=all_commas(ta.value); }   }  }*/  if (mw.config.get( 'wgPageName' )=="Special:Încărcare" && upload_form) { upload_form.onsubmit=function{ var ta=document.getElementById('wpUploadDescription'); var title=document.getElementById('wpDestFile'); if (!ta || !title) { return true; }     var newvalue = ta.value; var newtitle = title.value; var turkish_regexp = /(<(span) lang=[^<>]*>(.|\r|\n)*?<\/\2>)|(<(gallery)(.*?)>(.|\r|\n)*?<\/\5>)/gi; var turkish_phrases = newvalue.match(turkish_regexp); newvalue=all_commas(newvalue); var mixed_phrases = newvalue.match(turkish_regexp); if(mixed_phrases != null && turkish_phrases != null) { for(var i = 0; i < turkish_phrases.length; i++) { newvalue = newvalue.replace(mixed_phrases[i], turkish_phrases[i]); }     }      //restore the value to the editbox ta.value = newvalue; if(ta.value.search("{{titlu corect") > -1){//we found the template, do not replace the title return true; }     title.value = all_commas(newtitle); return true; } }  if ((mw.config.get( 'wgAction' )=='edit' || mw.config.get( 'wgAction' )=='submit') && fd_form) { fd_form.onsubmit=function { var ta=document.getElementById('wpTextbox1'); if (!ta) { return true; }     var newvalue = ta.value; var turkish_regexp = /(<(span) lang=[^<>]*>(.|\r|\n)*?<\/\2>)|(<(gallery)(.*?)>(.|\r|\n)*?<\/\5>)/gi; var turkish_phrases = newvalue.match(turkish_regexp); var interwiki_regexp = /\[\[:?([a-z]{2,3}|fișier|imagine|media|simple|roa-rup|be-x-old|zh-(yue|classical|min-nan)|bat-smg|cbk-zam|nds-nl|map-bms|fişier|file|image):(.*?)\]\]/gi; var interwiki_phrases = newvalue.match(interwiki_regexp); var template_regexp = /\{\{(proiecte surori|sisterlinks|commons|commonscat|wikicitat|wikimanuale|wikisursă|wikitravel|wikiştiri|wikţionar|WikimediaPentruPortale)\|(.|\r|\n)*?\}\}/gi; //var template_regexp = /\{\{((.|\r|\n)*)\}\}/gi; var template_phrases = newvalue.match(template_regexp); newvalue=all_commas(newvalue); var mixed_phrases = newvalue.match(turkish_regexp); if(mixed_phrases != null && turkish_phrases != null) { for(var i = 0; i < turkish_phrases.length; i++) { newvalue = newvalue.replace(mixed_phrases[i], turkish_phrases[i]); }     }      mixed_phrases = newvalue.match(interwiki_regexp); if(mixed_phrases != null && interwiki_phrases != null) { for(var i = 0; i < interwiki_phrases.length; i++) { newvalue = newvalue.replace(mixed_phrases[i], interwiki_phrases[i]); }     }      mixed_phrases = newvalue.match(template_regexp); if(mixed_phrases != null && template_phrases != null) { for(var i = 0; i < template_phrases.length; i++) { newvalue = newvalue.replace(mixed_phrases[i], template_phrases[i]); }     }      var modified = 0; if(ta.value != newvalue) modified = 1; //restore the value to the editbox ta.value = newvalue; // mind the scope! var form=document.getElementById('editform'); var es=document.getElementById('wpSummary'); if (form.elements['wpSection'].value=='new') { es.value=all_commas(es.value); } else if (es && modified && (es.value.search("WP:DVN")==-1)) { es.value+=' (corectat automat)'; }     return true; } } } /** Partea 2: Înlocuirea virgulelor cu sedile pentru utilizatorii care nu le văd ok */ function all_cedillas(str) {  str=str.replace(/ț/g,"ţ"); str=str.replace(/Ț/g,"Ţ"); str=str.replace(/ș/g,"ş"); str=str.replace(/Ș/g,"Ş"); return str; } function goodToBad(node) { var i; if (goodToBad.formInputs == undefined) { goodToBad.formInputs = document.getElementsByTagName('form'); } /* skip the form elements */ for (i = 0; i < goodToBad.formInputs.length; i++) { if(node == goodToBad.formInputs[i]) return; } if(node.nodeName == '#text') { node.nodeValue = all_cedillas(node.nodeValue); return; } // skip Romanian stuff if (node.getAttribute != undefined && node.getAttribute('lang') == 'ro') { return; } for(i = 0; i < node.childNodes.length; i++) goodToBad(node.childNodes[i]); } function loadOldDiacritics { if(mw.config.get( 'wgPageName' ).toLowerCase.search("diacritic") > -1) {   return;//do not modify the text for pages referring to diacritics } var ta=document.getElementById('wpTextbox1'); if (!ta) { return true; } var orig = ta.value; ta.value = all_cedillas(ta.value); // mind the scope! var form = document.getElementById('editform'); var es = document.getElementById('wpSummary'); if (form.elements['wpSection'].value == 'new') { es.value = all_cedillas(es.value); } return true; } var showOldDiacritics = function { /*excluding JS files for practical purposes*/ if(mw.config.get( 'wgTitle' ).indexOf(".js") > -1) return; var titleOverride=document.getElementById("full_title"); if (titleOverride!=undefined) { var DOMtitle=document.getElementById("firstHeading"); if (DOMtitle!=undefined) { DOMtitle.innerHTML=titleOverride.innerHTML; } }  //edit box if (mw.config.get( 'wgAction' )=='edit' || mw.config.get( 'wgAction' )=='submit') { //loadOldDiacritics; } //body goodToBad(document.body); //title document.title = all_cedillas(document.title); } // Copyright (c) 2010 Cristian Adam  // Adapted for Wikipedia by User:Strainu // License: MIT function diacriticsConfigureTextElement(element, text) { element.innerHTML = text; element.style.width = "auto"; element.style.visibility = "hidden"; element.style.position = "absolute"; element.style.fontSize = "96px"; } // http://stackoverflow.com/questions/1955048 function diacriticsGetStyle(element, property) { var camelize = function (str) { return str.replace(/\-(\w)/g, function(str, letter){           return letter.toUpperCase;        }); };   if (element.currentStyle) { return element.currentStyle[camelize(property)]; } else if (document.defaultView && document.defaultView.getComputedStyle) { return document.defaultView.getComputedStyle(element,null) .getPropertyValue(property); } else { return element.style[camelize(property)]; } } function diacriticsOnOlderOperatingSystems { var userAgent = navigator.userAgent.toLowerCase; if (userAgent.indexOf("bot") != -1 ||       userAgent.indexOf("crawl") != -1 ||        userAgent.indexOf("slurp") != -1 ||        userAgent.indexOf("archive") != -1) {       return false; }   var normalText = document.createElement("div"); diacriticsConfigureTextElement(normalText, "sStT"); var diacriticsText = document.createElement("div"); diacriticsConfigureTextElement(diacriticsText, "șȘțȚ"); document.body.insertBefore(normalText, document.body.firstChild); document.body.insertBefore(diacriticsText, document.body.firstChild); // Sometimes at various zoom settings there is a +1 difference var doChange = (Math.abs(normalText.offsetWidth - diacriticsText.offsetWidth) > 1); // Pocket Internet Explorer on Windows Mobile 6.5 returns 0 if (normalText.offsetWidth == 0 &&       diacriticsText.offsetWidth == 0) {       doChange = true; } /*   alert("doChange: " + doChange + "\n" +           normalText.offsetWidth + " vs " + diacriticsText.offsetWidth + "\n" +          "fontFamily: " + diacriticsGetStyle(diacriticsText, "font-family")); document.body.removeChild(normalText); document.body.removeChild(diacriticsText); if(doChange) { showOldDiacritics; } } /**Partea 3: Folosirea funcțiilor de mai sus */ addOnloadHook(saveNewDiacritics); addOnloadHook(diacriticsOnOlderOperatingSystems);