Misplaced Pages

User:Andrea105/monobook.js

Article snapshot taken from Wikipedia with creative commons attribution-sharealike license. Give it a read and then ask your questions in the chat. We can research this topic together.
< User:Andrea105
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump.
This code will be executed when previewing this page.
The accompanying .css page for this skin is at User:Andrea105/monobook.css.
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 Misplaced Pages:Bypass your cache.
importScript('User:Lupin/recent2.js');

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca;
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}


function eraseCookie(name) {
	createCookie(name,"",-1);
}

function warningtab()
{
  document.getElementById('column-one').getElementsByTagName('div').getElementsByTagName('ul').appendChild(tnaddlilink("javascript:markpage()", "pt"));
  if(document.URL.indexOf("User_talk") != -1 && document.URL.indexOf("edit") != -1)
  {
     document.getElementById('column-one').getElementsByTagName('div').getElementsByTagName('ul').appendChild(tnaddlilink("javascript:uwtest1()", "t1"));
     document.getElementById('column-one').getElementsByTagName('div').getElementsByTagName('ul').appendChild(tnaddlilink("javascript:uwvand2()", "w2"));
     document.getElementById('column-one').getElementsByTagName('div').getElementsByTagName('ul').appendChild(tnaddlilink("javascript:uwvand4()", "w4"));
  }
  if( ((document.URL.indexOf("&from=") != -1) && (document.URL.indexOf("&token=") != -1)) || (document.URL.indexOf("&autoclick=wpSave") != -1) )
  {
    markpage();
  }
}

function uwtest1()
{
  document.editform.wpTextbox1.value += ( "\n===" + "{" + "{" + "subst:" + "CURRENTMONTHNAME" + "}" + "}" + " {" + "{" + "subst:" + "CURRENTYEAR" + "}" + "}" + "===" );
  document.editform.wpTextbox1.value += ( "\n" + "{" + "{" + "subst:" + "uw-test1\|" + readCookie("pageattacked") + "}" + "}" + " ~" + "~" + "~" + "~" );
  document.editform.wpSummary.value = "uw-test1";
  document.editform.wpSave.click();
}

function uwvand2()
{
  document.editform.wpTextbox1.value += ( "\n===" + "{" + "{" + "subst:" + "CURRENTMONTHNAME" + "}" + "}" + " {" + "{" + "subst:" + "CURRENTYEAR" + "}" + "}" + "===" );
  document.editform.wpTextbox1.value += ( "\n" + "{" + "{" + "subst:" + "uw-vand2\|" + readCookie("pageattacked") + "}" + "}" + " ~" + "~" + "~" + "~" );
  document.editform.wpSummary.value = "uw-vand2";
  document.editform.wpSave.click();
}

function uwvand4()
{
  document.editform.wpTextbox1.value += ( "\n===" + "{" + "{" + "subst:" + "CURRENTMONTHNAME" + "}" + "}" + " {" + "{" + "subst:" + "CURRENTYEAR" + "}" + "}" + "===" );
  document.editform.wpTextbox1.value += ( "\n" + "{" + "{" + "subst:" + "uw-vand4\|" + readCookie("pageattacked") + "}" + "}" + " ~" + "~" + "~" + "~" );
  document.editform.wpSummary.value = "uw-vand4";
  document.editform.wpSave.click();
}

function markpage()
{
  createCookie("pageattacked", wgPageName.replace(/_/g, ' '), 2);
}

$(warningtab);

function tnaddlilink(url, name)
{
  var na = document.createElement('a');
  na.setAttribute('href', url);
 
  var txt = document.createTextNode(name);
  na.appendChild(txt);
 
  var li = document.createElement('li');
  li.appendChild(na);
  return li;
}

//ARV 2.0
// -----------------------------------------------------------------------------
// XMLHttpRequest support
// -----------------------------------------------------------------------------
if (document.implementation.createDocument) {
  var gml_xmlparser = new DOMParser();
}

function gml_XMLParse(string) {
  if (document.implementation.createDocument) {
    return gml_xmlparser.parseFromString(string, "text/xml");
  } else if (window.ActiveXObject) {
    var gml_xmldoc = new ActiveXObject("Microsoft.XMLDOM");
    gml_xmldoc.async = "false";
    ret = gml_xmldoc.loadXML(string);      
    if (!ret)
      return null;
    return gml_xmldoc.documentElement;
  }
  return null;
}

var gml_xmlhttp;

function HTTPClient() {
  var gml_http;
  if(window.XMLHttpRequest) {
    gml_http = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    try {
      gml_http = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        gml_http = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (E) {
        gml_http = false;
      }
    }
  }
  return gml_http;
}
var AIVpageName = 'Misplaced Pages:Administrator_intervention_against_vandalism';
var section = '1';
var vandal;
var reason;

