MediaWiki:Gadget-RecentChangesBox.js

Note : après avoir enregistré la page, vous devrez forcer le rechargement complet du cache de votre navigateur pour voir les changements.

Mozilla / Firefox / Konqueror / Safari : maintenez la touche Majuscule (Shift) en cliquant sur le bouton Actualiser (Reload) ou pressez Maj-Ctrl-R (Cmd-R sur Apple Mac) ;

Firefox (sur GNU/Linux) / Chrome / Internet Explorer / Opera : maintenez la touche Ctrl en cliquant sur le bouton Actualiser ou pressez Ctrl-F5.
/* New changes box */

mw.loader.using(['mediawiki.util', 'mediawiki.api', 'mediawiki.cookie', 'user'], function () {
  var isVectorSkin = mw.config.get('skin') === 'vector' || mw.config.get('skin') === 'vector-2022';
  // allow user settings through
  var rcp_enabled = window.rcp_enabled;
  var rcp_num_pages = window.rcp_num_pages;
  var rcp_refresh = window.rcp_refresh;

  if (rcp_enabled === undefined) {
    rcp_enabled = false;
  }
  if (rcp_num_pages === undefined) {
    rcp_num_pages = 10;
  }
  if (rcp_refresh === undefined) {
    rcp_refresh = 10;
  }

  // a few limits to be nice to the servers
  if (rcp_num_pages > 50) {
    rcp_num_pages = 50;
  }
  if (rcp_num_pages < 1) {
    rcp_num_pages = 1;
  }
  if (rcp_refresh < 2) {
    rcp_refresh = 2;
  }

  $( rcp_init );

  /* initalise */
  function rcp_init() {
    if( !document.getElementById('p-logo') ) return;

    // get our cookie
    if (mw.cookie.get('rcp_show_box') === 'yes') {
      rcp_enabled = true;
    } else {
      rcp_enabled = false;
    }

    // Either make a request or show nothing
    if (rcp_enabled == true) {
      rcp_ajax_request();
    } else {
      rcp_draw_disabled_box();
    }
  }

  /* make a request */
  function rcp_ajax_request() {
    // check we are enabled
    if (rcp_enabled == false) return;

    // firstly, inform the user
    var cur_box = document.getElementById('p-recentchanges');
    if (cur_box != null) {
      cur_box.firstChild.firstChild.data = 'Recent Changes (updating)';
    }

    new mw.Api()
      .get({
        action: 'query',
        list: 'recentchanges',
        rcnamespace: 0,
        rcshow: '!bot',
        rcprop: 'title',
        rclimit: rcp_num_pages,
      })
      .done(rcp_ajax_response);
  }

  /* we have received a response */
  function rcp_ajax_response(data) {

    var items = data.query.recentchanges;

    // create the div that holds all the recentchanges links
    var link_div = document.createElement('div');
    link_div.className = 'body pBody';
    var list = document.createElement('ul');
    link_div.appendChild(list);

    // populate the list with 10 links.
    for (var i = 0; i < items.length; i++) {
      var item_name = items[i].title;
      var item_url = mw.util.getUrl(item_name, {diff: 'cur', oldid: 'prev'});

      var a = document.createElement('a');
      a.setAttribute('href', item_url);
      a.appendChild(document.createTextNode(item_name));

      var li = document.createElement('li');
      li.appendChild(a);
      list.appendChild(li);
    }

    // Container div
    var div = document.createElement('div');
    div.setAttribute('id', 'p-recentchanges');
    div.className = 'portal portlet';
    div.setAttribute('role', 'navigation');
    var heading = document.createElement('h3');
    if( isVectorSkin ) heading.setAttribute('style', 'display: block');
    heading.appendChild(document.createTextNode('Modifications récentes'));
    div.appendChild(heading);
    div.appendChild(link_div);

    // disable link
    var p = document.createElement('p');
    p.style.fontSize = 'x-small';
    p.style.margin = '0px';
    p.style.textAlign = 'right';
    var a = document.createElement('a');
    a.setAttribute('style', 'cursor: pointer');
    a.appendChild(document.createTextNode('désactiver cette boite'));
    a.onclick = rcp_disable_box;
    p.appendChild(a);
    link_div.appendChild(p);

    // now replace the div
    var old_div = document.getElementById('p-recentchanges');
    if (old_div != null) {
      old_div.parentNode.replaceChild(div, old_div);
    } else {
      var node = document.getElementById('p-logo');
      node.parentNode.insertBefore(div, node.nextSibling);
    }

    // and do it again in some seconds
    setTimeout(rcp_ajax_request, rcp_refresh * 1000);
  }

  function rcp_disable_box() {
    rcp_enabled = false;
    rcp_draw_disabled_box();
    mw.cookie.set('rcp_show_box', 'no', 86400 * 365);
  }

  function rcp_enable_box() {
    rcp_enabled = true;
    rcp_ajax_request();
    mw.cookie.set('rcp_show_box', 'yes', 86400 * 365);
  }

  function rcp_draw_disabled_box() {
    // Container div
    var link_div = document.createElement('div');
    link_div.className = 'body pBody';
    var div = document.createElement('div');
    div.setAttribute('id', 'p-recentchanges');
    div.className = 'portal portlet';
    div.setAttribute('role', 'navigation');
    var heading = document.createElement('h3');
    if( isVectorSkin ) heading.setAttribute('style', 'display: block');
    heading.appendChild(document.createTextNode('Modifications récentes'));
    div.appendChild(heading);
    div.appendChild(link_div);

    // enable link
    var p = document.createElement('p');
    p.style.fontSize = 'x-small';
    p.style.margin = '0px';
    var a = document.createElement('a');
    a.setAttribute('style', 'cursor: pointer');
    a.appendChild(document.createTextNode('Activer cette boite'));
    a.onclick = rcp_enable_box;
    p.appendChild(a);
    link_div.appendChild(p);

    // now replace the div
    var old_div = document.getElementById('p-recentchanges');
    if (old_div != null) {
      old_div.parentNode.replaceChild(div, old_div);
    } else {
      var node = document.getElementById('p-logo');
      node.parentNode.insertBefore(div, node.nextSibling);
    }
  }

});