User:Vaikoovery/modern.js

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Note: After saving, you have to bypass your browser's cache to see the changes. Internet Explorer: press Ctrl-F5, Mozilla: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Opera/Konqueror: press F5, Safari: hold down Shift + Alt while clicking Reload, Chrome: hold down Shift while clicking Reload.
importScript('User:Magog the Ogre/cleanup.js');
if(wgAction == "edit" && wgCanonicalNamespace == "File")
{
      var wpTextBox1 = $('#wpTextbox1').val();
      wpTextBox1 = wpTextBox1.replace(/==\s*Summary\s*==/g, '== {{int:filedesc}} ==');
      wpTextBox1 = wpTextBox1.replace(/==\s*Licensing\s*==/g, '== {{int:license-header}} ==');
      wpTextBox1 = wpTextBox1.replace(/==\s*{{int:license}}\s*==/g, '== {{int:license-header}} ==');
      wpTextBox1 = wpTextBox1.replace(/==\s*License information\s*==/g, '== {{int:license-header}} ==');
 
      if(wpTextBox1.indexOf("== {{int:filedesc}} ==") < 0 
            && wpTextBox1.indexOf("=={{int:filedesc}}==") < 0 
            && wpTextBox1.indexOf("{Information") > 0) 
                  wpTextBox1 = "== {{int:filedesc}} ==\n" + wpTextBox1;
 
      if(document.getElementById("wpTextbox1").value != wpTextBox1)
            $('#wpTextbox1').val($.trim(wpTextBox1));
 
      FixLanguageLinks('w', 'w', 'User');
      FixLanguageLinks('en', 'w', 'User');
      FixLanguageLinks('ml', 'വി', 'ഉപയോക്താവ്');
}
 
function FixLanguageLinks(lang, langLink, userNS)
{
  var currentText = $('#wpTextbox1').val();
  // Finds all strings that match [[:lang:Blah Blah|Blah]] excepts User and Image links.
  var pattern = '\\[\\[:' + lang + ':((?!(\\[\\[|(User)|(Image)|(File)|(Category))).)*\\]\\]'; 
  var regex = new RegExp(pattern, 'gi');
  var matchedValues = currentText.match(regex);
  if( matchedValues != null) 
  {
    matchedValues.forEach(function(val){
      var insideVal = val.substring(6, val.length-2);
      var valArray = insideVal.split('|');
      var newVal;
 
      var userRegex = new RegExp(userNS, 'gi');
      if(valArray[0].match(userRegex)) return true;
 
      if(valArray.length == 1)
        newVal = '{{' + langLink + '|' + valArray[0] + '}}';
      else if (valArray.length==2)
      {
        if(valArray[0] == valArray[1])
          newVal = '{{' + langLink + '|' + valArray[0] + '}}';
        else
          newVal = '{{' + langLink + '|' + valArray[0] + '|' + valArray[1] + '}}';
      }
      currentText = currentText.replace(val, newVal);
    });
    $('#wpTextbox1').val(currentText);
  }
}