Jump to content

User:Wbm1058/ControversialMoves.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.
// Adds a checkbox to the "Move page" special page, containing the content "Do you think this move might be controversial?"
// When checked, a move request is added to the talkpage, using the proper template.

wgCanonicalSpecialPageName === "Movepage" && 
wgRelevantPageName.indexOf(":") == -1 && // mainspace only
$(function($){
  var submitButton = $(".mw-submit > input"), submitText = submitButton.val();
  function submitFunction( e ){
    e.preventDefault();
    var newTitle = $("#wpNewTitleMain").val();
    if ( newTitle == wgRelevantPageName || ! newTitle ) {
      alert( "Please select a new page title." ); return;
    }
    submitButton.attr("disabled", "disabled");
    $( "body" ).css("cursor", "wait");
    $.ajax( { "url" : "/w/api.php", data : { "format" : "json", "action" : "tokens" }, "error" : cmError, success : function( t ){ 
      $.ajax( { "url" : "/w/api.php", "type" : "POST", "data" : { "format" : "json", "action" : "edit", "title" : "Talk:" + wgRelevantPageName, "watchlist" : "watch", "section" : "new", "sectiontitle" : "Requested move", appendtext : "\{\{subst:requested move|" + newTitle + "}}" + $("#wpReason").val() + " \~\~\~\~", "token" : t.tokens.edittoken }, "error" : cmError, "success" : function(){ location.href = "/wiki/Talk:" + wgRelevantPageName + "#Requested_move" }})
    }})
  }
  function cmError( q, w, e ){
    $("#movepage").append( $("<span>").css("color","red").text("An error has occured: " + e ) );
  }
  submitButton.parent().parent().before( 
    $("<tr>").append(
      $("<td>"), 
      $("<td>").append( $("<input>").attr({"type":"checkbox","id":"controversialMove"} ).click(function(){
        var checked = $(this).attr("checked") == "checked";
        submitButton.attr("value", checked ? "Propose moving the page" : submitText );
        $("#wpMovetalk").attr("disabled", checked && "disabled" );
        $("#movepage")[ checked ? "on" : "off" ]( "submit", submitFunction );
      }), " ", $("<label>").attr("for", "controversialMove").text("Do you think this move might be controversial?") )
    )
  )
})