User:Dr Brains/sandbox.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. |
This user script seems to have a documentation page at User:Dr Brains/sandbox. |
var MainCategory = "Category:All free media";
function AddLink(){
var OngletsCactions = document.getElementById('p-cactions');
if(OngletsCactions){
var CactionsUl = OngletsCactions.getElementsByTagName('ul')[0];
CactionsUl.innerHTML += '<li><a href="javascript:List();">List '+MainCategory+'</a></li>';
if(OngletsCactions.className){
OngletsCactions.className = OngletsCactions.className.replace(/[ ]*emptyPortlet[ ]*/, " ");
}
}
}
var ajax = {
http:function(bundle){ var xmlhttp; try{ xmlhttp = new XMLHttpRequest(); }catch(e){ try{ xmlhttp = new ActiveXObject("Msxml2.XMLHTTP"); }catch(e){ try{ xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); }catch(e){ xmlhttp = false; }}}; if(xmlhttp){ xmlhttp.onreadystatechange = function(){ if (xmlhttp.readyState == 4){ ajax.httpComplete(xmlhttp,bundle);}}; xmlhttp.open(bundle.method ? bundle.method : "GET",bundle.url,bundle.async == false ? false : true); if (bundle.headers) { for (var field in bundle.headers){ try{ xmlhttp.setRequestHeader(field,bundle.headers[field]); }catch(err){}}}; xmlhttp.send(bundle.data ? bundle.data : null); }; return xmlhttp;},
httpComplete: function(xmlhttp,bundle){ if(xmlhttp.status == 200 || xmlhttp.status == 302){ if(bundle.onSuccess) bundle.onSuccess(xmlhttp,bundle); }else if(bundle.onFailure){ bundle.onFailure(xmlhttp,bundle); }else{ }}
};
var PageInCategory = new Array();
var PageInCategoryCount = 0;
var CategoryInCategory = new Array();
var CategoryInCategoryCount = 0;
var IndexCategory = -1;
var APILimit = 499;
if( (wgUserGroups.indexOf("sysop")!=-1)||(wgUserGroups.indexOf("bot")!=-1)){
APILimit = 4999;
}
function List(category, categorycontinue){
if(!category) category = MainCategory;
if(!categorycontinue) categorycontinue = '';
var URL = mw.config.get('wgServer') + mw.config.get('wgScriptPath') + '/api.php?format=xml&action=query&list=categorymembers&cmlimit='+APILimit+'&cmtitle=' + encodeURIComponent(MainCategory) + '&cmcontinue=' + encodeURIComponent(categorycontinue);
prompt("URL :" , URL);
ajax.http({
url: URL,
cat: MainCategory,
onSuccess:ListDone
});
}
function ListDone(Req, data){
var ElementTraitement = Req.responseXML;
var Pages = ElementTraitement.getElementsByTagName('cm');
for(a=0;a<Pages.length;a++){
var PageNamespace = parseInt(Pages[a].getAttribute('ns'));
if((PageNamespace!=6)&&(PageNamespace!=14)) continue;
var TitrePage = Pages[a].getAttribute('title');
if((TitrePage.indexOf(wgFormattedNamespaces[14]+ ':')!=-1)){
if(CategoryInCategory.indexOf(TitrePage)==-1){
CategoryInCategory[CategoryInCategoryCount] = TitrePage;
CategoryInCategoryCount++
}
}else if(TitrePage.indexOf(wgFormattedNamespaces[14] + ':')==-1){
TitrePage = TitrePage+"|[[:"+TitrePage+"]]";
if(PageInCategory.indexOf(TitrePage)==-1){
PageInCategory[PageInCategoryCount] = TitrePage;
PageInCategoryCount++
}
}
}
var MustContinue = ElementTraitement.getElementsByTagName('query-continue')[0];
if(MustContinue && MustContinue.fistChild){
var AutreRequeteContinue = MustContinue.fistChild.getAttribute("cmcontinue");
List(data.cat, AutreRequeteContinue);
}else{
IndexCategory++
var NewCategory = CategoryInCategory[IndexCategory];
if(NewCategory){
List(NewCategory);
}else{
PageInCategory = PageInCategory.sort()
DisplayList();
}
}
}
function DisplayList(){
var Header = "Il y a "+PageInCategory.length+" fichiers dans la catégorie "+MainCategory+" :";
var List = "<gallery>\n"+PageInCategory.join("\n")+"\n</gallery>";
var Notice = document.getElementById("siteNotice");
var P = document.createElement('p');
P.appendChild(document.createTextNode(Header));
var TextArea = document.createElement('textarea');
TextArea.rows = 5;
TextArea.value = List;
var Div = document.createElement('div');
Div.appendChild(P);
Div.appendChild(TextArea);
Notice.parentNode.insertBefore(Div, Notice);
}
addOnloadHook(AddLink);