Jump to content

User:Kangaroopower/AjaxRC.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Kangaroopower (talk | contribs) at 18:57, 1 August 2013 (rm wikia specific code). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
/*
 * ADVANCED AJAX AUTO-REFRESHING ARTICLES
 * Code originally by "pcj" of Wowpedia
 * Maintenance, cleanup, style and bug fixes by Grunny (http://community.wikia.com/wiki/User:Grunny) 
 * and Kangaroopower (http://community.wikia.com/wiki/User:Kangaroopower)
 */
( function ( $, mw, window ) {
	'use strict';

	var	ajaxIndicator = window.ajaxIndicator || 'http://images2.wikia.nocookie.net/dev/images/8/82/Facebook_throbber.gif',
		ajaxTimer,
		refreshText = typeof window.AjaxRCRefreshText === 'string' ? window.AjaxRCRefreshText : 'AJAX',
		refreshHover = typeof window.AjaxRCRefreshHoverText === 'string' ? window.AjaxRCRefreshHoverText : 'Enable auto-refreshing page loads',
		ajRefresh = window.ajaxRefresh || 60000,
		ajPages = window.ajaxPages || [ 'Special:RecentChanges' ];


	function storage( setTo ) {
		if ( localStorage.getItem( 'AjaxRC-refresh' ) === null ) {
			localStorage.setItem( 'AjaxRC-refresh', true );
		}
		if ( setTo === false ) {
			localStorage.setItem( 'AjaxRC-refresh', false );
		} else if ( setTo === true ) {
			localStorage.setItem( 'AjaxRC-refresh', true );
		}
		return JSON.parse( localStorage.getItem( 'AjaxRC-refresh' ) );
	}


	/**
	 * Main function to start the Auto-refresh process
	 */
	function preloadAJAXRL() {
		var	$appTo = $( '.firstHeading' ) );
		$appTo.append( '&nbsp;<span style="font-size: xx-small; line-height: 100%;" id="ajaxRefresh"><span style="border-bottom: 1px dotted; cursor: help;" id="ajaxToggleText" title="' + refreshHover + '">' + refreshText + ':</span><input type="checkbox" style="margin-bottom: 0;" id="ajaxToggle"><span style="display: none;" id="ajaxLoadProgress"><img src="' + ajaxIndicator + '" style="vertical-align: baseline; float: none;" border="0" alt="Refreshing page" /></span></span>' );
		$( document ).ajaxSend( function ( event, xhr, settings ) {
			if ( location.href === settings.url ) {
				$( '#ajaxLoadProgress' ).show();
			}
		} ).ajaxComplete ( function ( event, xhr, settings ) {
			var	$collapsibleElements = $( '#mw-content-text' ).find( '.mw-collapsible' ),
				ajCallAgain = window.ajaxCallAgain || [];
			if ( location.href === settings.url ) {
				$( '#ajaxLoadProgress' ).hide();
				if ( $collapsibleElements.length ) {
					$collapsibleElements.makeCollapsible();
				}
				if ( mw.config.get( 'wgCanonicalSpecialPageName' ) === 'Recentchanges' ) {
					mw.special.recentchanges.init();
				}
				for ( var i = 0; i < ajCallAgain.length; i++ ) {
					ajCallAgain[i]();
				}
			}
		} );
		$( '#ajaxToggle' ).click( toggleAjaxReload );
		$( '#ajaxToggle' ).attr( 'checked', storage());
		if ( storage() ) {
			loadPageData();
		}
	}

	/**
	 * Turn refresh on and off by toggling the checkbox
	 */
	function toggleAjaxReload() {
		if ( $( '#ajaxToggle' ).prop( 'checked' ) === true ) {
			storage( true );
			loadPageData();
		} else {
			storage( false );
			clearTimeout( ajaxTimer );
		}
	}

	/**
	 * Does the actual refresh
	 */
	function loadPageData() {
		var $temp = $( '<div>' );
		$temp.load( location.href + " #mw-content-text", function () {
			var $newContent = $temp.children( '#mw-content-text' );
			if ( $newContent.length ) {
				$( '#mw-content-text' ).replaceWith( $newContent );
			}
			ajaxTimer = setTimeout( loadPageData, ajRefresh );
		} );
	}

	/**
	 * Load the script on specific pages
	 * Should we make it load only on view or just not on edit....
	 */
	$( function () {
		if ( $.inArray( mw.config.get( 'wgPageName' ), ajPages ) !== -1 && $( '#ajaxToggle' ).length === 0 && mw.config.get( 'wgAction' ) !== 'edit' ) {
			preloadAJAXRL();
		}
	} );

}( jQuery, mediaWiki, this ) );