function addlilinkAIV(url, name, id){
    addPortletLink( 'p-cactions', url, name, id, null, null );
}

function AIV(){
  if (document.title.indexOf("User talk:") != -1){
    username = encodeURIComponent( wgTitle );
    var test = username.split('.');
    if(test) {
    addlilinkAIV('javascript:AIVandal("' + username + '")', 'Report IP', 'td-report');
    }
    else {
    addlilinkAIV('javascript:AIVandal("' + username + '")', 'Report', 'td-report');
    }
  }
  if (document.title.indexOf("User contributions") != -1){
    username = location.href.split('/');
    if(username == undefined || username == ""){
      //if this is the case, then something is up
      if(location.href.indexOf("&target=")){
        //it looks like we came here from a target link, lets just take the username from that
        username = location.href.split('=');
      }
    }
    addlilinkAIV('javascript:AIVandal("' + username + '")', 'Report', 'td-report');
  }
}

function AIVandal(vandalU){
      vandal = vandalU;
      if(vandal ==  document.getElementById('pt-userpage').getElementsByTagName('a').innerHTML){
        alert('You don\'t want to report yourself, do you?');
        return;
      }
      reason = prompt('Reason?:');
      /* if(!reason)
        return; */
      document.getElementById('bodyContent').innerHTML = 'Please wait' + '...';
      gml_xmlhttp = HTTPClient();
      if (!gml_xmlhttp)
        return;
      document.getElementById('bodyContent').innerHTML += '<br />Grabbing page...';
      gml_xmlhttp.open('GET', 'http://en.wikipedia.org/search/?title=' + AIVpageName + '&action=edit' + '&section=' + section + '', true);
      gml_xmlhttp.onreadystatechange = AIVMore;
      gml_xmlhttp.send(null);
}

function AIVparams( arr ) {
	var resarr = Array();
	for( var i in arr ) {
		if( typeof arr == 'undefined' ) {
			continue;
		}
		var res;
		if( arr instanceof Array ){
			var v =  Array();
			for(var j = 0; j < arr.length; ++j ) {
				v = encodeURIComponent( arr );
			}
			res = v.join('|');
		} else {
			res = encodeURIComponent( arr );
		}
		resarr.push( encodeURIComponent( i ) + '=' + res );
	}
	return resarr.join('&');
}

function AIVMore() {
  if (gml_xmlhttp.readyState != 4){
    document.getElementById('bodyContent').innerHTML += '.';
    return
  }

  // Insert the downloaded form in our current page, using
  // only hidden form inputs.
  var doc = gml_XMLParse(gml_xmlhttp.responseText);
  var form = doc.getElementById('editform');

  var x;
  var t = form.wpTextbox1.value;
  var myArray = t.split(" ");
  var add = true;
  document.getElementById('bodyContent').innerHTML += '<br />Searching for existing report...';
  var test = "*{{vandal|" + vandal + "}}";
  for (x=0; x < myArray.length; x++)
  {
    var myArray2 = myArray.split(/\n/);
    var y;
    for (y=0; y < myArray2.length; y++ ){
      if(myArray2 == '{{vandal|' + vandal + '}}'){
        add = false;
      }
      if(myArray2 == test){
        add = false;
      }
      if(myArray2 == '{{IPvandal|' + vandal + '}}'){
        add = false;
      }
    }
  }
  if(add == true){
    document.getElementById('bodyContent').innerHTML += '<br />No report found, adding...';
  }
  else{
    //alert('CSD tag found');
    document.getElementById('bodyContent').innerHTML += '<br />Report found, will not add...';
    return;
  }
  //let's do something expiremntal, to see if it's an IP address
    var test = username.split('.');
    if(test) {
      form.wpTextbox1.value += '*{{IPvandal|' + vandal + '}} - ' + reason + ' --~' + '~' + '~' + '~';
    }
    else {
    form.wpTextbox1.value += '*{{vandal|' + vandal + '}} - ' + reason + ' --~' + '~' + '~' + '~';
    }

  var params = {
    action : 'edit',
    title : AIVpageName,
    section : section,
    summary : 'Reporting ] using ].',
    minor : null,
    text : form.wpTextbox1.value,
    token : form.wpEditToken.value
  }

  document.getElementById('bodyContent').innerHTML += '<br />Submitting form...';

  var xmlhttp = sajax_init_object();
  xmlhttp.overrideMimeType('application/json');
  xmlhttp.open( 'POST' , mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php', true);
  xmlhttp.setRequestHeader('Content-type','application/x-www-form-urlencoded');
  xmlhttp.send( AIVparams(params) );
}

$(AIV);
User:Andrea105/monobook.js Add topic