User:Bill william compton/HotInterwiki.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
![]() | Documentation for this user script can be added at User:Bill william compton/HotInterwiki. |
// <nowiki>
/* {{Projet:JavaScript/Script|HotInterwiki}} */
/*
Gadget de retrait/modification/ajout de lien inter-langue
* Documentation : [[Projet:JavaScript/Notices/HotInterwiki]]
*/
//<source lang=javascript>//<pre>//<nowiki>
// ***********************************************
// Variables
//////////////////////
// Textes
var HotInterwiki_Text = new Array();
HotInterwiki_Text["Autres langues"] = "Other languages";
HotInterwiki_Text["(-)"] = "(-)";
HotInterwiki_Text["ConfirmRemove"] = "Do you want to remove [[$1]] interwiki link?";
HotInterwiki_Text["SummaryRemove"] = "[[$1]] interwiki link removed";
HotInterwiki_Text["(±)"] = "(±)";
HotInterwiki_Text["ConfirmModify"] = "Voulez-vous vraiment modifier le lien inter-langue [[$1]] -> [[$2]] ?";
HotInterwiki_Text["SummaryModify"] = "Interwiki link changed [[$1]] → [[$2]]";
HotInterwiki_Text["(+)"] = "(+)";
HotInterwiki_Text["ConfirmAdd"] = "Voulez-vous vraiment ajouter le lien inter-langue [[$1]] ?";
HotInterwiki_Text["SummaryAdd"] = "Added interwiki link [[$1]]";
HotInterwiki_Text["IntputOK"] = "OK";
HotInterwiki_Text["IntputCancel"] = "Cancel";
HotInterwiki_Text["InterwikiTemplate"] = "$1:$2";
HotInterwiki_Text["InterwikiError"] = "Interwiki non valide";
//////////////////////
// Paramètres
var HotInterwiki_Param = new Array();
HotInterwiki_Param["AutoSubmit"] = true;
//////////////////////
// Autres variables
var HotInterwiki_Matrix = new Array();
HotInterwiki_Matrix['li'] = new Array();
HotInterwiki_Matrix['interwiki'] = new Array();
HotInterwiki_Matrix['form'] = new Array();
// ***********************************************
// Lancement
if(wgNamespaceNumber>-1&&document.URL.indexOf("printable=yes")==-1&&wgAction!="history"){
// Importation styles CSS
importStylesheetURI('http://fr.wikipedia.org/w/index.php?title=Utilisateur:Dr Brains/HotInterwiki.css&action=raw&ctype=text/css');
// Lancement
addOnloadHook(HotInterwiki_Init);
}
// ***********************************************
// Initialisation
function HotInterwiki_Init(){
if(typeof(HotInterwiki_LangCustom)=="function") HotInterwiki_LangCustom();
if(typeof(HotInterwiki_SiteCustom)=="function") HotInterwiki_SiteCustom();
if(typeof(HotInterwiki_UserCustom)=="function") HotInterwiki_UserCustom();
var PLang = document.getElementById('p-lang');
if(!PLang) PLang = HotInterwiki_CreatePLang();
if(!PLang) return;
HotInterwiki_ModifyUl(PLang);
HotInterwiki_AddIWLi(PLang);
}
// ***********************************************
// Création cadre "Autres langues"
function HotInterwiki_CreatePLang(){
try{
var Target = document.getElementById('p-tb');
var TargetClassName = Target.className
var TargetBodyClassName = Target.getElementsByTagName('div')[0].className;
}catch(e){
return false;
}
var PLang = document.createElement('div');
PLang.id = "p-lang";
PLang.className = TargetClassName
var H5Lang = document.createElement('h5');
H5Lang.appendChild(document.createTextNode(HotInterwiki_Text["Autres langues"]));
var BodyLang = document.createElement('div');
BodyLang.className = TargetBodyClassName;
BodyLang.appendChild(document.createElement('ul'));
PLang.appendChild(H5Lang);
PLang.appendChild(BodyLang);
Target.parentNode.appendChild(PLang);
return PLang;
}
// ***********************************************
// Liens de retrait ou de modification d'un interwiki
function HotInterwiki_ModifyUl(PLang){
var Ul = PLang.getElementsByTagName('ul')[0];
if(!Ul) return;
var Lis = Ul.getElementsByTagName('li');
for(var a=0,l=Lis.length;a<l;a++){
var Li = Lis[a];
HotInterwiki_Matrix['li'][a] = Li;
Li.id = "Interwiki_"+a;
var Link = Li.getElementsByTagName('a')[0];
var LinkText = Link.title;
var Lang = Li.className.split("interwiki-").join("");
HotInterwiki_Matrix['interwiki'][a] = Lang + ":" + LinkText;
var SpanLinks = document.createElement('span');
var RemoveLink = document.createElement('a');
RemoveLink.href = "javascript:HotInterwiki_RemoveIW("+a+");";
RemoveLink.appendChild(document.createTextNode(HotInterwiki_Text["(-)"]));
var ModifyLink = document.createElement('a');
ModifyLink.href = "javascript:HotInterwiki_CreateForm("+a+");";
ModifyLink.appendChild(document.createTextNode(HotInterwiki_Text["(±)"]));
SpanLinks.appendChild(document.createTextNode(" "));
SpanLinks.appendChild(RemoveLink);
SpanLinks.appendChild(document.createTextNode(" "));
SpanLinks.appendChild(ModifyLink);
Li.appendChild(SpanLinks);
}
}
// ***********************************************
// Lien d'ajout d'un interwiki
function HotInterwiki_AddIWLi(PLang){
var Ul = PLang.getElementsByTagName('ul')[0];
if(!Ul) return;
var AddLi = document.createElement('li');
AddLi.id = "Interwiki_1000";
var AddLink = document.createElement('a');
AddLink.appendChild(document.createTextNode(HotInterwiki_Text["(+)"]));
AddLink.href = "javascript:HotInterwiki_CreateForm(1000);";
AddLi.appendChild(AddLink);
Ul.appendChild(AddLi);
HotInterwiki_Matrix['li'][1000] = AddLi;
HotInterwiki_Matrix['interwiki'][1000] = "";
}
// ***********************************************
// Retrait d'un interwiki
function HotInterwiki_RemoveIW(LiID){
var Li = document.getElementById("Interwiki_"+LiID);
var Lang = Li.className.split("interwiki-").join("");
var Link = Li.getElementsByTagName('a')[0];
var Text = Link.title;
var Interwiki = HotInterwiki_Text["InterwikiTemplate"].split('$1').join(Lang).split('$2').join(Text)
var SurEtCertain = confirm(HotInterwiki_Text["ConfirmRemove"].split('$1').join(Interwiki));
if(SurEtCertain) HotInterwiki_Edit(Interwiki, false);
}
// ***********************************************
// Création formulaire de modification ou d'ajout d'interwiki
function HotInterwiki_CreateForm(Index){
var Li = document.getElementById("Interwiki_"+Index);
if(!Li) return;
var Link = Li.getElementsByTagName('a')[0];
var Span = Li.getElementsByTagName('span')[0];
if(Link) Link.style.display = "none";
if(Span) Span.style.display = "none";
var Form = document.createElement('form');
Form.style.display = "inline";
Form.id = "InterwikiForm_"+Index;
Form.method = "post" ;
Form.onsubmit = function () {
var FormIndex = HotInterwiki_getIndex(this);
HotInterwiki_OK(FormIndex);
return false;
} ;
var InputText = document.createElement('input');
InputText.type = "text";
InputText.value = HotInterwiki_Matrix['interwiki'][Index];
var InputOK = document.createElement('input');
InputOK.type = "button";
InputOK.id = "InterwikiOKInput_"+Index;
InputOK.className = "InterwikiInput";
InputOK.value = HotInterwiki_Text["IntputOK"];
InputOK.onclick = function(){
var FormIndex = HotInterwiki_getIndex(this);
HotInterwiki_OK(FormIndex);
}
InputOK.onselect = function(){
var FormIndex = HotInterwiki_getIndex(this);
HotInterwiki_OK(FormIndex);
}
var InputCancel = document.createElement('input');
InputCancel.type = "button";
InputCancel.id = "InterwikiOKInput_"+Index;
InputCancel.className = "InterwikiInput";
InputCancel.value = HotInterwiki_Text["IntputCancel"];
InputCancel.onclick = function(){
var FormIndex = HotInterwiki_getIndex(this);
HotInterwiki_Cancel(FormIndex);
}
InputCancel.onselect = function(){
var FormIndex = HotInterwiki_getIndex(this);
HotInterwiki_Cancel(FormIndex);
}
Form.appendChild(InputText);
Form.appendChild(InputOK);
Form.appendChild(InputCancel);
Li.appendChild(Form);
HotInterwiki_Matrix['form'][Index] = Form;
}
// ***********************************************
// Validation du formulaire de modification ou d'ajout d'interwiki
function HotInterwiki_OK(FormIndex){
var Li = HotInterwiki_Matrix['li'][FormIndex];
var Form = HotInterwiki_Matrix['form'][FormIndex];
var OldText = HotInterwiki_Matrix['interwiki'][FormIndex];
var NewText = Form.getElementsByTagName('input')[0].value;
if(NewText == "") return;
var IWRegExp = new RegExp('^([a-z][a-z].?(x?-[^\\]]+)?|simple|tokipona):([^\\]]*)$');
if(NewText.match(IWRegExp)==null){
alert(HotInterwiki_Text["InterwikiError"]);
return;
}
var ConfirmText;
if(OldText == ""){
ConfirmText = HotInterwiki_Text["ConfirmAdd"].split('$1').join(NewText);
OldText = false;
}else{
ConfirmText = HotInterwiki_Text["ConfirmModify"].split('$1').join(OldText).split('$2').join(NewText);
}
var SurEtCertain = confirm(ConfirmText);
if(SurEtCertain) HotInterwiki_Edit(OldText, NewText);
}
// ***********************************************
// Annulation du formulaire de modification ou d'ajout d'interwiki
function HotInterwiki_Cancel(FormIndex){
var Li = HotInterwiki_Matrix['li'][FormIndex];
var Form = HotInterwiki_Matrix['form'][FormIndex];
Form.parentNode.removeChild(Form);
var Link = Li.getElementsByTagName('a')[0];
var Span = Li.getElementsByTagName('span')[0];
if(Link) Link.style.display = "";
if(Span) Span.style.display = "";
}
// ***********************************************
// Initialisation édition
function HotInterwiki_Edit(IWtoRemove, IwToAdd){
var URL = mw.config.get('wgServer') + mw.config.get('wgScript') + '?title=' + wgPageName.replace(/&/g, "%26") + '&action=edit';
var NouvelleRequete = sajax_init_object();
NouvelleRequete.open("GET", URL, true);
NouvelleRequete.onreadystatechange = function() {
if(NouvelleRequete.readyState != 4) return;
while(document.body.firstChild){document.body.removeChild(document.body.firstChild);}
document.body.innerHTML = NouvelleRequete.responseText;
HotInterwiki_DoEdit(IWtoRemove, IwToAdd);
}
NouvelleRequete.send(null);
}
// ***********************************************
// Édition
function HotInterwiki_DoEdit(IWtoRemove, IwToAdd){
var OldText = document.getElementById("wpTextbox1").value;
var IWRegExp = new RegExp('^\\s*\\[\\[([a-z][a-z].?(x?-[^\\]]+)?|simple|tokipona):([^\\]]*)\\]\\]\\s*$');
var blankRegExp = new RegExp('^\\s*$');
var lines = OldText.split('\n');
var BeforeInterwikis = new Array();
var Interwikis = new Array();
var FoundInterwiki = false;
for (var lineId = lines.length - 1; lineId >= 0; --lineId){
if (!IWRegExp.exec(lines[lineId]) && !blankRegExp.exec(lines[lineId])){
BeforeInterwikis = lines.slice(0, lineId + 1);
Interwikis = lines.slice(lineId + 1);
break;
}
}
BeforeInterwikis = BeforeInterwikis.join('\n') + '\n';
Interwikis = Interwikis.join('\n');
var Summary = "";
if(IWtoRemove==false && IwToAdd!=false){ // ************ Add
if(Interwikis!=""){
Interwikis = Interwikis.replace(/^\n/, "");
Interwikis = Interwikis.split('\n');
Interwikis.push("[["+IwToAdd+"]]");
Interwikis = Interwikis.sort();
Interwikis = "\n" + Interwikis.join('\n');
}else{
Interwikis = "\n[["+IwToAdd+"]]\n";
}
Summary = HotInterwiki_Text["SummaryAdd"].split('$1').join(IwToAdd);
}else if(IWtoRemove!=false && IwToAdd==false){ // ****** Remove
Interwikis = Interwikis.split("[["+IWtoRemove+"]]\n").join("");
Summary = HotInterwiki_Text["SummaryRemove"].split('$1').join(IWtoRemove);
}else if(IWtoRemove!=false && IwToAdd!=false){ // ****** Modify
Interwikis = Interwikis.split("[["+IWtoRemove+"]]\n").join("[["+IwToAdd+"]]\n");
Interwikis = Interwikis.replace(/^\n/, "");
Interwikis = Interwikis.split('\n');
Interwikis = Interwikis.sort();
Interwikis = "\n" + Interwikis.join('\n');
Summary = HotInterwiki_Text["SummaryModify"].split('$1').join(IWtoRemove).split('$2').join(IwToAdd);
}
document.getElementById("wpTextbox1").value = BeforeInterwikis+Interwikis;
document.getElementById("wpSummary").value = Summary;
if(HotInterwiki_Param["AutoSubmit"]) document.editform.submit();
}
// ***********************************************
// Récupère le n° d'index d'un élément
function HotInterwiki_getIndex(Element){
return parseInt(Element.id.replace(/[^0-9]/g, ""));
}
//</nowiki></pre></source>
// </nowiki>