User:Theopolisme/Scripts/ajaxWatchlist.js: Difference between revisions

Content deleted Content added
load dependencies
Backported from https://meta.wikimedia.org/w/index.php?title=User:He7d3r/Tools/ajaxWatchlist.js&oldid=12538372 "Do not reload the watchlist while there is user interaction; Use .done/.fail instead of the deprecated jQuery callbacks; Do not..."
Line 7:
*
* @author Theopolisme
* @author He7d3r
*/
( function ( $, mw ) {
"use strict";
var jqxhr, interval, $ajaxWatchlist;
 
function updateWatchlist () {
var $loadingIndicator = $( '.watchlistLoadingIndicator' ),
$content = $( '#mw-content-text' ),;
 
$ajaxWatchlist = $( '#ajaxWatchlist' );
 
// If this is the first time we run the script, wrap everything
Line 21 ⟶ 24:
if ( $ajaxWatchlist.length === 0 ) {
$content.find( '#mw-watchlist-form' ).nextAll().addBack().wrapAll( '<div id="ajaxWatchlist"></div>' );
$ajaxWatchlist = $( '#ajaxWatchlist' );
.hover( function () {
jqxhr.abort();
clearInterval( interval );
}, function () {
interval = setInterval( updateWatchlist, window.watchlistUpdateFrequency || 20000 );
} );
}
 
Line 42 ⟶ 51:
 
// Make the ajax request to actually update the watchlist
jqxhr = $.ajax( {
url: ___location.href,
dataType: 'html',
} )
success: .done( function ( data ) {
// If the watchlist contents have changed, update the page
// to display the new contents.
var $newContent = $( data ).find( '#mw-content-text #mw-watchlist-form' ).nextAll().addBack(); // Same selector as $ajaxWatchlist
if ( $ajaxWatchlist.text() !== $newContent.text() ) {
$ajaxWatchlist.empty().append( $newContent );
mw.hook( 'wikipage.content' ).fire( $ajaxWatchlist ); // So scripts will run on the updated content
}
$loadingIndicator.hide();
}, )
error:.fail( function ( jqXHR, textStatus ) {
// Hide the indicator and then display an error notification
$loadingIndicator.hide();
if ( textStatus !== 'abort' ) {
mw.notify( 'ajaxWatchlist: Unable to automatically update watchlist.' );
}
} );
Line 68 ⟶ 79:
// Run updateWatchlist() every 20 seconds by default (can be configured via window.watchlistUpdateFrequency)
mw.loader.using( [ 'jquery.spinner', 'mediawiki.notify' ], function () {
interval = setInterval( updateWatchlist, window.watchlistUpdateFrequency || 20000 );
} );
}