Utilisateur:Mabot/monobook.js

Un article de Wikipédia, l'encyclopédie libre.

Note : Après avoir publié la page, vous devrez forcer son rechargement pour voir les changements : Mozilla / Konqueror / Firefox : Shift-Ctrl-R, Internet Explorer / Opera : Ctrl-F5, Safari : Cmd-R.

var debugFlag = true;
 
importScript("Utilisateur:Mabot/featuredTemplateSwitch.js")
 
function wpURLEncode(string) 
{
    if (!string) return "";
    return string.replace(/ /g, "_").replace(/[\x00-\x2C\x3B-\x40\x5B-\x5E\x60\x7B-\uFFFF]/g,
      function (match) {
        var c = match.charCodeAt(0);
        var s = new Array();
        if (c < 0x80) s.push(c);
        else if (c < 0x0800) s.push(c >>  6 | 0xC0, c & 0x3F | 0x80);
        else if (c < 0x010000) s.push(c >> 12 | 0xE0, c >>  6 & 0x3F | 0x80, c & 0x3F | 0x80);
        else if (c < 0x110000) s.push(c >> 18 | 0xF0, c >> 12 & 0x3F | 0x80, c >>  6 & 0x3F | 0x80, c & 0x3F | 0x80);
        for (var i = 0, len = s.length; i < len; i++)
          s[i] = (s[i] < 16 ? "%0" : "%") + s[i].toString(16).toUpperCase();
        return s.join("");
      });
}
 
function getElementById(id) 
{
	var element = null;
	if (this.id == id) 
	{
		element = this;
		element.getElementsByClassName = document.getElementsByClassName;
	}
 
	for (var child = this.firstChild; !element && child; child = child.nextSibling)
		if (child.nodeType == 1) 
		{
			child.getElementById = getElementById;
			element = child.getElementById(id);
		}
 
	return element;
}
 
 
function httpRequest(url, options, data) 
{
    // options.async
    // options.method
    // options.headers
    // options.content
    // options.onSuccess(request,data)
    // options.onFailure(request,data)
    var request= new XMLHttpRequest();
 
    request.onreadystatechange = function() 
	{
		if (request.readyState == 4)
			httpComplete(request, options, data);
	}
 
	request.open(options.method ? options.method : "GET", url, options.async == false ? false : true);
    if (options.headers) 
	{
        for (var field in options.headers)
        	request.setRequestHeader(field, options.headers[field]);
	}
 
    request.send(options.content ? options.content : null);
 
    return request;
}
 
 
function httpComplete(request, options, data) 
{
	if (request.status >= 200 && request.status < 300) 
	{
		if (options.onSuccess) 
		{
			var contentType = request.getResponseHeader("Content-Type");
			var regex = new RegExp(/^([^\/]+\/[^;]+).*/);
			contentType = contentType.replace(regex, "$1");
			if (contentType == "text/html") 
			{
				var doc = document.createElement("div");
				doc.innerHTML = request.responseText;
				doc.getElementsByClassName = document.getElementsByClassName;
				doc.getElementById = getElementById;
				options.onSuccess(doc, data);
			}
			else if (contentType == "text/xml") 
			{
				var xml = new DOMParser();
				xml = xml.parseFromString(request.responseText, 'text/xml');
 
			options.onSuccess(xml, data);
			}
			else 
			{
				options.onSuccess(request, data);
			}
		}
	} 
	else if (options.onFailure) 
	{
		options.onFailure(request, data);
	} 
	else 
	{
		alert(request.statusText);
	}
}
 
function wpEditPage (title, params, editFunction, saveFunction, data) 
{
    var url = wgServer + wgScriptPath + '/index.php?title=' + wpURLEncode(title)
            + '&action=edit';
 
    for (var name in params)
      url += '&' + name + '=' + wpURLEncode(params[name]);
 
    var options = { onSuccess: wpSavePage };
 
    data.title = title;
    data.editFunction = editFunction;
    data.saveFunction = saveFunction;
 
    httpRequest(url, options, data);
}
 
