مستخدم:Mohammed Qays/Gadget-LastContrib.js

ملاحظة: بعد الحفظ، قد يلزمك إفراغ الكاش لرؤية التغييرات (إفراغ الكاش الآن).

if ( mw.config.get( 'wgArticleId' ) > 0 && mw.config.get( 'wgAction' ) === 'view' ) {
    $.when( mw.loader.using( [ 'mediawiki.api', 'mediawiki.util', 'user' ] ), $.ready ).done( function () {
        'use strict';

        var alertLevels = {
            1: typeof lastContribNiveau1 !== 'undefined' ? lastContribNiveau1 : 100,    
            2: typeof lastContribNiveau2 !== 'undefined' ? lastContribNiveau2 : 8 * 60, 
            3: typeof lastContribNiveau3 !== 'undefined' ? lastContribNiveau3 : 40 * 60 
        };

        function lastContribCallback( timestampLastRevision ) {
            var last = new Date( timestampLastRevision );
            var now = new Date();
            var diff = Math.round( ( now - last ) / 1000 );

            if ( diff < -600 ) {
                mw.notify( "Erreur probable dans la concordance des horloges serveur-client. Vérifiez l'exactitude de l'heure de votre ordinateur et le fuseau horaire utilisé dans vos préférences globales wiki.", {
                    autoHide: true,
                    title: 'Gadget LastContrib',
                    type: 'error'
                } );
                return;
            }
            if ( diff < 0 ) {
                diff = 0;
            }

            if ( diff <= alertLevels[ 3 ] ) {
                var $contentSub = $( '#contentSub' );
                if ( !$contentSub.length ) { return; }

                var diffHeures = Math.floor( diff / 3600 );
                var diffMinutes = Math.floor( ( diff - diffHeures * 3600 ) / 60 );
                var diffSecondes = diff - diffHeures * 3600 - diffMinutes * 60;

                var icon;
                if ( diff <= alertLevels[ 1 ] ) {
                    icon = '<img src="https://upload.wikimedia.org/wikipedia/commons/archive/4/4e/20190401212815%21OOjs_UI_icon_alert_destructive.svg" width="24" height="24" style="margin-right:5px">';
                } else if ( diff <= alertLevels[ 2 ] ) {
                    icon = '<img src="https://upload.wikimedia.org/wikipedia/commons/archive/3/3b/20180610093750%21OOjs_UI_icon_alert-warning.svg" width="24" height="24" style="margin-right:5px">';
                } else {
                    icon = '';
                }

                var ellapsed = ( diffHeures > 0 ? diffHeures + 'ساعة ' : '' ) + ( diffMinutes > 0 ? diffMinutes + 'دقيقة ' : '' ) + diffSecondes + ' ثانية';
                var $link = $( '<a href="' + mw.util.getUrl( null, {
                    action: 'history'
                } ) + '" style="font-size:8pt; color:#545454;">' + last.toLocaleTimeString() + ' (' + ellapsed + ')</a> ' );

                $contentSub.prepend( icon, $link );
            }
        }

        new mw.Api()
            .get( {
                'action': 'query',
                'prop': 'revisions',
                'titles': mw.config.get( 'wgPageName' ),
                'rvprop': 'timestamp',
                'formatversion': 2
            } )
            .then( function ( data ) {
                var page = data.query.pages[ 0 ];
                lastContribCallback( page.revisions[ 0 ].timestamp );
            } )
            .fail( function ( error, details ) {
                if ( details && details.xhr && details.xhr.readyState < 4 ) {
                    return;
                }
                mw.notify( "Erreur rencontrée lors de la requête à l'API.", {
                    autoHide: true,
                    title: 'Gadget LastContrib',
                    type: 'error'
                } );
                console.log( error, details );
            } );

    } );
}