Utilisateur:Karima Rafes/Gadget-Recompense.js
Note : après avoir enregistré la page, vous devrez forcer le rechargement complet du cache de votre navigateur pour voir les changements.
Mozilla / Firefox / Konqueror / Safari : maintenez la touche Majuscule (Shift) en cliquant sur le bouton Actualiser (Reload) ou pressez Maj-Ctrl-R (Cmd-R sur Apple Mac) ;
Firefox (sur GNU/Linux) / Chrome / Internet Explorer / Opera : maintenez la touche Ctrl en cliquant sur le bouton Actualiser ou pressez Ctrl-F5./* (version 1.0 du 4 août 2012) par Karima Rafes et inspiré par MediaWiki:Gadget-Evaluation.js
* {{Projet:JavaScript}}
*
*/
//<source lang=javascript>//<pre><nowiki>
if(mw.config.get('wgNamespaceNumber')==0 && mw.config.get('wgAction') == "view") addOnloadHook(recompense_addEvalTab)
var _GET;
var _DiffUserRecompense;
var _RecompenseDiv;
var _RecPage;
function recompense_addEvalTab()
{
try {if(alwaysEvalRecompense){recompense_launchEval()}}
catch(e){ }
}
function recompense_launchEval()
{
var req=new XMLHttpRequest()
var div = document.getElementById('bodyContent') // Monobook et affiliés, Vector
if(!div) div = document.getElementById('mw_contentholder'); // Moderne
if(!div) div = document.getElementById('article'); // Bleu de Cologne, Standard, Nostalgia
if(!div) return;
var dummy = new Date().getTime()
if (document.URL.match(/&printable/))
{
/* version imprimable */
return;
}
recompense_Init();
if(_DiffUserRecompense === undefined)
return;
_RecPage = _DiffUserRecompense + "/Recompenses";
_RecompenseDiv = document.getElementById('RecompenseDiv')
if(!_RecompenseDiv)
{
_RecompenseDiv= document.createElement('div');
_RecompenseDiv.id = "RecompenseDiv";
_RecompenseDiv.align="center";
div.parentNode.insertBefore(_RecompenseDiv, div);
}
if(_DiffUserRecompense !== undefined){
_RecompenseDiv.innerHTML = "Interrogation de la page de récompense de " + mw.config.get('wgScript')+"?title="+_RecPage;
req.onreadystatechange = function()
{
if(req.readyState == 4)
{
if(req.status==200)
{
var textHTML = recompense_Form(req.responseText);
_RecompenseDiv.innerHTML =textHTML ;
}
else if(req.status==404) //la pdd n'existe pas
{
var textHTML = recompense_Form("");
_RecompenseDiv.innerHTML =textHTML ;
}
}//fin readyState == 4
}
req.open("GET",mw.config.get('wgScript')+"?title="+ _RecPage + "&action=raw&_dum=" + dummy, true);
req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
req.send(null);
}
}
function recompense_Init()
{
/*
//Init Get*************
var get = new Array();
var uri = location.href;
var temp_get_arr = uri.substring(_uri.indexOf('?')+1, uri.length).split("&");
var temp_get_arr_1 = new Array();
for(get_arr_i=0; get_arr_i<temp_get_arr.length; get_arr_i++){
temp_get_arr_1 = temp_get_arr[get_arr_i].split("=");
get[decodeURI(temp_get_arr_1[0])] = decodeURI(temp_get_arr_1[1]);
}
delete uri; delete temp_get_arr; delete temp_get_arr_1;
//Init global variable
_GET = get;
*/
//Init User *********
var userDiv= document.getElementById('mw-diff-ntitle2');
if(userDiv)
{
var link = userDiv.getElementsByTagName('a');
_DiffUserRecompense = link[0].title;
}
}
function recompense_Extract(text){
var re = /\{\{Récompense pour un élève\/([^|]*)\|signature=--\[\[Utilisateur:[^|]*\|([^\]]*)[^\}]*\}\}/g;
var textHtml,condition ,checked,title;
var rec = new Array();
while (match = re.exec(text)) {
rec[match[1]]=match[2];
}
return rec;
}
function recompense_Form(text){
var rec = recompense_Extract(text);
textHtml = '<form><div style="width:660px;">';
condition = "Sources" in rec;
checked = condition ?'checked':'';
title = condition ? 'Attribué par ' + rec["Sources"]:'';
textHtml += '<div style="float: left;"><img src="http://upload.wikimedia.org/wikipedia/commons/thumb/e/e8/Wikipedia-Student-Source.svg/60px-Wikipedia-Student-Source.svg.png" title="'+title+'" /><br/><input type="checkbox" name="recompense" value="Sources" '+checked +'/></div>';
condition = "Gnoll" in rec;
checked = condition ?'checked':'';
title = condition ? 'Attribué par ' + rec["Gnoll"]:'';
textHtml += '<div style="float: left;"><img src="http://upload.wikimedia.org/wikipedia/commons/thumb/d/d0/Wikipedia-Student-Gnoll.svg/60px-Wikipedia-Student-Gnoll.svg.png" title="'+title+'" /><br/><input type="checkbox" name="recompense" value="Gnoll" '+checked +'/></div>';
condition = "Patrouilleur" in rec;
checked = condition ?'checked':'';
title = condition ? 'Attribué par ' + rec["Patrouilleur"]:'';
textHtml += '<div style="float: left;"><img src="http://upload.wikimedia.org/wikipedia/commons/thumb/f/f1/Wikipedia-Student-Patrol.svg/60px-Wikipedia-Student-Patrol.svg.png" title="'+title+'" /><br/><input type="checkbox" name="recompense" value="Patrouilleur" '+checked +'/></div>';
condition = "Modèle" in rec;
checked = condition ?'checked':'';
title = condition ? 'Attribué par ' + rec["Modèle"]:'';
textHtml += '<div style="float: left;"><img src="http://upload.wikimedia.org/wikipedia/commons/thumb/f/f6/Wikipedia-Student-Template.svg/60px-Wikipedia-Student-Template.svg.png" title="'+title+'" /><br/><input type="checkbox" name="recompense" value="Modèle" '+checked +'/></div>';
condition = "Rédacteur" in rec;
checked = condition ?'checked':'';
title = condition ? 'Attribué par ' + rec["Rédacteur"]:'';
textHtml += '<div style="float: left;"><img src="http://upload.wikimedia.org/wikipedia/commons/thumb/1/10/Wikipedia-Student-Writer.svg/60px-Wikipedia-Student-Writer.svg.png" title="'+title+'" /><br/><input type="checkbox" name="recompense" value="Rédacteur" '+checked +'/></div>';
condition = "Fée" in rec;
checked = condition ?'checked':'';
title = condition ? 'Attribué par ' + rec["Fée"]:'';
textHtml += '<div style="float: left;"><img src="http://upload.wikimedia.org/wikipedia/commons/thumb/d/dd/Wikipedia-Student-Fairy.svg/60px-Wikipedia-Student-Fairy.svg.png" title="'+title+'" /><br/><input type="checkbox" name="recompense" value="Fée" '+checked +'/></div>';
condition = "Maintenance" in rec;
checked = condition ?'checked':'';
title = condition ? 'Attribué par ' + rec["Maintenance"]:'';
textHtml += '<div style="float: left;"><img src="http://upload.wikimedia.org/wikipedia/commons/thumb/b/bb/Wikipedia-Student-Cleaner.svg/60px-Wikipedia-Student-Cleaner.svg.png" title="'+title+'" /><br/><input type="checkbox" name="recompense" value="Maintenance" '+checked +'/></div>';
condition = "Gnome" in rec;
checked = condition ?'checked':'';
title = condition ? 'Attribué par ' + rec["Gnome"]:'';
textHtml += '<div style="float: left;"><img src="http://upload.wikimedia.org/wikipedia/commons/thumb/9/93/Wikipedia-Student-Gnome.svg/60px-Wikipedia-Student-Gnome.svg.png" title="'+title+'" /><br/><input type="checkbox" name="recompense" value="Gnome" '+checked +'/></div>';
condition = "Média" in rec;
checked = condition ?'checked':'';
title = condition ? 'Attribué par ' + rec["Média"]:'';
textHtml += '<div style="float: left;"><img src="http://upload.wikimedia.org/wikipedia/commons/thumb/4/4f/Wikipedia-Student-Camera.svg/60px-Wikipedia-Student-Camera.svg.png" title="'+title+'" /><br/><input type="checkbox" name="recompense" value="Média" '+checked +'/></div>';
condition = "Fourmi" in rec;
checked = condition ?'checked':'';
title = condition ? 'Attribué par ' + rec["Fourmi"]:'';
textHtml += '<div style="float: left;"><img src="http://upload.wikimedia.org/wikipedia/commons/thumb/0/03/Wikipedia-Student-Ant.svg/60px-Wikipedia-Student-Ant.svg.png" title="'+title+'" /><br/><input type="checkbox" name="recompense" value="Fourmi" '+checked +'/></div>';
condition = "N'hésitez pas" in rec;
checked = condition ?'checked':'';
title = condition ? 'Attribué par ' + rec["N'hésitez pas"]:'';
textHtml += '<div style="float: left;"><img src="http://upload.wikimedia.org/wikipedia/commons/thumb/e/ef/Wikipedia-Degree.svg/60px-Wikipedia-Degree.svg.png" title="'+title+'" /><br/><input type="checkbox" name="recompense" value="N\'hésitez pas" '+checked +'/></div>';
textHtml += '</div><br style="clear:both;"><div><input type="button" id="RecompenseButton" onclick="recompense_setRecompense();" value="Modifier"/></div></form>';
textHtml += "<div><a href='"+mw.config.get('wgScript')+"?title="+_RecPage + "' >Voir la page récompense :"+ _DiffUserRecompense +"</a> - ";
textHtml += "<a href='"+mw.config.get('wgScript')+"?title=Wikipédia:Projets pédagogiques/Récompenses pour les élèves' >Définitions des récompenses</a></div>";
return textHtml;
}
function recompense_setRecompense() {
document.getElementById("RecompenseButton").disabled = true;
wpEditPage(_RecPage, {}, recompense_editFoo, recompense_saveFoo, {});
}
function recompense_change(text)
{
var rec = recompense_Extract(text);
var expreg;
var expregText;
var newtext = text;
var nameModel;
recCheckbox = document.getElementsByName("recompense");
for(var i=0; i < recCheckbox.length; i++){
nameModel = recCheckbox[i].value;
expregText= "{{Récompense pour un élève/"+nameModel +"[^}]*}}";
expreg= new RegExp(expregText, "g");
if(recCheckbox[i].checked ){
if(!(recCheckbox[i].value in rec))
newtext = "{{Récompense pour un élève/"+nameModel +"|signature=--~~~~}}\n" + newtext ;
}else{
newtext = newtext.replace(expreg,"");
}
}
// _RecompenseDiv.innerHTML += test + "<br/>Avant:" + text +"<br/>Apres:" + newtext ;
return newtext;
}
function recompense_editFoo(text, summary, doc, data)
{
summary = "[[Utilisateur:Karima_Rafes/Gadget-Recompense.js|Récompense]]: "+Recompense_getSummary();
text = recompense_change(text);
return { wpTextbox1: text, wpSummary: summary, data: data };
}
function recompense_saveFoo()
{
document.getElementById("RecompenseButton").disabled = false
}
function Recompense_getSummary()
{
return "Modification par le script Gadget-Recompense";
}
/////////////////////////////////////////////////////////from tichou
// URL encode
if (typeof(wpURLEncode) == "undefined") {
wpURLEncode = function(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("");
});
}
}
// getElementById
if (typeof(getElementById) == "undefined") {
getElementById = function(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;
}
}
// HTTP
if (typeof(httpRequest) == "undefined") {
httpRequest = function(url, options, data) {
// options.async
// options.method
// options.headers
// options.content
// options.onSuccess(request,data)
// options.onFailure(request,data)
var request;
try {
request = new XMLHttpRequest();
} catch(e) {
try {
request = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try {
request = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e) {
request = false;
}
}
}
if (request) {
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;
}
}
if (typeof(httpComplete) == "undefined") {
httpComplete = function(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 = null;
if (navigator.appName == "Microsoft Internet Explorer") {
xml = new ActiveXObject("Microsoft.XMLDOM");
xml.async = false;
xml.loadXML(request.responseText);
} else if (window.DOMParser) {
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);
}
}
}
// Edit WP page
if (typeof(wpEditPage) == "undefined") {
wpEditPage = function(title, params, editFunction, saveFunction, data) {
var url = mw.config.get('wgServer') + mw.config.get('wgScript') + '?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);
}
}
// Save WP page
if (typeof(wpSavePage) == "undefined") {
wpSavePage = function(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 = mw.config.get('wgServer') + mw.config.get('wgScript') + '?title=' + wpURLEncode(data.title)
+ '&action=submit';
var headers = new Object();
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);
}
}
}
var wpfunctions = 1;
//</nowiki></pre></source>