MediaWiki:Gadget-tag-status-change.js

De La Coppermind
Ir a la navegación Ir a la búsqueda

Nota: tras guardar, quizás necesites actualizar la caché de tu navegador para ver los cambios.

  • Firefox/Safari: Mantén presionada la tecla Mayús mientras pulsas el botón Actualizar, o presiona Ctrl+F5 o Ctrl+R (⌘+R en Mac)
  • Google Chrome: presiona Ctrl+Shift+R (⌘+Mayús+R en Mac)
  • Internet Explorer: mantén presionada Ctrl mientras pulsas Actualizar, o presiona Ctrl+F5
  • Opera: dirígete a Menú → Configuración (Opera → Preferencias en Mac) y luego a Privacidad y seguridad → Borrar datos de navegación → Imágenes y archivos en caché.
mw.hook('wikipage.diff').add(function tag_status_change() {

// 2018-11-06 add status-change tags to pages with diffs that change the status
const diff = document.querySelector('.diff');
if (!diff) return;

// exclude pages outside ns 0
if (mw.config.get('wgNamespaceNumber') != 0) return;

const status_tags = ['[sS]tub', '[pP]artial', '[cC]omplete'];

diff.querySelectorAll('.diff-deletedline').forEach(function find_status_change(el) {
  const row = el.parentElement;
  const rem = row.querySelector('.diff-deletedline');
  const add = row.querySelector('.diff-addedline');
  // FIXME: the above selectors do not work in newer diffs
  if (!rem || !add) return;

  var other_tags = [];
  var i;
  for (i=0; i<status_tags.length; i+=1) {
    var st = new RegExp('\{\{' + status_tags[i] + '\}\}');
    if (!st.test(rem.innerText)) {
      other_tags.push(status_tags[i]);
    }
  }
  if (other_tags.length == status_tags.length) return;
  
  other_tags = other_tags.toString().replaceAll(',', '|');
  var ot = new RegExp('\{\{(' + other_tags + ')\}\}');
  if (!ot.test(add.innerText)) return;
  
  const msg = rem.innerText.trim() +' => '+ add.innerText.trim();

// guard against tagging latest revision in multi-change diffs
if (diff.querySelector('.diff-multi')) {
 // TODO: determine which of the revisions to tag?
 console.warn('gadget:', 'tag-status-change', 'ignored on combination diff');
 mw.notify('theses revisions contain a potential status-change ('+ msg +') which has been ignored');
 return; 
}

console.warn('gadget:', 'tag-status-change')

// send the notification 
;(new mw.Api).postWithToken('csrf', {
  action: 'tag',
  revid: mw.config.get('wgRevisionId'),
  add: 'status-change',
  reason: msg,
}).then(function notify_success() {
     mw.notify('revision tagged with '+ msg);
})


})

})