User:452/DragDropUploader

//<!-- // Just fuck off. // People like you are why I don't help others anymore. // People like you make me regret EVER helping ANYONE. // So just FUCK OFF.

$(function {  if (!$("#mw-upload-form").length || !(mw.util.getParamValue('DragDrop') || window.DragDropUploader) || $("#dropAreaOuter").length || mw.config.get('wgUserId') != 3403151) return;  $(".hideforDragDrop").remove;

console.log("DragDropUploader v 1.4.25.4"); var reuploadMessage = ""; if ($("#wpForReUpload").val) reuploadMessage = "Dropping a single file here will upload it as: \""+$("#wpDestFile").val+"\" Multiple ";

$("#mw-upload-form").before(' ');

var uploadDiv = document.getElementById("dropAreaOuter"); window.DragDrop = { batchCounter:1, busy:0, uploaderQueue:[], }

if( $("#mw-upload-permitted").length && $("#mw-upload-permitted").html.indexOf(" Permitted file types:") != -1) { DragDrop.allowedFiles = $("#mw-upload-permitted").html.split(" Permitted file types: ")[1].split(".")[0].split(", "); } else { DragDrop.allowedFiles = new Array("png", "gif", "jpg", "jpeg", "ico", "pdf", "svg", "odt", "ods", "odp", "odg", "odc", "odf", "odi", "odm", "ogg", "ogv", "oga"); }

function sendNext { if (DragDrop.busy) return; if (!DragDrop.uploaderQueue.length) return; xhr = DragDrop.uploaderQueue.shift; xhr.open("post", $("#mw-upload-form").attr("action")); xhr.send(xhr.form); DragDrop.busy = 1; }

function makeUploadForm(newForm, whichBatch, whichFile) { var xhr = new XMLHttpRequest; xhr.whichBatch = whichBatch; xhr.whichFile = whichFile; xhr.form = newForm;

$("#statusArea").prepend(' '); $("#batch"+xhr.whichBatch).html('Status of batch '+xhr.whichBatch+': Queued. ('+xhr.whichFile+')');

xhr.onload = function { $("#errorArea").prepend(" "); $("#errorArea").html($("#errorArea").html.replace(/ /g," "));

if (xhr.status == 200) { $("#batch"+xhr.whichBatch).html('Status of batch '+xhr.whichBatch+': Complete. ('+xhr.status+') Uploaded: "'+xhr.whichFile+'".');

if ($(".warning", xhr.response).length) {                                                      //Warning $("#errorArea").prepend($(".warning", xhr.response)); $("#batch"+xhr.whichBatch).addClass("warning"); }       else if ($(".mw-destfile-warning", xhr.response).length) {                                      //Warning $("#errorArea").prepend($(".mw-destfile-warning", xhr.response)); $("#errorArea .wikia-photogallery-add").remove; $("#batch"+xhr.whichBatch).addClass("warning"); }       else if ($(".mw-destfile-error", xhr.response).length) {                                        //Error $("#errorArea").prepend($(".mw-destfile-error", xhr.response)); $("#batch"+xhr.whichBatch).addClass("error"); $("#batch"+xhr.whichBatch).addClass("error2"); }       else if ($(".error", xhr.response).length) {                                                    //Error $("#errorArea").prepend($(".error", xhr.response)); $("#batch"+xhr.whichBatch).addClass("error"); $("#batch"+xhr.whichBatch).addClass("error2"); }       else if ($(".fullImageLink", xhr.response).length) {                                            //Success if ($("#successArea").html) $("#successArea").prepend(" "); $("#successArea").prepend(""+xhr.whichFile+""); $("#successArea").html($("#successArea").html.replace(/\: File uploaded\./g,"")); } else {                                                                                       //Unknown $("#errorArea").prepend("Non-specific error for batch "+xhr.whichBatch+". Please upload the file manually."); $("#batch"+xhr.whichBatch).addClass("error"); $("#batch"+xhr.whichBatch).addClass("error4"); }     } else {                                                                                          //HTTP error $("#errorArea").prepend('Error: '+xhr.status+' for batch '+xhr.whichBatch+'. Automatically resubmitting files.'); $("#batch"+xhr.whichBatch).addClass("error"); $("#batch"+xhr.whichBatch).addClass("error5"); DragDrop.uploaderQueue.push(xhr); }     DragDrop.busy = 0; sendNext; };   xhr.upload.onprogress = function (event) { if (event.lengthComputable) { $("#batch"+xhr.whichBatch).html("Status of batch "+xhr.whichBatch+': '+(event.loaded / event.total * 100 | 0)+"% of 1 file uploaded."); if ((event.loaded / event.total) == 1) $("#batch"+xhr.whichBatch).html("Status of batch "+xhr.whichBatch+': '+(event.loaded / event.total * 100 | 0)+"% of 1 file uploaded. Waiting for response from server."); }   };    DragDrop.uploaderQueue.push(xhr); sendNext; }

uploadDiv.addEventListener("dragover", function(event) {   event.preventDefault;  }, false); uploadDiv.addEventListener("drop", function(event) {   event.preventDefault;    var files = event.dataTransfer.files,        totalsize = 0,        multiform = new Array,        batchlimit = 1,        filecount = 0;

if (!files.length) return; //dragging text

if ($("#statusArea").text == "This is the Status area") { $("#historyArea").html(" "); $("#errorArea").html(""); $("#successArea").html(""); $("#statusArea").html(""); }

if ($("#successArea").html && $("#wpForReUpload").val) { var rereupload = confirm("You have already replaced this file, are you sure?"); if (rereupload != true) return; }

if ($("#historyArea").html) $("#historyArea table").prepend("  "); for (var i = 0; i < files.length; i++) { $("#historyArea table").prepend(" " + files[i].name + "  " + files[i].type +" " + files[i].size + "  "); totalsize += files[i].size;

if (DragDrop.allowedFiles.indexOf(files[i].name.split(".").pop.toLowerCase) == -1) { $("#errorArea").prepend("Invalid filetype: "+files[i].name+"</li></ul>"); $("#statusArea").prepend(" Error: Invalid filetype "); continue; }     //local errors must be caught by this point. //i is the index for files dropped. //filecount is the index for files being sent.

var j = DragDrop.batchCounter;

if (!multiform[j]) { multiform[j] = new FormData; multiform[j].append("title", "Special:Upload"); multiform[j].append("wpUpload", "Upload file"); multiform[j].append("wpEditToken", $("#wpEditToken").val); multiform[j].append("wpUploadDescription", $("#wpUploadDescription").val +" "); if ($("#wpLicense").val) multiform[j].append("wpLicense", $("#wpLicense").val); if ($("#wpWatchthis:checked").length) multiform[j].append("wpWatchthis", $("#wpWatchthis").val); if ($("#wpIgnoreWarning:checked").length) multiform[j].append("wpIgnoreWarning", $("#wpIgnoreWarning").val); if ($("#wpForReUpload").val && (files.length == 1)) multiform[j].append("wpForReUpload", 1); }

multiform[j].append("wpUploadFile", files[i]); if ($("#wpForReUpload").val && (files.length == 1)) multiform[j].append("wpDestFile", $("#wpDestFile").val); else multiform[j].append("wpDestFile", files[i].name);

filecount++; if (filecount == batchlimit) { // filecount is always 1 here because Special:MultipleUpload is gone makeUploadForm(multiform[j], DragDrop.batchCounter++, files[i].name); //filecount=20 is fine. filecount = 0; }   }

$("#historyArea table").prepend(" Batch "+j+": "+files.length+" Files <td style='text-align:right;'>Total: "+totalsize+" "); if (filecount && (multiform[j] != undefined)) { //don't resend the same 20 files and don't send when there is no file console.log("final make upload form"); makeUploadForm(multiform[j], DragDrop.batchCounter++, files[i].name); }   sendNext; }, false); }); //-->