User:Disneyfreak96/wikilove.js

mw.loader.load( 'jquery.ui' ); function getXhr { try { return new XMLHttpRequest; } catch(e) { try { return new ActiveXObject("Msxml2.XMLHTTP"); } catch(e) { try { return new ActiveXObject("Microsoft.XMLHTTP"); } catch(e) { throw "Could not create an XmlHttpRequest"; }}} } function apiRequest( query ) { var xhr = getXhr; xhr.open('POST', wgScriptPath + '/api.php?format=json', true); xhr.setRequestHeader( "Content-Type", "application/x-www-form-urlencoded" ); xhr.send( query ); xhr.onreadystatechange = function { if ( xhr.readyState == 4 ) { var response; var error = false; if ( xhr.status != 200 ) { error = "Bad XMLHttpRequest status."; } else { response = JSON.parse( xhr.responseText ); if ( response.edit.result == "Success" ) { window.location.reload; } else { error = "Unknown result from API."; }     }      if ( !response || error ) { $('#barnstarForm div').hide; $('#barnstarForm').append ( $(' ').html( 'Error: '+error ) ); }   }  } } function getEditToken { $.getJSON(   wgScriptPath + '/api.php?',    { action: 'query',      prop: 'info',      intoken: 'edit',      titles: 'Main Page',      indexpageids: '',      format: 'json'    },    function( data ) {      if ( data.query.pages && data.query.pageids ) {        var pageid = data.query.pageids[0];        wgEditToken = data.query.pages[pageid].edittoken;      }    }  ) } function initializeBarnstars { if (( wgCanonicalNamespace == 'User_talk' && wgAction == 'view' )) { var $barnstarList = [ 'The Original Barnstar', 'The Editor\'s Barnstar', 'The Working Man\'s Barnstar', 'The Minor Barnstar', 'The Random Acts of Kindness Barnstar', 'The Barnstar of Diligence', 'The Surreal Barnstar', 'The Tireless Contributor Barnstar', 'The Photographer\'s Barnstar', 'The Defender of the Wiki Barnstar', 'The Anti-Vandalism Barnstar', 'The Barnstar of Good Humor', 'The Graphic Designer\'s Barnstar', 'The Copyeditor\'s Barnstar', 'The Special Barnstar', 'The Citation Barnstar', 'The Teamwork Barnstar' ];   $barnstars = $(' ') .append( $(' ').html( 'Select barnstar: ' ).append( $(' ') ) ) .append( $(' ').html( 'Enter a message (including signature): ' ).append( $(' ') ) ) .dialog({       width: 500,        height: 245,        autoOpen: false,        title: 'Spread the WikiLove',        modal: true,        buttons: { "Submit": function {           var x = 0;          // If edit token hasn't been retrieved, wait for it          while ( typeof wgEditToken == undefined && x < 6 ) {            $('#barnstarForm div').hide;            $('#barnstarForm').append ( $(' ').html( 'Retrieving edit token... ' ) );            setTimeout( 'x++', 1000 );          }          if ( typeof wgEditToken != undefined ) {            $('#barnstarForm div').hide;            $('#barnstarForm').append ( $(' ').html( 'Adding barnstar to page... ' ) );            // Perform edit to page            var template = '%7B%7Bsubst%3A'+encodeURIComponent( $('#barnstarSelect').val )+'%7C'+encodeURIComponent( $('#barnstarMessage').val )+'%7D%7D'; var summary = 'Barnstar'; apiRequest( 'action=edit&title='+encodeURIComponent( wgPageName )+'&section=new&summary='+summary+'&text='+template+'&token='+encodeURIComponent( wgEditToken ) ); } else { $('#barnstarForm div').hide; $('#barnstarForm').append ( $(' ').html( 'Error: Could not retrieve edit token.' ) ); }       }}      });    // Populate select list    $.each( $barnstarList, function(index, value) { $('#barnstarSelect').append( $('  ').val(value).html(value) ); });    // Insert new tab into page    $('#ca-history').after(' WikiLove ');  } } $( initializeBarnstars );