Utilisateur:EDUCA33E/searchreplace.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 urlsearch = window.location.search;
if (urlsearch != '') {
    data = urlsearch.substr(1, urlsearch.length - 1);
    parameters = data.split('&');
    urlvariables = new Array();
    for (var i = 0; i < parameters.length; i++) {
        parameter = parameters[i]
        pos = parameter.indexOf('=');
        variable = parameter.substr(0, pos);
        value = decodeURI(parameter.substring(pos + 1, parameter.length));
        while (value.search(/\+/) != -1)
            value = value.replace(/\+/, ' ');
        urlvariables[variable] = value;
    }
}
 
function Replace(diff) {
    var form = document.forms['tc-searchreplace'];
    var search = form.elements['tc-search'].value;
    var replace = form.elements['tc-replace'].value;
    var form = document.forms['tc-resume'];
    var resume = form.elements[0].value;
    if(search == '' || replace == '') return false;
 
    form = document.forms['editform'];
    var text = form.elements['wpTextbox1'];
//    text.value = text.value.replace(new RegExp(/\[{2}(:)?[Cc]at[ée]gor(y|ie)[ ]?:[ ]?([^\]\|]*)/g), "[[$1Catégorie:"+"{{"+"subst:ucfirst:$3"+"}}");
    text.value = text.value.replace(new RegExp(search, "g"), replace);
//  form.elements['wpSummary'].value += ' ' + resume + ' s/'+search+'/'+replace+'/';
    form.elements['wpSummary'].value = resume + ' s/'+search+'/'+replace+'/';
    if (diff) {
        form.elements['wpDiff'].click();
    } else {
        form.elements['wpSave'].click();
    }
    return false;
}
 
function SaveCookie(name, value) {
    var expires = new Date();
    expires.setTime(expires.getTime() + 365*24*3600*1000);
    expires = expires.toGMTString();
    document.cookie = name + '=' + escape(value) + '; expires=' + expires + '; path=/; domain=wikipedia.org;';
}
 
function GetCookie(name) {
    var cookies = document.cookie;
    if (cookies) {
        var start = cookies.indexOf(name + '=');
        if (start != -1) {
            var end = cookies.indexOf(";", start);
            if (end == -1) end = cookies.length;
            return unescape(cookies.substring(start + name.length + 1, end));
        }
    }
    return '';
}
 
function SearchPrompt() {
    var search = prompt('Chaîne à remplacer ?');
    if (search) {
        var replace = prompt('À remplacer par ?');
        if (!replace && replace != '') return;
        SaveCookie('TCsearch', search);
        SaveCookie('TCreplace', replace);
    }
}
 
