User:Iritscen/vector-2022.js: Difference between revisions

moved to Common.js
mNo edit summary
(moved to Common.js)
 
(5 intermediate revisions by the same user not shown)
Line 1: Line 1:
function getLang()
{
  if (navigator.languages !== undefined)
    return navigator.languages[0];
  return navigator.language;
}
function changeDateSeparator(component, separator)
{
  const styleSheets = document.styleSheets;
  var ruleIndex;
  console.log('Setting component "'+component+'" to use separator "'+separator+'".');
  for (var i = 0; i < styleSheets.length; i++)
  {
    const rules = styleSheets[i].cssRules || styleSheets[i].rules;
    console.log('Looking for date rule ".date-'+component+'::before" in these rules:');
    console.log(rules);
    for (var j = 0; j < rules.length; j++)
    {
      if ((component == 'day' && rules[j].selectorText === '.mw-parser-output .date-day::before') || (component == 'month' && rules[j].selectorText === '.mw-parser-output .date-month::before'))
      {
        console.log('Found desired date rule.');
        ruleIndex = j;
        break;
      }
    }
    if (ruleIndex !== undefined)
    {
      // Update the content of the existing rule
      styleSheets[i].deleteRule(ruleIndex);
      styleSheets[i].insertRule('.date-'+component+'::before{content:"'+separator+'";}', ruleIndex);
      break;
    }
  }
}
function localizeDates()
{
  const lang = getLang();
  const root = document.querySelector(':root');
  if (root == undefined || lang == undefined)
    return;
  if (lang != "en-US") // LocaleDate prints dates in U.S.-style by default
  {
    root.style.setProperty('--month-order', '2');
    root.style.setProperty('--day-order', '1');
    changeDateSeparator('month', '/');
    changeDateSeparator('day', '');
  }
}
$(localizeDates);
mw.hook('wikiEditor.toolbarReady').add(function($textarea)
mw.hook('wikiEditor.toolbarReady').add(function($textarea)
{
{