MediaWiki:Gadget-watchlistnotifier.js

Nota: Después de guardar, debes refrescar la caché de tu navegador para ver los cambios. Internet Explorer: mantén presionada Ctrl mientras pulsas Actualizar. Firefox: mientras presionas Mayús pulsas el botón Actualizar, (o presiona Ctrl-Shift-R). Los usuarios de Google Chrome y Safari pueden simplemente pulsar el botón Recargar. Para más detalles e instrucciones acerca de otros exploradores, véase Ayuda:Cómo limpiar la caché.

/*
 * Watchlist notifier ([[w:User:Ais523/watchlistnotifier.js]])
 * displays a message every time a watched page changes.
 * <nowiki>
 */
// Protege contra doble inclusión y que no se este en la lista de seguimiento
if ( window.wmwpajax === undefined && mw.config.get('wgCanonicalSpecialPageName') !== "Watchlist") {
    //Checker
    // From [[WP:US]] mainpage (wpajax renamed to wmwpajax)
    var wmwpajax = {
        download: function (bundle) {
            // mandatory: bundle.url
            // optional:  bundle.onSuccess (xmlhttprequest, bundle)
            // optional:  bundle.onFailure (xmlhttprequest, bundle)
            // optional:  bundle.otherStuff OK too, passed to onSuccess and onFailure
            var x = window.XMLHttpRequest ? new XMLHttpRequest() :
                window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") :
                false;

            if (x) {
                x.onreadystatechange = function () {
                    x.readyState == 4 && wmwpajax.downloadComplete(x, bundle);
                };
                x.open("GET", bundle.url, true);
                x.send(null);
            }
            return x;
        },

        downloadComplete: function (x, bundle) {
            x.status == 200 && (bundle.onSuccess && bundle.onSuccess(x, bundle) || true) ||
            (bundle.onFailure && bundle.onFailure(x, bundle) || alert(x.statusText + ': ' + bundle.url));
        }
    };

    //Enlaza con window
    window.wmwpajax = wmwpajax;

    //Scope local para no contaminar espacio global
    (function ( $, mw ) {
        // Example:
        // function dlComplete(xmlreq, data) {
        //      alert(data.message + xmlreq.responseText);
        // }
        //  wmwpajax.download({url:'//en.wikipedia.org/w/index.php?title=Thresher&action=raw', 
        //                   onSuccess: dlComplete, message: "Here's what we got:\n\n" });
        // End of [[WP:US]] quote
        function wmWatchEditFound(xmlreq, data) {
            var watchrev, watchsum, watchrevold, watchpage, watchuser, watchrevid;
            if (xmlreq.responseText.indexOf('revid=') == -1) {
            	// Workaround for https://phabricator.wikimedia.org/T315639
            	// Can be removed when API is made available T316830
            	if(!document.getElementById('contentSub')) {
            		$('<div id="contentSub"></div>').insertBefore('#mw-content-text');
            	}

                $('#contentSub').html($('#contentSub').html() +
                    "<div class='watchlistnotify'>(<i>watchlistnotifier no pudo determinar " +
                    "si una página vigilada ha sido modificada</i>)</div>");
                return;
            }
            watchrev = xmlreq.responseText.split('revid="')[1].split('"')[0];
            try {
                watchrevold = document.cookie.split('ais523wmwatchrev=')[1].split('.')[0];
            } catch (junk) {
                watchrevold = 0;
            }
            if (mw.config.get('wgPageName') == "Special:Watchlist") {
                document.cookie = "ais523wmwatchrev=" + watchrev + ".; path=/";
                var aas = mw.util.$content[0].getElementsByTagName('a');
                var i = aas.length;
                while (i--) {
                    if (aas[i].href.indexOf('diff=') != -1 && watchrevold) {
                        if (+(aas[i].href.split('diff=')[1].split('&')[0]) > watchrevold) {
                            aas[i].parentNode.style.fontWeight = 'bold';
                        }
                    }
                }
            } else {
                if (watchsum = xmlreq.responseText.split('comment="')[1]) {
                    watchsum = xmlreq.responseText.split('comment="')[1].split('"')[0];
                } else {
                    watchsum = '';
                }
                watchpage = xmlreq.responseText.split('title="')[1].split('"')[0];
                watchuser = xmlreq.responseText.split('user="')[1].split('"')[0];
                watchrevid = xmlreq.responseText.split('revid="')[1].split('"')[0];
                watchsum = watchsum.split('<').join('&lt;').split('>').join('&gt;');
                watchpage = watchpage.split('<').join('&lt;').split('>').join('&gt;');
                watchuser = watchuser.split('<').join('&lt;').split('>').join('&gt;');
                if (watchrev != watchrevold) {
                    $('#contentSub').html($('#contentSub').html() +
                        "<div class='watchlistnotify'>\"" + '<a href="/wiki/' +
                        watchpage + '">' + watchpage + '</a>' + '" ha sido modificado por ' +
                        '<a href="/wiki/User:' + watchuser + '">' + watchuser + '</a> ' +
                        '(<a href="/wiki/User Talk:' + watchuser + '">' + 'disc' + '</a>)' +
                        ' "' + watchsum + '". ' + '(<a href="' + mw.util.wikiScript('index') +
                        '?title=' + watchpage + '&diff=' + watchrevid + '">' + 'dif' + '</a>) ' +
                        '(<a href="/wiki/Special:Watchlist">lista de seguimiento</a>)</div>');
                }
            }
        }

        $(document).ready(function () {
            /* Find the top item in the watchlist, and its edit summary. We only need one item, so
               set the limit to 1 to ease the load on the server. */
            wmwpajax.download({
                url: mw.util.wikiScript('api') + '?action=query&list=watchlist&wllimit=1&' +
                    'wldir=older&format=xml&wlprop=comment|ids|title|user',
                onSuccess: wmWatchEditFound
            });
        });

    })( jQuery, mediaWiki ); // Fin de función anónima
} //fin de chequeo
//</nowiki>