MediaWiki:Gadget-BackgroundRC.js

Z Nonsensopedii, polskiej encyklopedii humoru

Uwaga: aby zobaczyć zmiany po zapisaniu, może zajść potrzeba wyczyszczenia pamięci podręcznej przeglądarki.

  • Firefox / Safari: Przytrzymaj Shift podczas klikania Odśwież bieżącą stronę, lub naciśnij klawisze Ctrl+F5 lub Ctrl+R (⌘-R na komputerze Mac)
  • Google Chrome: Naciśnij Ctrl-Shift-R (⌘-Shift-R na komputerze Mac)
  • Internet Explorer: Przytrzymaj Ctrl jednocześnie klikając Odśwież lub naciśnij klawisze Ctrl+F5
  • Konqueror: Kliknij polecenie Odśwież lub naciśnij klawisz F5
  • Opera: Wyczyść pamięć podręczną w Narzędzia → Preferencje
/** 
 * łałtor: [[User:Ostrzyciel]] 
 **/
$( document ).ready( function() {
	if ( mw.config.get( 'wgCanonicalSpecialPageName' ) !== 'Recentchanges' ) {
		return;
	}

	var title = document.title;
	var bgrc = {};

	// setup ajax hook
	( function( send ) {
		jQuery.ajax = function() {
			if ( arguments.length === 2 && 
				arguments[0].indexOf( 'Specjalna:Ostatnie_zmiany' ) > -1 &&
				arguments[0].indexOf( 'action=render' ) > -1 &&
				arguments[0].indexOf( 'peek=1' ) > -1
			) {
				bgrc.requestUrl = arguments[0];
			}
			return send.apply(this, arguments);
		};
	} )( jQuery.ajax );

	bgrc.updateTitle = function( count ) {
		if ( !count ) {
			document.title = title;
		} else {
			document.title = '(' + count + ') ' + title;
		}
	};

	bgrc.intervalId = null;

	bgrc.stopUpdates = function() {
		if ( bgrc.intervalId ) {
			window.clearInterval( bgrc.intervalId );
		}
		bgrc.intervalId = null;
	};

	bgrc.setUpdates = function( interval ) {
		bgrc.stopUpdates();
		bgrc.intervalId = window.setInterval( bgrc.doUpdate, interval );
	};

	bgrc.doUpdate = function() {
		if ( !bgrc.requestUrl ) {
			return;
		}

		var requestUrl = bgrc.requestUrl.replace( '&peek=1', '' );
		$.ajax( requestUrl, { contentType: 'html' } ).done( function( content, message, jqxhr ) {
			if ( jqxhr.status !== 200 ) {
				return;
			}

			$resp = $( content );
			if ( $( 'div.mw-changeslist-empty', $resp ).length ) {
				return;
			}

			var tables = $('table.mw-changeslist-line', $resp );
			if ( tables.length === 0 ) {
				return;
			}
			var totalNum = 0;
			tables.each( function() {
				var len = $( this ).find( 'tr' ).length;
				if ( len > 1 ) {
					totalNum += ( len - 1 );
				} else {
					totalNum++;
				}
			} );
			bgrc.updateTitle( totalNum );
			bgrc.topTs = $( 'tr[data-mw-ts]', tables.first() ).first().attr( 'data-mw-ts' );

			// throttle updates
			bgrc.setUpdates( 30000 );
		} );
	};

	var prevType = null;
	$( window ).on( "blur focus", function( e ) {
		if ( prevType != e.type ) {
			switch ( e.type ) {
				case "blur":
					bgrc.setUpdates( 15000 );
					break;
				case "focus":
					bgrc.stopUpdates();
					bgrc.updateTitle( null );
					if ( bgrc.topTs ) {
						bgrc.requestUrl = bgrc.requestUrl.replace( /from=\d{14}/, 'from=' + bgrc.topTs );
					}
					break;
			}
		}
		prevType = e.type;
	} );
} );