User:MarkusRost/multiupload-rename.js

mw.loader.using(['site']).then(function {	i18n = {		specialpage: "Special:Upload",		multiupload: "Upload multiple files:",		yes: "Yes",		no: "No",		sourcefiles: "Source files:",		destination: "Destination filenames:",		toggle: "Toggle",		uploadfiles: "Upload files",		nofiles: "Please select some files first.",		nolicense: "Please select a valid license first.",		summary: "Summary",		license: "License",		uploading: "Uploading files...",		uploaded: "Uploaded:",		failed: "Failed:",		done: "Done."	};	if (mw.config.get("wgPageName")!==i18n.specialpage) return;	$("#wpUploadFile").parent.parent.addClass("regularFileSelect");	$("tr.regularFileSelect").before(' '+i18n.multiupload+'   '+i18n.yes+'   '+i18n.no+'  '); $("tr.regularFileSelect").after(''+i18n.sourcefiles+'  '); $("#wpDestFile").parent.parent.parent.attr({class:"mw-collapsible mw-collapsed",id:"mw-customcollapsible-multipleFileRename"}); $("#wpDestFile").parent.parent.before(''+i18n.destination+'   ['+i18n.toggle+']    ') $("input[name='wpUpload']").addClass("regularFileSelect"); $("#wpDestFile").parent.parent.addClass("regularFileSelect"); $("#wpIgnoreWarning").parent.parent.addClass("regularFileSelect"); $("span.mw-htmlform-submit-buttons").append(''); $("input[name='multipleFiles']").change(function{		if (this.value===i18n.yes) {			$(".regularFileSelect").hide;			$(".multipleFileSelect").show;			if (!$("#collapse-test").is( ":hidden" )) $(".multipleFileRename").show;		}		else {			$(".regularFileSelect").show;			$(".multipleFileSelect").hide;			$(".multipleFileRename").hide;		}	}); $("input#multiupload").change(async function{		$(".multipleFileRename").remove;		files = this.files;		for (var i = 0; i < files.length; i++) {			try {				var bitmapimage = await createImageBitmap(files[i]);				if (bitmapimage.width>=bitmapimage.height) {					var width = 180;					var height = bitmapimage.height*(180/bitmapimage.width);				} else {					var width = bitmapimage.width*(180/bitmapimage.height);					var height = 180;				}				var canvas = $( '' ).get(0);				canvas.getContext('2d').drawImage(bitmapimage,(180-width)/2,(180-height)/2,width,height);				$("#multipleFileRename").after( $( ' ' ).addClass("multipleFileRename mw-htmlform-field-HTMLTextField mw-collapsible-content").attr({style:($("#collapse-test").is( ":hidden" )?"display:none;":"")}).append(						$( ' ' ).addClass("mw-label").append( $( ' ' ).addClass("thumb").append($( ' ' ).addClass("thumbinner").append( canvas, $( ' ' ).addClass("thumbcaption").append($( ' ' ).addClass("fileinfo").text(bitmapimage.width+' × '+bitmapimage.height+', '+files[i].size+' Bytes')) ))						),						$( ' ' ).addClass("mw-input").append( $( '' ).addClass("webfonts-changed multipleFileRename-Name").val(files[i].name) )					)				);			}			catch {				$("#multipleFileRename").after( $( ' ' ).addClass("multipleFileRename mw-htmlform-field-HTMLTextField mw-collapsible-content").attr({style:($("#collapse-test").is( ":hidden" )?"display:none;":"")}).append(						$( ' ' ).addClass("mw-label"),						$( ' ' ).addClass("mw-input").append( $( '<input size="60" name="multipleFileRename-'+i+'">' ).addClass("webfonts-changed multipleFileRename-Name").val(files[i].name) )					)				);			}		}	});	$("#multiFileSubmit").click(function {		files = $("#multiupload")[0].files;		if (files.length === 0) {			alert(i18n.nofiles);			return false;		}		if ($("#wpLicense option:selected").val === "") {			alert(i18n.nolicense);			return false;		}		names = [];		for (var i = 0; i < files.length; i++) {			names[i] = $('.multipleFileRename-Name[name="multipleFileRename-'+i+'"]').val;		}		summary = $("#wpUploadDescription").val;		if (summary !== "") summary = "== "+i18n.summary+" ==\n"+summary;		license = "== "+i18n.license+" ==\n"+$("#wpLicense option:selected").prop("title");		text = summary + "\n" + license;		watch = "preferences";		if ($("#wpWatchthis").is(":checked")) watch = "watch";		else watch = "nochange";		curFile = 0;		$("#firstHeading").text(i18n.uploading);		$("#mw-content-text").html(" "+i18n.uploaded+" <ul></ul> "+i18n.failed+" <ul></ul> ");		function gNF { if(curFile>files.length) { $("#mw-content-text").append(" "+i18n.done+" "); return; }			if(files[curFile] === undefined) { curFile++; gNF; return; }			$.ajax({url:mw.util.wikiScript('api'),data:{action:'query',meta:'tokens',format:'json'},dataType:'json'}).done(function(data) {				fd = new FormData;				fd.append("action","upload");				fd.append("token",data.query.tokens.csrftoken);				fd.append("filename",names[curFile]);				fd.append("file",files[curFile]);				fd.append("text",text);				fd.append("watchlist",watch);				fd.append("ignorewarnings",1);				fd.append("format","json");				$.ajax({ url:mw.util.wikiScript('api'), method:'POST', data:fd, cache:false, contentType:false, processData:false, type:'POST' }).done(function(d){ if (d.error == undefined) { $("#mw-content-text > ul").append('<li>'+d.upload.filename+'</a></li>'); }			 		else { $("#multiUploadFailed ul").append('<li>'+names[curFile]+'</li>'); $("#multiUploadFailed").show; }					curFile++; gNF; }).fail(function(d) { $("#multiUploadFailed ul").append('<li>'+names[curFile]+'</li>'); $("#multiUploadFailed").show; curFile++; gNF; });			});		}		gNF; }); });