Utilisateur:Juli1.Co/Scripts-RLR.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.function write_page(pagename, text, summary, timestamp)
{
var api = new mw.Api();
return api.postWithEditToken( {
action: 'edit',
text: text,
summary: summary,
title: pagename,
basetimestamp: timestamp
} );
}
/* return true if the most left html tag is a red link */
function first_red_link(li)
{
var a = li.getElementsByTagName('a');
return a.length > 0 && a[0].className === 'new';
}
/* return true if a part of the element is striked */
function striked_element(e)
{
return e.getElementsByTagName('s').length != 0;
}
function add_submit_button(funct)
{
$('#bodyContent .mw-editsection').each(function(){
var submit = document.createElement('button');
submit.type = 'submit';
submit.innerHTML = 'submit';
submit.onclick = funct;
$(this).append($(submit));
});
}
function get_clickodrome_config()
{
var page = mw.config.get('wgPageName');
var slash = page.indexOf('/');
if (slash === -1)
return null;
var root_page = page.substr(0, slash);
var sub_page = page.substr(slash + 1);
var config = {autodelete_red_links: false, two_levels: false, strike_allowed: true, skip_header: false};
if (root_page === 'Projet:Restauration_lien_rouge') {
if (/^par_distance\/([A-Z]|autres)$/.test(sub_page)) {
config.skip_header = true;
config.autodelete_red_links = true;
return config;
} else if (/^par_distance\/Exceptions\/([A-Z]|autres)\/[0-9]+$/.test(sub_page)) {
return config;
}
} else if (root_page === 'Projet:Restauration_des_ancres_brisées') {
if (/^([A-Z]|Autres|0-9|Multiple)$/.test(sub_page)) {
return config;
}
} else if (root_page === "Projet:Remplissage_des_pages_d'homonymie") {
if (/^[0-9]+$/.test(sub_page)) {
config.two_levels = true;
return config;
} else if (sub_page == 'Exceptions') {
config.strike_allowed = false;
return config;
}
}
return null;
}
function get_li_to_fix(config)
{
var content = $('#bodyContent');
var main_lists_containers = content;
if (config.skip_header) {
// Skips list items in the header.
main_lists_containers = main_lists_containers.find('h2').first().nextAll();
}
return main_lists_containers.find('li:not(#catlinks *)');
}
function get_list_level(line)
{
for (var i = 0; i < line.length; i++)
if (line[i] !== '*')
return i;
return line.length;
}
// Removes top-level list items without nested list items.
function remove_li_without_sublist(lines)
{
var new_lines = [];
for (var i = 0; i < lines.length; i++)
if (get_list_level(lines[i]) !== 1 || (i + 1 < lines.length && get_list_level(lines[i + 1]) > 1))
new_lines.push(lines[i]);
return new_lines;
}
function clickodrome_change_text(text, config)
{
var content = document.getElementById('bodyContent');
var input = content.getElementsByTagName("input");
var li = get_li_to_fix(config);
if (li.length * 2 != input.length) {
alert('bad li/input count ' + li.length + ' ' + input.length)
return text;
}
var lines = text.split('\n');
var new_lines = [];
var k = 0;
for (var j = 0; j < lines.length; ++j) {
var line = lines[j];
var list_level = get_list_level(line);
if (list_level > 0) {
if (k >= li.length) {
alert('something feel bad, k >= input.length');
return text;
}
var delete_line = input[k * 2 + 1].checked;
var strike_line = input[k * 2].checked;
k++;
if (delete_line) {
continue;
} else if (strike_line && line.indexOf('<s>') == -1) {
line = line.slice(0, list_level) + '<s>' + line.slice(list_level) + '</s>';
}
}
new_lines.push(line);
}
if (k * 2 != input.length) {
alert('k != input.length' + k + ' ' + input.length);
return text;
}
if (config.two_levels) {
new_lines = remove_li_without_sublist(new_lines);
}
return new_lines.join('\n');
}
function clickodrome_submit(config)
{
var api = new mw.Api();
api.get({
'action': 'query',
'prop': 'revisions',
'revids': mw.config.get( 'wgRevisionId' ),
'rvprop': 'content|timestamp',
'rvslots': 'main',
'formatversion': 2
}).done(function(data) {
var revision = data.query.pages[0].revisions[0];
var old_content = revision.slots.main.content;
var new_content = clickodrome_change_text(old_content, config);
if (new_content === old_content) {
alert('Pas de changement à enregistrer');
return;
}
write_page(mw.config.get('wgPageName'), new_content, 'mise à jour', revision.timestamp).done(function() {
window.location = window.location;
}).fail(function(code) {
alert('La mise à jour de la page a échoué (code "' + code + '")');
});
});
}
function init_clickodrome()
{
if (typeof disable_clickodrome != 'undefined' && disable_clickodrome)
return;
var config = get_clickodrome_config();
if (!config)
return
var li = get_li_to_fix(config);
for (var j = 0; j < li.length; j++) {
var input = document.createElement('input');
input.type = 'checkbox';
if (striked_element(li[j])) {
input.checked = striked_element(li[j]);
input.disabled = true;
} else {
input.disabled = !config.strike_allowed;
}
var input2 = document.createElement('input');
input2.type = 'checkbox';
input2.checked = config.autodelete_red_links && !first_red_link(li[j]);
input2.className = 'del-checkbox';
li[j].insertBefore(input2, li[j].firstChild);
li[j].insertBefore(input, li[j].firstChild);
if (config.two_levels) {
$(input2).change(function() {
children = $(this.parentElement).find('li .del-checkbox');
children.prop('checked', this.checked);
children.prop('disabled', this.checked);
});
}
/* Add Open Link */
if (!striked_element(li[j])) {
var openLink = document.createElement('a');
openLink.text = '[[open]]';
openLink.style.marginLeft = '15px';
openLink.style.cursor = 'pointer';
openLink.style.verticalAlign = 'text-bottom';
$(openLink).click(function(e){
e.preventDefault();
var sublinks = $(this.parentElement).children('a[href]');
window.open(sublinks.last()[0].href, '_blank');
window.open(sublinks.last().prev()[0].href, '_blank');
});
li[j].appendChild(openLink, li[j].lastChild);
}
}
add_submit_button( function() {
clickodrome_submit(config);
} );
}
if (mw.config.get('wgAction') == 'view' && mw.config.get('wgArticleId') != 0) {
var modules = [ 'mediawiki.api' ];
$.when(
mw.loader.using( modules ),
$.ready
).then( function() {
init_clickodrome();
} );
}