User:Alex brollo/common.js

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Note: After saving, you have to bypass your browser's cache to see the changes. Internet Explorer: press Ctrl-F5, Mozilla: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Opera/Konqueror: press F5, Safari: hold down Shift + Alt while clicking Reload, Chrome: hold down Shift while clicking Reload.
// attivazione test di ipt
mw.loader.load('//it.wikisource.org/w/index.php?title=User:Alex brollo/ipt.js&action=raw&ctype=text/javascript');
// api.php?action=query&list=categorymembers&cmtitle=Category:Illustrations from Iliade (Romagnoli)&cmtype=file&cmlimit=500
importScript('User:Rillke/bigChunkedUpload.js');
/* 
La funzione costruisce e scrive in edit il codice gallery relativo ai files di una categoria di immagini (max 500);
eventuale codice gallery preesistente viene sovrascritto

Test function, it builds and writes the gallery code for files into a category; it writes the code into current page, or updates it if already present. 
The aim is, to use that code into Index:Talk wikisource pages. 
*/
function gallery() {
	var categoria=prompt("Categoria:");
	if (categoria==="") categoria=mw.config.get("wgPageName");
	var t="";
	testo="";
	var galleryText="";
	var api = new mw.Api();
	api.get( {
	    action: 'query',
	    list: 'categorymembers',
	    cmtitle:categoria,
	    cmtype: "file",
	    cmlimit:"500"
	} ).done( function ( data ) {
	    var pagine=data.query.categorymembers;
	    
	    	
	    for (i=0;i<pagine.length;i+=1){
	    	t+=pagine[i].title+"|"+pagine[i].title+"\n";
	    }
	    t="<pre><gallery>\n"+t+"</gallery></pre>";
	    testo=$("#wpTextbox1").val();
	    galleryText=testo.match(/<pre>[\s\S].*?<\/pre>/ );
	    if (galleryText===null) $("#wpTextbox1").val(testo+"\n\n"+t);
	    else $("#wpTextbox1").val(testo.replace(galleryText,t));
	    
	} );
}