function wpSavePage(doc, data) 
{
    var inputs = doc.getElementById('editform').getElementsByTagName('input');
    var editform = new Array();
    for (var i = 0, len = inputs.length; i < len; i++) 
    {
    	var value = inputs[i].value
    	if(inputs[i].type != "checkbox" || inputs[i].checked) 
		   	editform[inputs[i].name] = value;
    }
 
    var wpTextbox1 = doc.getElementById('wpTextbox1').value;
    var wpSummary = editform['wpSummary'];
 
    var regex = new RegExp(/var wgArticleId = "(\d+)";(?:\n.*)*var wgCurRevisionId = "(\d+)";/m);
    var match = regex.exec(doc.textContent);
    if (match) {
      data.pageId = match[1];
      data.curRevId = match[2];
    }
 
    var edit = data.editFunction(wpTextbox1, wpSummary, doc, data);
 
    if (typeof(edit.error) == "undefined") { 
      editform['wpTextbox1'] = edit.wpTextbox1;
      editform['wpSummary'] = edit.wpSummary;
 
      var params = [ 'wpTextbox1', 'wpSummary', 'wpSection',
                     'wpStarttime', 'wpEdittime', 'wpEditToken', 'wpSave' ];
 
      if(editform['wpWatchthis']) params.push('wpWatchthis')
      var content = params.map(function(name) { return name + '=' + encodeURIComponent(editform[name]); }).join("&");
 
      var url = wgServer + wgScriptPath + '/index.php?title=' + wpURLEncode(data.title)
              + '&action=submit';
 
      var headers = new Array();
      headers['Content-Type'] = 'application/x-www-form-urlencoded';
 
      var options = { method: 'POST', headers: headers, content: content, onSuccess: data.saveFunction };
 
      data = edit.data;
 
      httpRequest(url, options, data);
    } else {
      edit.error(edit.data);
    }
}
 
 
function jsonRequest(url, type, handler, data)
{
    var request= new XMLHttpRequest();
 	var res = [];
 
 	var doRequest = function(extraUrl)
 	{
 		setTimeout(function()
 		{
 			console.log(res.length)
			request.open("GET", wgServer + wgScriptPath + "/" + url + "&format=json" + extraUrl , true);
	    	request.send( null);
	    }
	    ,10)
 	}
 
 	var onreadystatechange = function() 
	{
		if (request.readyState == 4)
			if (request.status >= 200 && request.status < 300) 
			{
				var dum = eval ("(" + request.responseText + ")")
 
				for(var i in dum.query[type])
					res[i] = dum.query[type][i]
 
				if(dum["query-continue"])
				{
					var ex = ""
					for(var val in dum["query-continue"][type])
						for(var val in dum["query-continue"][type])
							ex = ex + "&" + val + "=" + dum["query-continue"][type][val]
 
					doRequest(ex)
				}
				else
					handler(res, data)
			}
			else
			{
				console.error("request error : " + request.status)
			}
	}
 
 	request.onreadystatechange = onreadystatechange	
 	doRequest("")
 
    return request;
}
 
 
 
function getPageListUsingTemplate(templateName, handler)
{
	var url = "api.php?action=query&list=embeddedin&eilimit=500&eititle=" + encodeURIComponent(templateName)
 
	if(debugFlag) url = url + "&einamespace=2"
 
	jsonRequest(url, "embeddedin", handler)	
}
 
function processList(list, handler, delay)
{
	if(!delay) delay = 30
	var foo = function()
	{
		for(var i in list)
		{
			var actionMaded = handler(list[i])
			delete list[i]	
 
			if(actionMaded)
				break
		}
 
		setTimeout(foo, delay*1000)
	}
 
	foo()
}
 
 
function modifyPage(title, handler, summary)
{
	var editFunction = function(wpTextbox1, wpSummary, doc, data)
	{
		var res = {}
		res.wpTextbox1 = handler(wpTextbox1)
		res.wpSummary = summary
 
		return res
	}
 
	if(debugFlag && title.indexOf("Utilisateur:Mabot/")!=0) return false
 
	wpEditPage(title, {}, editFunction, function(){}, {})
 
	return true
}