MediaWiki:Common.js: Forskelle mellem versioner

Fra AarhusWiki
mNo edit summary
Tag: Tilbagerullet
mNo edit summary
Tag: Manuel tilbagerulning
Linje 97: Linje 97:
$(function() {
$(function() {
   addRandomArticleLink();
   addRandomArticleLink();
});
// Add "Lykke" link to the navigation menu
$(function() {
  if ( mw.config.get('wgPageName') === 'AarhusWiki' ) {
    var $nav = $('#p-navigation ul');
    var $link = $('<a>', {
      href: mw.util.wikiGetlink('Special:RandomInCategory/Lykke'),
      text: 'Lykke'
    });
    var $li = $('<li>').append($link);
    $nav.append($li);
  }
});
});

Versionen fra 13. apr. 2023, 13:05

/* Javascript inkluderet her vil være aktivt for alle brugere. */

// Adds a default template to new pages
$(function() {
  if (mw.config.get('wgAction') !== 'edit' || mw.config.get('wgNamespaceNumber') !== 0 || mw.config.get('wgArticleId') !== 0) {
    return;
  }
  var template = "\n\n== Litteratur og kilder ==\n* \n\n[[Kategori:]]";
  var content = $('#wpTextbox1').val();
  if (!content) {
    $('#wpTextbox1').val(template);
  } else if (content.indexOf('== Litteratur og kilder ==') === -1) {
    $('#wpTextbox1').val(content + '\n\n' + template);
  }
});

$(document).ready(function() {
  $('head').append('<script defer data-domain="aarhuswiki.dk" src="https://plausible.io/js/plausible.js"></script>');

  // Shorten text to keep background
  $('#p-banner').text('Aarhuswiki');

  $('#searchInput').on('focus', function() {
    $('body .suggestions').prependTo('#container');
  });

  /*
  if (window.location.href.indexOf("Louise_Lindskrog_(1992-)") > 0) {
    document.getElementById("ca-history").style.display = "none";
  }
  */

  /* Erstat link til Mediawiki, med link til vores hjemmeside */
  $('#f-poweredbyico a').attr('href', 'http://www.aarhusstadsarkiv.dk');

$("#mw-site-navigation").prepend(
  '<div id="site-themes" class="sidebar-chunk"><div role="navigation" class="mw-portlet" id="p-tbt" aria-labelledby="p-tbt-label"><div class="mw-portlet-body"><img style="width:100%;" alt="Aarhus Stadsarkiv" src="/mediawiki/images/2/29/As_logo.png"/></div></div></div>'
);

  $("#mw-site-navigation").append(
      '<div id="site-themes" class="sidebar-chunk"><div role="navigation" class="mw-portlet" id="p-tbt" aria-labelledby="p-tbt-label"><h3 id="p-tbt-label" dir="ltr" lang="da" style="padding-bottom:0;margin-bottom:0;">Særlige temaer</h3><br><div class="mw-portlet-body"><a class="image" href="https://aarhuswiki.dk/wiki/Kongelunden"><img style="width:100%;margin-bottom:.7em;" alt="Kongelunden" src="/mediawiki/images/7/73/Kongelunden_logo_groen_01_150px.jpg"/></a><br><hr><p><div class="mw-portlet-body"><a class="image" href="https://aarhuswiki.dk/wiki/Teatre_i_Aarhus"><img style="width:100%;margin-bottom:.7em;" alt="Teatre i Aarhus" src="/mediawiki/images/0/0d/Teatre_i_Aarhus.png"/></a></div></div></div></div>'
  );

  /* Add link to accessability-declaration in footer */
  $("#f-list").append( '<li id="accessability-declaration"><a class="noprint stopMobileRedirectToggle" href="https://www.was.digst.dk/aarhuswiki-dk">Webtilgængelighedserklæring</a></li>' );
});

// Function to generate a random integer between 0 and max
function getRandomInt(max) {
  return Math.floor(Math.random() * Math.floor(max));
}

// Function to retrieve the page ID of a random page
function getRandomPageId() {
  var apiUrl = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php';
  var params = {
    action: 'query',
    generator: 'random',
    format: 'json',
    prop: 'info',
    inprop: 'url',
    formatversion: 2,
    indexpageids: true,
    redirects: true
  };
  return new Promise(function(resolve, reject) {
    $.get(apiUrl, params).done(function(data) {
      var pageIds = data.query.pageids;
      if (pageIds.length > 0) {
        var randomIndex = getRandomInt(pageIds.length);
        var pageId = pageIds[randomIndex];
        resolve(pageId);
      } else {
        reject('No pages found.');
      }
    }).fail(function() {
      reject('Failed to retrieve random page ID.');
    });
  });
}

// Function to add a "Random Article" link to the navigation panel
function addRandomArticleLink() {
  var navPanel = $('#p-navigation ul');
  if (navPanel.length > 0) {
    getRandomPageId().then(function(pageId) {
      var linkUrl = mw.config.get('wgServer') + mw.config.get('wgScript') + '?title=' + encodeURIComponent(mw.config.get('wgPageName')) + '&action=edit&section=0&random=' + pageId;
      var linkText = 'Tilfældig side';
      navPanel.append('<li><a href="' + linkUrl + '">' + linkText + '</a></li>');
    }).catch(function(errorMessage) {
      console.error('Error adding "Random Article" link: ' + errorMessage);
    });
  }
}

// Add the "Random Article" link to the navigation panel when the document is ready
$(function() {
  addRandomArticleLink();
});