Jump to content

User:Technical 13/SandBox/NoThanks.js

From Wikipedia, the free encyclopedia
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.
/* This is the beta version of this script, for the stable version see: [[User:Technical_13/Scripts/NoThanks.js]] or use the documentation link above ↑ */

/* Diff pages */
if (wgAction != 'history') { // Haven't found what is the special setting for viewing diffs yet...
    $('a.mw-thanks-thank-link').replaceWith('-⊗-');
    $('div#mw-diff-ntitle1').html($('div#mw-diff-ntitle1').html().replace(' (-⊗-)', ''));
}
 
/* History pages */
if (wgAction == 'history') {
    $('li').each(function () {
        var $elem = $(this);
        $elem.children('a.mw-thanks-thank-link').replaceWith('-⊗-');
      //  For history pages with undo links (or some other link)
//        $elem.html($elem.html().replace(' | -⊗-', ''));
      //  For history pages with no other links
//          if ($elem.children('span.comment').html().length == 0){
//                $elem.children('span.mw-changeslist-separator:last').replaceWith('-⊗-');
//                $elem.html($elem.html().replace(' -⊗-  ', ''));
//          }
//          $elem.html($elem.html().replace(' (-⊗-)', ''));
    });
}

var thankedHistory = {
        maxHistory: 100,
        load: function() {
                var cookie = $.cookie( 'thanks-thanked' );
                if ( cookie === null ) {
                        return [];
                }
                return unescape( cookie ).split( ',' );
        },
        push: function( $thankLink ) {
                var saved = this.load();
                saved.push( $thankLink.attr( 'data-revision-id' ) );
                if ( saved.length > this.maxHistory ) { // prevent forever growing
                        saved = saved.slice( saved.length - this.maxHistory );
                }
                $.cookie( 'thanks-thanked', escape( saved.join( ',' ) ) );
        }
};

$( 'a.mw-thanks-thank-link' ).click( function( e ) {
        var $thankLink = $( this ), source;
        e.preventDefault();
        if ( mw.config.get( 'wgAction' ) === 'history' ) {
                source = 'history';
        } else {
                source = 'diff';
        }
        ( new mw.Api ).get( {
                'action' : 'thank',
                'rev' : $thankLink.attr( 'data-revision-id' ),
                'source' : source,
                'token' : mw.user.tokens.values.editToken
        } )
        .done( function( data ) {
                $thankLink.before( mw.message( 'thanks-thanked', mw.user ).escaped() );
                $thankLink.remove();
                thankedHistory.push( $thankLink );
        } )
        .fail( function( errorCode, details ) {
                // TODO: use something besides alert for the error messages
                switch( errorCode ) {
                        case 'invalidrevision':
                                alert( mw.msg( 'thanks-error-invalidrevision' ) );
                                break;
                        case 'ratelimited':
                                alert( mw.msg( 'thanks-error-ratelimited' ) );
                                break;
                        default:
                                alert( mw.msg( 'thanks-error-undefined' ) );
                }
        } );
} );