MediaWiki:Gadget-AllmessagesDeluxe.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./* *********************************************************************
;Fonction :
permet de rechercher dans les messages système ([[Special:Allmessages]]) par mot-clefs (Expressions régulières)
;Installation :
obtenir('AllmessagesDeluxe');
{{Catégorisation JS|AllmessagesDeluxe}}
************************************************************************* */
//<syntaxhighlight lang="javascript"><pre><nowiki>
/* globals mw, $, AllmessagesDeluxe */
////////////////////////////////////////// REQUÊTE DES MESSAGES SYSTÈME ///////////////////////////////////////////
window.AllmessagesDeluxe = {
getMessages: function() {
mw.loader.using('mediawiki.api', function () {
new mw.Api()
.get({action: 'query', meta: 'allmessages', formatversion: 2})
.done(AllmessagesDeluxe.populateMessages);
});
},
populateMessages: function( data ) {
/* jshint sub: true */
var AllMessages = data.query.allmessages;
for(var a=0;a<AllMessages.length;a++) {
var Message = AllMessages[a];
if(Message['missing']) continue;
var MessageTitle = Message['name'];
var MessageContent = Message['content'];
AllmessagesDeluxe.messagesTitles.push(MessageTitle);
AllmessagesDeluxe.messagesContents.push(MessageContent);
}
AllmessagesDeluxe.GetMessagesOK = true;
if(AllmessagesDeluxe.CreatePanelOK) AllmessagesDeluxe.enableForm();
},
////////////////////////////////////////// ACTIVATION DU BOUTON "APPLIQUER" ///////////////////////////////////////////
enableForm: function() {
var Button = document.getElementById("AllmessagesDeluxe_Button");
if(Button){
Button.value = "Appliquer";
Button.disabled = "";
}
},
////////////////////////////////////////// MODIFICATION DU FORMULAIRE DE RECHERCHE ///////////////////////////////////////////
createPanel: function() {
/* jshint laxbreak: true */
var $form = $('#mw-content-text').find('form').first();
if(!$form.length) return;
var $container = $('<table style="width:100%; border-collapse:collapse;">');
var $row = $('<tr>');
var $tdOne = $('<td style="width:50%; padding:0;">');
var $tdTwo = $('<td style="width:50%; padding:0;">');
$container.append($row);
$row.append($tdOne);
$row.append($tdTwo);
$form.replaceWith($container);
$tdOne.append($form);
var newForm = document.createElement('form');
newForm.id = "DeluxeSearchForm";
newForm.method = "post";
newForm.onsubmit = function () {
AllmessagesDeluxe.checkPanel();
return false;
};
newForm.innerHTML = ''
+ '<table style="width:100%; max-width:50em;">'
+ '<tbody>'
+ '<tr>'
+ '<td colspan="2" style="text-align:center;text-weight:bold;" >'
+ '<label for="DeluxeSearchForm" >Rechercher par mot-clef</label>'
+ '</td>'
+ '</tr>'
+ '<tr>'
+ '<td style="text-align:right;">'
+ '<label for="SearchInTitle" >Dans le titre du message : </label>'
+ '</td>'
+ '<td>'
+ '<input id="SearchInTitle" type="text" value="" />'
+ '</td>'
+ '</tr>'
+ '<tr>'
+ '<td style="text-align:right;">'
+ '<label for="SearchInMessage" >Dans le contenu du message : </label>'
+ '</td>'
+ '<td>'
+ '<textarea id="SearchInMessage" value="" ></textarea>'
+ '</td>'
+ '</tr>'
+ '<tr>'
+ '<td>'
+ '</td>'
+ '<td>'
+ '<input id="AllmessagesDeluxe_Button" type="button" value="Appliquer (veuillez patienter)" disabled="disabled" '
+ 'onclick="AllmessagesDeluxe.checkPanel();" onselect="AllmessagesDeluxe.checkPanel();" />'
+ '</td>'
+ '</tr>'
+ '</tbody>'
+ '</table>';
$tdTwo.append(newForm);
AllmessagesDeluxe.CreatePanelOK = true;
if(AllmessagesDeluxe.GetMessagesOK) AllmessagesDeluxe.enableForm();
},
////////////////////////////////////////// VÉRIFICATION DU FORMULAIRE DE RECHERCHE ///////////////////////////////////////////
checkPanel: function() {
var MessageInput = document.getElementById('SearchInMessage').value;
var TitleInput = document.getElementById('SearchInTitle').value;
if((!MessageInput)&&(!TitleInput)) return;
AllmessagesDeluxe.checkMessages(MessageInput, TitleInput);
},
////////////////////////////////////////// RECHERCHE DES MOT-CLEFS DANS LES MESSAGES ///////////////////////////////////////////
checkMessages: function( MessageInput, TitleInput ) {
var RegExpContent = false;
var RegExpTitle = false;
var TitleArray = [];
var MatchedTitleArray = [];
var ContentArray = [];
if(MessageInput) RegExpContent = new RegExp(MessageInput, "g");
if(TitleInput) RegExpTitle = new RegExp(TitleInput, "g");
for(var a=0;a<AllmessagesDeluxe.messagesTitles.length;a++) {
var MessageTitle = AllmessagesDeluxe.messagesTitles[a];
var MessageContent = AllmessagesDeluxe.messagesContents[a];
if(RegExpContent&&RegExpTitle) {
if(MessageTitle.match(RegExpTitle)&&MessageContent.match(RegExpContent)) {
TitleArray.push(MessageTitle);
MatchedTitleArray.push(AllmessagesDeluxe.checkMatches(MessageTitle, RegExpTitle));
ContentArray.push(AllmessagesDeluxe.checkMatches(MessageContent, RegExpContent));
}
} else if(RegExpContent) {
if(MessageContent.match(RegExpContent)) {
TitleArray.push(MessageTitle);
MatchedTitleArray.push(MessageTitle);
ContentArray.push(AllmessagesDeluxe.checkMatches(MessageContent, RegExpContent));
}
} else if(RegExpTitle) {
if(MessageTitle.match(RegExpTitle)) {
TitleArray.push(MessageTitle);
MatchedTitleArray.push(AllmessagesDeluxe.checkMatches(MessageTitle, RegExpTitle));
ContentArray.push(AllmessagesDeluxe.checkMatches(MessageContent));
}
}
}
AllmessagesDeluxe.upDateTable(TitleArray, MatchedTitleArray, ContentArray, false);
},
////////////////////////////////////////// SURLIGNAGE ///////////////////////////////////////////
checkMatches: function( Text, Reg ) {
Text = Text.replace(/</g,"<").replace(/>/g,">");
var Before = "<span style=\"background-color:#FFAAAA;border:1px dotted red;\">";
var After = "</span>";
if(Reg){
var Matches = Text.match(Reg);
Text = Text.replace(Reg, "TRALALA");
for(var a=0;a<Matches.length;a++){
Text = Text.replace(/TRALALA/, Before+Matches[a]+After);
}
}
return Text;
},
////////////////////////////////////////// AFFICHAGE DES RÉSULTATS ///////////////////////////////////////////
upDateTable: function( Titles, MatchedTitles,Contents ) {
var Table = document.getElementById("mw-allmessagestable");
if(!Table) return;
var TableHead = Table.getElementsByTagName('thead')[0];
TableHead.innerHTML = '<tr><th>Nom du message </th><th>Message actuel</th></tr>';
$(Table).find('tbody').remove();
for(var a=0;a<Titles.length;a++){
var NewTbody = document.createElement('tbody');
var NewTR = document.createElement('tr');
var TdTitle = document.createElement('td');
TdTitle.innerHTML = '<a href="'+mw.config.get('wgServer')+mw.config.get('wgArticlePath').split('$1').join(mw.config.get('wgFormattedNamespaces')[8]+":"+Titles[a])+'" >'+MatchedTitles[a]+'</a>';
var TdContent = document.createElement('td');
TdContent.innerHTML = Contents[a];
NewTbody.appendChild(NewTR);
NewTR.appendChild(TdTitle);
NewTR.appendChild(TdContent);
Table.appendChild(NewTbody);
}
}
};
////////////////////////////////////////// LANCEMENT ///////////////////////////////////////////
if(mw.config.get('wgCanonicalSpecialPageName')=="Allmessages"){
AllmessagesDeluxe.GetMessagesOK = false;
AllmessagesDeluxe.CreatePanelOK = false;
AllmessagesDeluxe.messagesTitles = [];
AllmessagesDeluxe.messagesContents = [];
AllmessagesDeluxe.getMessages();
$(AllmessagesDeluxe.createPanel);
}
//</nowiki></pre></syntaxhighlight>