Jump to content

User:DannyS712/ContribSearch.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.
//<nowiki>
//Copied from [[User:Abelmoschus Esculentus/ContribSearch.js]
var user, rollbacktoken, cs_limit, blocked = false, lastcontrib = null, newcontrib = false;
var enabled = false;
function checkifblocked() {
  var cs_http3 = new XMLHttpRequest();
  cs_http3.onreadystatechange = function() {
    var items3 = cs_http3.responseXML.getElementsByTagName('block');
    if (items3.length !== 0) {
      blocked = true;
    }
    else blocked = false;
  };
  cs_http3.open("GET", "/w/api.php?action=query&list=blocks&bkusers=" + user + "&format=xml", true);
  cs_http3.send(); 
}
function get_content() {
  if ($("#CS-interface-content").text() != "Loading contributions..." && enabled === true) $("#CS-interface-header").text("Contribution Search - " + user.replace('_', ' ') + " (Updating...)");
  var cs_http2 = new XMLHttpRequest();
  cs_http2.onreadystatechange = function() {
    if (cs_http2.readyState == 4 && cs_http2.status == 200 && enabled === true) {
      var items = cs_http2.responseXML.getElementsByTagName('item');
      if (items.length === 0) {
        alert("Cannot find any contributions for " + user.replace('_', ' '));
        screen00();
        return;
      }
      if (lastcontrib !== null && lastcontrib != items[0].getAttribute('revid').substring(0)) {
        newcontrib = true;
      }
      if ($("#CS-interface-content").text() == "Loading contributions...") {
        $("#CS-interface-content").empty();
      }
      $("#CS-interface-content").empty();
      var temp = user.replace('_', '+');
      for (var i = 0; i < items.length; i++) {
        var diff = items[i].getAttribute('revid').substring(0);
        var gs = items[i].getAttribute('timestamp').substring(0);
        var ts = gs.replace('T', ' ');
        var timestamp = ts.replace('Z', '');
        var editsummary = items[i].getAttribute('comment').substring(0);
        if (editsummary === "") {
        	editsummary = "No summary";
        }
        var item_name = items[i].getAttribute('title').substring(0);
        var item_url = 'http://en.wikipedia.org/wiki/' + item_name;
        var diff_url = 'https://en.wikipedia.org/w/index.php?title=' + item_name + '&diff=prev&oldid=' + diff;
        var rollbackurl = 'http://en.wikipedia.org/w/index.php?action=rollback&title=' + item_name + '&from=' + temp + '&token=' + rollbacktoken;
        if (newcontrib === true && lastcontrib == diff) {
          $('#HRULE').remove();
          newcontrib = false;
          lastcontrib = diff;
          if (items[i].hasAttribute('minor')) {
            $("#CS-interface-content").append(
              $('<div>').css('margin-bottom','0.5em').append(
                $('<hr>').attr({'id':'HRULE'}),
                $('<font>').text(timestamp+' '),
                $('<b>').text('m '),
                $('<a>').attr({'href':item_url}).text(item_name),
                $('<font>').text(' ('),
                $('<a>').attr({'href':diff_url}).text('diff'),
                $('<font>').text(') ['),
                $('<a>').attr({'href':rollbackurl}).text('rollback'),
                $('<font>').text(']')
              ),
              $('<div>').css('margin-bottom','0.5em').append(
                $('<i>').text(' (' + editsummary + ')')
              )
            );
          }
          else {
            $("#CS-interface-content").append(
              $('<div>').css('margin-bottom','0.5em').append(
                $('<hr>').attr({'id':'HRULE'}),
                $('<font>').text(timestamp+' '),
                $('<a>').attr({'href':item_url}).text(item_name),
                $('<font>').text(' ('),
                $('<a>').attr({'href':diff_url}).text('diff'),
                $('<font>').text(') ['),
                $('<a>').attr({'href':rollbackurl}).text('rollback'),
                $('<font>').text(']')
              ),
              $('<div>').css('margin-bottom','0.5em').append(
                $('<i>').text(' (' + editsummary + ')')
              )
            );
          }
        }
        else {
          if (items[i].hasAttribute('minor')) {
            $("#CS-interface-content").append(
              $('<div>').css('margin-bottom','0.5em').append(
                $('<font>').text(timestamp+' '),
                $('<b>').text('m '),
                $('<a>').attr({'href':item_url}).text(item_name),
                $('<font>').text(' ('),
                $('<a>').attr({'href':diff_url}).text('diff'),
                $('<font>').text(') ['),
                $('<a>').attr({'href':rollbackurl}).text('rollback'),
                $('<font>').text(']')
              ),
              $('<div>').css('margin-bottom','0.5em').append(
                $('<i>').text(' (' + editsummary + ')')
              )
            );
          }
          else {
            $("#CS-interface-content").append(
              $('<div>').css('margin-bottom','0.5em').append(
                $('<font>').text(timestamp+' '),
                $('<a>').attr({'href':item_url}).text(item_name),
                $('<font>').text(' ('),
                $('<a>').attr({'href':diff_url}).text('diff'),
                $('<font>').text(') ['),
                $('<a>').attr({'href':rollbackurl}).text('rollback'),
                $('<font>').text(']')
              ),
              $('<div>').css('margin-bottom','0.5em').append(
                $('<i>').text(' (' + editsummary + ')')
              )
            );
          }
        }
      }
      if (lastcontrib === null) {
        lastcontrib = items[0].getAttribute('revid').substring(0);
      }
      $('#SHOWING').text(" Showing " + items.length + " items; Maximum " + cs_limit + " items ");
      $("#CS-interface-header").text("Contribution Search - " + user.replace('_', ' '));
      checkifblocked();
      if (blocked === true) {
        $("#CS-interface-header").append(
          $('<font>').attr({'color':'red','font-weight':'bold'}).text(' (BLOCKED)')
        );
      }
      setTimeout(get_content, 3000);
    }
  };
  cs_http2.open("GET", "/w/api.php?action=query&list=usercontribs&ucuser=" + user + "&uclimit=" + cs_limit + "&format=xml&ucdir=older", true);
  cs_http2.send();
}
function cs_init() {
  mw.util.addPortletLink('p-tb', 'javascript:void(0)', 'Contrib Search', 'aca-contribsearch', null, null);
  $('#aca-contribsearch').on('click', function() {
    $('body').prepend('<div id="CS-modal">'+
      '<div id="CS-interface">'+
        '<h4 id="CS-interface-header"></h4>'+
        '<hr>'+
        '<div id="CS-interface-content"></div>'+
        '<hr>'+
        '<div id="CS-interface-footer"></div>'+
      '</div>'+
    '</div>');
    $("#CS-modal").css({
      "position": "fixed",
      "z-index": "1",
      "left": "0",
      "top": "0",
      "width": "100%",
      "height": "100%",
      "overflow": "hidden",
      "background-color": "rgba(0,0,0,0.4)"
    });
    $("#CS-interface").css({
      "background-color": "#ffe8e8",
      "margin": "15% auto",
      "padding": "2px 20px",
      "border": "1px solid #888",
      "width": "80%",
      "max-width": "60em",
      "font-size": "90%"
    });
    $("#CS-interface-content").css({
      "min-height": "7em",
      "width" : "875px",
      "height" : "400px",
      "overflow-y": "scroll"
    });
    $("#CS-interface-footor").css("min-height", "3em");
    screen00();
  });
}
var screen00 = function() {
  $("#CS-interface-header, #CS-interface-content, #CS-interface-footer").empty();
  $("#CS-interface-header").text("Contribution Search");
  $("#CS-interface-content").append(
    $('<div>').css('margin-bottom','0.5em').append(
      $('<label>').attr({'for':'CS-userinput-label', 'id':'CS-userinput-label'}).text('Insert user: '),
      $('<input>').attr({'type':'text', 'name':'CS-userinput','id':'CS-userinput'})
    ),
    $('<div>').css('margin-bottom','0.5em').append(
      $('<label>').attr({'for':'CS-userinput-limit-label', 'id':'CS-userinput-limit-label'}).text('Limit (1-500): '),
      $('<input>').attr({'type':'number', 'name':'CS-userinput-limit','id':'CS-userinput-limit','min':'1','max':'500','value':'100'})
    ),
    $('<div>').css('margin-bottom','0.5em').append(
      $('<button>').attr({'name':'CS-userinput-button', 'id':'CS-userinput-button'}).text('Load')
    ),
    $('<div>').css('margin-bottom','0.5em').append(
      $('<img>').attr({'src':'https://upload.wikimedia.org/wikipedia/commons/e/e5/OOjs_UI_icon_alert_destructive_black-darkred.svg','height':'23','width':'23'}),
	  $('<span>').attr('style','font-size:94%;font-weight:bold;').text('You take full responsibility for any action you perform using this script. Persistent misuse of rollback will lead to the removal of the permission. In extreme circumstances, you may even be blocked from editing.')
	)
  );
  $("#CS-interface-footer").append(
    $('<button>').attr('id', 'CS-cancel').text('Close')
  );
  $('#CS-cancel').click(function() {
  	user = null, rollbacktoken = null, cs_limit = null, blocked = false;
  	enabled = false;
    $('#CS-modal').remove();
  });
  $('#CS-userinput-button').click(function () {
  	var ttt = $('#CS-userinput-limit').val();
  	if (ttt > 500 || ttt < 1) {
  		alert('Invalid value');
  	}
  	else if ($('#CS-userinput').val().length < 1) {
  		alert('Please specify a user!');
  	}
  	else {
  	enabled = true;
    var temp = $('#CS-userinput').val();
    user = temp.replace(' ', '_');
    cs_limit = $('#CS-userinput-limit').val();
    var temp2 = temp.replace('_', ' ');
    $("#CS-interface-header").text("Contribution Search - " + temp2);
    $("#CS-interface-content").empty();
    $("#CS-interface-content").text("Loading contributions...");
    var cs_http = new XMLHttpRequest();
    cs_http.onreadystatechange = function() {
      if (cs_http.readyState == 4 && cs_http.status == 200) {
        var items2 = cs_http.responseXML.getElementsByTagName('tokens');
        var t = items2[0].getAttribute('rollbacktoken');
        rollbacktoken = t.replace('+', '%2B');
        $("#CS-interface-footer").append(
    		  $('<button>').attr('id', 'CS-back').css('margin-left','1em').text('Back')
		    );
        $('#CS-back').click(function () {
          user = null, rollbacktoken = null, cs_limit = null, blocked = false;
          enabled = false;
          document.getElementById("CS-back").remove();
          screen00();
        });
        $('#CS-interface-footer').append(
          $('<small>').attr({'id':'SHOWING'}).text(' ')
        );
        checkifblocked();
        get_content();
      }
    };
    cs_http.open("GET", "/w/api.php?action=query&meta=tokens&type=rollback&format=xml");
    cs_http.send();
  	}
  });
};
mw.loader.using(['mediawiki.util', 'mediawiki.api', 'mediawiki.Title'], function() {
  cs_init();
});
//</nowiki>