function SearchReplace() {
//  if (document.title.indexOf('Modification de ') == 0) {
        var toolbar = document.getElementById('toolbar'); 
        if (toolbar) {
        var br = document.createElement('br');
        toolbar.appendChild(br);
 
        var form = document.createElement('form');
        form.setAttribute('action', '');
        form.setAttribute('method', 'get');
        form.setAttribute('enctype', 'text/plain');
        form.setAttribute('id', 'tc-resume');
        form.setAttribute('style', 'display: inline; vertical-align: bottom;');
 
        var select = document.createElement('select');
        select.setAttribute('onchange', 'SaveCookie(\'TCresume\', this.value)');
 
        var option = document.createElement('option');
        option.value = option.text = 'Orthographe';
        select.appendChild(option);
 
        var option = document.createElement('option');
        option.value = option.text = 'Typographie';
        select.appendChild(option);
 
        var option = document.createElement('option');
        option.value = option.text = 'Anglicisme';
        select.appendChild(option);
 
        var option = document.createElement('option');
        option.value = option.text = 'Grammaire';
        select.appendChild(option);
 
        var option = document.createElement('option');
        option.value = option.text = 'Correction';
        select.appendChild(option);
 
        var option = document.createElement('option');
        option.value = option.text = 'Ponctuation';
        select.appendChild(option);
 
        var option = document.createElement('option');
        option.value = option.text = 'Ajout';
        select.appendChild(option);
 
        var option = document.createElement('option');
        option.value = option.text = 'Suppression';
        select.appendChild(option);
 
        var option = document.createElement('option');
        option.value = option.text = 'Recatégorisation';
        select.appendChild(option);
 
        var option = document.createElement('option');
        option.value = option.text = 'Suppression catégorie';
        select.appendChild(option);
 
        var option = document.createElement('option');
        option.value = option.text = 'Retouche';
        select.appendChild(option);
 
        var option = document.createElement('option');
        option.value = option.text = 'Lien';
        select.appendChild(option);
 
        var option = document.createElement('option');
        option.value = option.text = 'Fair use';
        select.appendChild(option);
 
        var option = document.createElement('option');
        option.value = option.text = 'Métamodèle';
        select.appendChild(option);
 
        var option = document.createElement('option');
        option.value = option.text = 'Recommandation Commons';
        select.appendChild(option);
 
        var option = document.createElement('option');
        option.value = option.text = 'Désormais sur Commons';
        select.appendChild(option);
 
        select.value = GetCookie('TCresume');
 
        form.appendChild(select);
        toolbar.appendChild(form);
 
        var form = document.createElement('form');
        form.setAttribute('action', '');
        form.setAttribute('method', 'get');
        form.setAttribute('enctype', 'text/plain');
        form.setAttribute('onsubmit', 'return Replace(1)');
        form.setAttribute('id', 'tc-searchreplace');
        form.setAttribute('style', 'display: inline; vertical-align: middle;');
 
        var text = document.createTextNode(' s/');
        form.appendChild(text);
 
        var input = document.createElement('input');
        input.setAttribute('type', 'text');
        input.setAttribute('size', '10');
        input.setAttribute('name', 'tc-search');
        input.setAttribute('id', 'tc-search');
        input.setAttribute('value', GetCookie('TCsearch'));
        input.setAttribute('title', 'Chaîne à remplacer ?');
        input.setAttribute('onchange', 'SaveCookie(\'TCsearch\', this.value)');
        form.appendChild(input);
 
        text = document.createTextNode('/');
        form.appendChild(text);
 
        input = document.createElement('input');
        input.setAttribute('type', 'text');
        input.setAttribute('size', '10');
        input.setAttribute('name', 'tc-replace');
        input.setAttribute('id', 'tc-replace');
        input.setAttribute('value', GetCookie('TCreplace'));
        input.setAttribute('title', 'À remplacer par ?');
        input.setAttribute('onchange', 'SaveCookie(\'TCreplace\', this.value)');
        form.appendChild(input);
 
        text = document.createTextNode('/ ');
        form.appendChild(text);
 
        input = document.createElement('input');
        input.setAttribute('type', 'image');
        input.setAttribute('src', 'http://upload.wikimedia.org/wikipedia/commons/9/96/Button_trait%C3%A9supprimer.png');
        input.setAttribute('alt', 'Rechercher, remplacer et prévisualiser les changements');
        input.setAttribute('style', 'vertical-align: middle;');
        form.appendChild(input);
 
        toolbar.appendChild(form);
 
        var img = document.createElement('img');
        img.setAttribute('src', 'http://upload.wikimedia.org/wikipedia/commons/a/ae/Button_trait%C3%A9conserver.png');
        img.setAttribute('alt', 'Rechercher, remplacer et sauvegarder les changements');
        img.setAttribute('style', 'vertical-align: middle;');
 
        var a = document.createElement('a');
        a.setAttribute('href', 'javascript:Replace(0)'); 
        a.appendChild(img); 
 
        toolbar.appendChild(a);
 
        if (urlvariables['searchreplace'] == 'save') Replace(0);
        if (urlvariables['searchreplace'] == 'diff') Replace(1);
    }
 
 //   if (document.getElementById('ca-nstab-main').getAttribute('class') == "selected") {
 
        var tabs = document.getElementById('p-cactions').getElementsByTagName('ul')[0];
        if (!tabs) return;
 
        var li = document.createElement('li');
        li.setAttribute('id', 'ca-tc-searchreplace');
 
/*      var font = document.createElement('font');
        font.setAttribute('style', 'color: blue; font-weight: bold; padding-left: 0.5em');
        var text = document.createTextNode('s&r');
        font.appendChild(text);
        li.appendChild(font); */
 
        var a = document.createElement('a');
        a.setAttribute('href', 'javascript:SearchPrompt()'); 
        a.setAttribute('title', 'Saisir les termes de recherche et de remplacement');
        a.setAttribute('style', 'color: blue; font-weight: bold;');
        text = document.createTextNode('s&r');
        a.appendChild(text);
        li.appendChild(a);
 
        var editlink = document.getElementById('ca-edit').getElementsByTagName('a')[0].getAttribute('href');
        var a = document.createElement('a');
        a.setAttribute('href', editlink + '&searchreplace=diff'); 
        a.setAttribute('title', 'Rechercher, remplacer et prévisualiser les changements');
        a.setAttribute('style', 'color: red; font-weight: bold;');
        text = document.createTextNode('D');
        a.appendChild(text);
        li.appendChild(a);
 
        a = document.createElement('a');
        a.setAttribute('href', editlink + '&searchreplace=save'); 
        a.setAttribute('title', 'Rechercher, remplacer et sauvegarder les changements');
        a.setAttribute('style', 'color: green; font-weight: bold;');
        text = document.createTextNode('S');
        a.appendChild(text);
        li.appendChild(a);
 
        var a = document.createElement('a');
        a.setAttribute('href', 'javascript:SearchHighlight()'); 
        a.setAttribute('title', 'Mettre en surbrillance les termes à remplacer');
        a.setAttribute('style', 'color: yellow; font-weight: bold;');
        text = document.createTextNode('H');
        a.appendChild(text);
        li.appendChild(a);
 
        tabs.insertBefore(li, document.getElementById('ca-history'));
//    }
}
 
addLoadEvent(SearchReplace);
 
// <b style="color:black;background-color:#ffff66">layer</b>