MediaWiki:Gadget-WiP.js

// if ( $.inArray( 'Works In Progress', mw.config.get( 'wgCategories' ) ) !== -1 && $( '.WorkInProgressDone' ).attr( 'id' ) !== undefined ) { var wipArticleId = mw.config.get( 'wgArticleId' ); /* This it the code to get a list of redirects and create a string element from them delimited by pipes *//* var templateWipRedirects = 'Work-in-progress'; var templateWipId = 42820;// Template:Work-in-progress ( new mw.Api ).get( {   action: 'query',    prop: 'redirects',    titles: 'Template:Work-in-progress'  } ).done( function( rdResult ) {    if ( rdResult.query.pages[ templateWipId ].redirects !== undefined ) {      var redirects = [];      $.each( rdResult.query.pages[ templateWipId ].redirects, function( index, redirect ) { if ( redirect.ns === 10 ) { redirects[ index ] = redirect.title.replace( 'Template:', '' ); }     } );      if ( templateWipRedirects.length >= 1 ) {        templateWipRedirects += '|' + redirects.join( '|' );      }    }  } ); */ function removeTag( wipIndexNumber ) { ( new mw.Api ).get( {     action: 'query',      prop: 'revisions',      rvprop: 'content',      pageids: wipArticleId    } ).done( function ( rvResult ) {      var content = rvResult.query.pages[ wipArticleId ].revisions[ 0 ][ '*' ];      var matchCountIndex = 0;      var matchCountStart = 0;      while ( matchCountIndex !== -1 ) {        var matchText = content.match( /\{\{(?:WiP|Work-in-progress)(?:\|(?:.*?))?\}\}(?:\n*)?/g );        if ( matchText[ matchCountIndex ] === undefined ) {          matchCountIndex = -1;        } else {          var preTextStart = content.indexOf( matchText[ matchCountIndex ], matchCountStart );          var matchTextLength = matchText[ matchCountIndex ].length;          matchCountStart = preTextStart + matchTextLength;          if ( matchCountIndex === ( wipIndexNumber - 1 ) ) {            var preText = content.substr( 0, preTextStart );            var remText = content.substr( preTextStart, matchCountStart ); remText = remText.match( /\{\{(WiP|Work-in-progress)(?:\|(.*?))?\}\}\n+/ ); var postText = content.substr( matchCountStart ); var newText = preText + postText; ( new mw.Api ).postWithToken( 'edit', {             action: 'edit',              pageid: wipArticleId,              minor: true,              text: newText,              summary: 'Removing T:' + remText[ 1 ] + ' #' + wipIndexNumber + ' from page.'            } ).done( function ( editResult ) {             if ( editResult.edit.result === 'Success' ) {                console.log( 'success:\n%o', remText );                if ( remText[ 2 ] !== undefined ) {                  /* Better to build an object here with all of the parameters used in the template than to test for specific parameters */                  //if ( /small=/.test( remText[ 2 ] ) === false ) {                  /* Notify the user's who's name was in the template that it has been removed from the page on their talk page. */console.log( 'Notify: %s', remText[ 2 ] );                  //}                }                /* if Link replace line with big text that say the template has been successfuly removed from the source then fades away */ /* if Button replace red color with green, icon with green check, and text to say the template has been successfuly removed from the source then fades away */ } else { /* indicate the edit failed */console.log( 'failed' ); }           } );            matchCountIndex = -2;          }          matchCountIndex++;        }      }    } ); } $( '.WorkInProgressDone' ).each( function( i ) {    var wipID = $( this ).attr( 'id' );    var wipIndexNumber = wipID.replace( 'WiP',  ).replace( /(Link|Button)/gi,  );    var wipLinkOrButton = wipID.replace( 'WiP' + wipIndexNumber, '' );    if ( wipLinkOrButton === 'Link' ) {      $( 'div#WiP' + wipIndexNumber + 'Link' ).html( '[&thinsp;Mark as done&thinsp;]' ).toggle;    } else if ( wipLinkOrButton === 'Button' ) {      $( 'div#WiP' + wipIndexNumber + 'Button' ).html( '' ).toggle;    } else {      alert( 'Invalid WiP template detected. Please report this page to an administrator. Thanks.' );   }  } ); } //