« Utilisateur:Phe/Entete page.js » : différence entre les versions
Contenu supprimé Contenu ajouté
mAucun résumé des modifications |
updated scripts, migrated deprecated functions |
||
Ligne 46 : | Ligne 46 : | ||
var page = Number(r.exec(rh)[1]) + 2; |
var page = Number(r.exec(rh)[1]) + 2; |
||
rh = rh.replace(/\d+/, page); |
rh = rh.replace(/\d+/, page); |
||
header.value += rh; |
header.value += rh; |
||
return true; |
return true; |
||
} |
} |
||
catch(err) { |
catch(err) { |
||
return false; |
return false; |
||
} |
} |
||
}, |
}, |
||
Ligne 74 : | Ligne 74 : | ||
header.value += rh; |
header.value += rh; |
||
return true |
return true |
||
} |
} |
||
catch(err) { |
catch(err) { |
||
return false; |
return false; |
||
} |
} |
||
Ligne 91 : | Ligne 91 : | ||
success = this.increment_arabic(header, rh); |
success = this.increment_arabic(header, rh); |
||
if (!success) |
if (!success) |
||
success = this.increment_roman(header, rh); |
success = this.increment_roman(header, rh); |
||
// if both attempt fail, reuse the previous rh |
// if both attempt fail, reuse the previous rh |
||
if (!success) |
if (!success) |
||
header.value += rh; |
header.value += rh; |
||
var h = document.getElementById("prp_header"); |
var h = document.getElementById("prp_header"); |
||
if (h.style.cssText != '') { |
if (h.style.cssText != '') { |
||
Ligne 141 : | Ligne 141 : | ||
if (header && !r.test(header.value)) { |
if (header && !r.test(header.value)) { |
||
var r = new RegExp("(\\d+)$"); |
var r = new RegExp("(\\d+)$"); |
||
var page = Number(r.exec(wgPageName)[1]); |
var page = Number(r.exec(mw.config.get('wgPageName'))[1]); |
||
running_header.pagename = 'Page:' + wgTitle.replace(/\d+$/g, page - 2); |
running_header.pagename = 'Page:' + mw.config.get('wgTitle').replace(/\d+$/g, page - 2); |
||
running_header.hyphenname = 'Page:' + wgTitle.replace(/\d+$/g, page - 1); |
running_header.hyphenname = 'Page:' + mw.config.get('wgTitle').replace(/\d+$/g, page - 1); |
||
var url = wgServer + wgScriptPath |
var url = mw.config.get('wgServer') + mw.config.get('wgScriptPath') |
||
+ "/api.php?action=query&prop=revisions&callback=running_header.fill_rh&rvprop=content" |
+ "/api.php?action=query&prop=revisions&callback=running_header.fill_rh&rvprop=content" |
||
+ "&format=json&titles=" |
+ "&format=json&titles=" |
||
+ encodeURIComponent(running_header.pagename) + '|' |
+ encodeURIComponent(running_header.pagename) + '|' |
||
+ encodeURIComponent(running_header.hyphenname); |
+ encodeURIComponent(running_header.hyphenname); |
||
mw.loader.load(url); |
|||
} |
} |
||
}, |
}, |
||
addButton2 : function(id, alt, comment, source, onclick) { |
addButton2 : function(id, alt, comment, source, onclick) { |
||
var tb = document.getElementById("toolbar"); |
var tb = document.getElementById("toolbar"); |
||
if(tb) { |
if(tb) { |
||
var image = document.createElement("img"); |
var image = document.createElement("img"); |
||
Ligne 170 : | Ligne 170 : | ||
tb.appendChild(image); |
tb.appendChild(image); |
||
} |
} |
||
}, |
|||
create_script_obj : function (url) { |
|||
var scriptObj = document.createElement("script"); |
|||
scriptObj.setAttribute("type", "text/javascript"); |
|||
scriptObj.setAttribute("src", url); |
|||
document.body.appendChild(scriptObj); |
|||
} |
} |
||
}; |
}; |
||
Ligne 186 : | Ligne 179 : | ||
} |
} |
||
$ |
$(addRhButton); |
||
⚫ | |||
function rmflinks() { |
|||
⚫ | |||
⚫ | |||
* TemplateScript adds configurable templates and scripts to the sidebar, and adds an example regex editor. |
|||
} |
|||
* @see https://meta.wikimedia.org/wiki/TemplateScript |
|||
⚫ | |||
* @update-token [[File:pathoschild/templatescript.js]] |
|||
⚫ | |||
/*$.ajax('//tools-static.wmflabs.org/meta/scripts/pathoschild.templatescript.js', { dataType:'script', cache:true }).then(function() { |
|||
pathoschild.TemplateScript.add([ |
|||
⚫ | |||
]); |
|||
});*/ |
Dernière version du 10 août 2015 à 01:05
/*
* Cherche deux pages avant la page courante pour en extraire un modèle nr
* et l'insère dans la page courante après avoir incrémenté le numéro de page.
* See the FIXME comment(s) for caveats.
*
*/
// Some code here come from left and right, mainly [[User:InductiveLoad]]
// From http://blog.stevenlevithan.com/archives/javascript-roman-numeral-converter
var roman_converter = {
to_roman : function (num) {
if (!+num)
return false;
var digits = String(+num).split(""),
key = ["","C","CC","CCC","CD","D","DC","DCC","DCCC","CM",
"","X","XX","XXX","XL","L","LX","LXX","LXXX","XC",
"","I","II","III","IV","V","VI","VII","VIII","IX"],
roman = "",
i = 3;
while (i--)
roman = (key[+digits.pop() + (i * 10)] || "") + roman;
return Array(+digits.join("") + 1).join("M") + roman;
},
to_int : function (s) {
var str = s.toUpperCase(),
validator = /^M*(?:D?C{0,3}|C[MD])(?:L?X{0,3}|X[CL])(?:V?I{0,3}|I[XV])$/,
token = /[MDLV]|C[MD]?|X[CL]?|I[XV]?/g,
key = {M:1000,CM:900,D:500,CD:400,C:100,XC:90,L:50,XL:40,X:10,IX:9,V:5,IV:4,I:1},
num = 0, m;
if (!(str && validator.test(str)))
return false;
while (m = token.exec(str))
num += key[m[0]];
return num;
}
};
var running_header = {
increment_arabic : function (header, rh){
try {
var r = new RegExp("(\\d+)");
var page = Number(r.exec(rh)[1]) + 2;
rh = rh.replace(/\d+/, page);
header.value += rh;
return true;
}
catch(err) {
return false;
}
},
increment_roman : function (header, rh){
try {
var r = new RegExp("^(.*[\\|] *)([ivxlcm]+|[IVXLCM]+)(\\.? *[\\|}].*)$");
var regex_res = r.exec(rh);
var numeral = regex_res[2];
var lower_case = numeral.toUpperCase() != numeral;
var number = roman_converter.to_int(numeral) + 2;
numeral = roman_converter.to_roman(number);
if (lower_case)
numeral = numeral.toLowerCase();
rh = regex_res[1] + numeral + regex_res[3];
header.value += rh;
return true
}
catch(err) {
return false;
}
},
set_rh : function (header, content) {
var r = new RegExp("{" + "{([Nn]r|[Nn]umérotation) *\\|.*}}");
var match = r.exec(content);
if (match) {
// FIXME: needs to be tweaked, actually works only if the first
// rh parameters consisting of only digits or roman numerals is the one to increment
var rh = match[0];
success = this.increment_arabic(header, rh);
if (!success)
success = this.increment_roman(header, rh);
// if both attempt fail, reuse the previous rh
if (!success)
header.value += rh;
var h = document.getElementById("prp_header");
if (h.style.cssText != '') {
// Sighs ...
//pr_toggle_visibility()
}
}
},
/* Not really a rh but ... */
set_hyphen : function (header, content) {
var wpTextbox1 = document.getElementById("wpTextbox1");
if (wpTextbox1) {
var hyphen = new RegExp("{" + "{([Tt])iret *(\\|[^}]*)}}");
var match = hyphen.exec(content);
if (match)
wpTextbox1.value = match[0].replace(hyphen, "{" + "{$1iret2$2}}") + wpTextbox1.value;
}
},
pagename : null,
hyphenname : null,
fill_rh : function (data) {
var header = document.getElementsByName('wpHeaderTextbox')[0];
if (header && !data.query.pages["-1"]) {
for (var ids in data.query.pages) {
if (data.query.pages[ids].title == running_header.pagename) {
var content = data.query.pages[ids].revisions[0]['*'];
this.set_rh(header, content);
} else if (data.query.pages[ids].title == running_header.hyphenname) {
var content = data.query.pages[ids].revisions[0]['*'];
this.set_hyphen(header, content);
}
}
}
},
set_running_header : function() {
var header = document.getElementsByName('wpHeaderTextbox')[0];
var r = new RegExp("{" + "{([Nn]r|[Nn]umérotation) *\\|.*}}");
if (header && !r.test(header.value)) {
var r = new RegExp("(\\d+)$");
var page = Number(r.exec(mw.config.get('wgPageName'))[1]);
running_header.pagename = 'Page:' + mw.config.get('wgTitle').replace(/\d+$/g, page - 2);
running_header.hyphenname = 'Page:' + mw.config.get('wgTitle').replace(/\d+$/g, page - 1);
var url = mw.config.get('wgServer') + mw.config.get('wgScriptPath')
+ "/api.php?action=query&prop=revisions&callback=running_header.fill_rh&rvprop=content"
+ "&format=json&titles="
+ encodeURIComponent(running_header.pagename) + '|'
+ encodeURIComponent(running_header.hyphenname);
mw.loader.load(url);
}
},
addButton2 : function(id, alt, comment, source, onclick) {
var tb = document.getElementById("toolbar");
if(tb) {
var image = document.createElement("img");
image.width = 23;
image.height = 22;
image.border = 0;
image.className = "mw-toolbar-editbutton";
image.style.cursor = "pointer";
image.alt = alt;
image.title = comment;
image.src = source;
image.onclick = onclick;
tb.appendChild(image);
}
}
};
function addRhButton()
{
running_header.addButton2("wpRh", "Running header auto", "Running header",
"http://upload.wikimedia.org/wikipedia/commons/thumb/b/bc/Button_running_header.svg/23px-Button_running_header.svg.png", running_header.set_running_header);
}
$(addRhButton);
/**
* TemplateScript adds configurable templates and scripts to the sidebar, and adds an example regex editor.
* @see https://meta.wikimedia.org/wiki/TemplateScript
* @update-token [[File:pathoschild/templatescript.js]]
*/
/*$.ajax('//tools-static.wmflabs.org/meta/scripts/pathoschild.templatescript.js', { dataType:'script', cache:true }).then(function() {
pathoschild.TemplateScript.add([
{ name: 'Running header', script:set_running_header }
]);
});*/