User:Rillke/AjaxUpload.js
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.
Documentation for this user script can be added at User:Rillke/AjaxUpload. |
if (typeof(AjaxUplaod) == 'undefined') {
window.AjaxUplaod = {
obtainEditToken: function () {
if (mw.user && mw.user.tokens && mw.user.tokens.get) this.editToken = mw.user.tokens.get( 'editToken' );
this.editToken = (this.editToken || window['wikilove-edittoken'] || (mw.user.anonymous() ? '+\\' : '') );
if (this.editToken) return;
var query = {
action: 'query',
prop: 'info',
intoken: 'edit',
titles: 'FAQ' // Random title
};
this.doAPICall(query, 'obtainEditTokenCB');
},
obtainEditTokenCB: function (result) {
var pages = result.query.pages;
for (var id in pages) { // there should be only one, but we don't know its ID
this.editToken = pages[id].edittoken;
}
},
ulDlg: function () {
var dlgButtons = {};
dlgButtons[this.i18n.submitButtonLabel] = function () {
AjaxUplaod.ulUpload();
};
dlgButtons[this.i18n.cancelButtonLabel] = function () {
$(this).dialog("close");
};
this.dlg = $('<div></div>').html('<div id="AjaxUlContainer"></div>').dialog({
modal: true,
closeOnEscape: true,
position: 'center',
dialogClass: "wikiEditor-toolbar-dialog",
title: 'Upload by URL Script in Alpha-Mode. ' +
'Report bugs and ideas to <a href="http://commons.wikimedia.org/wiki/User:Rillke" target="_blank">Rillke</a>.',
height: Math.min($(window).height(), 700),
width: Math.min($(window).width(), 850),
buttons: dlgButtons,
close: function () {
$(this).dialog("destroy");
$(this).remove();
AjaxUplaod.umDlgPresent = false;
},
});
this.umDlgPresent = true;
if (this.dlg) {
this.$AjaxUlContainer = $('#AjaxUlContainer');
this.$AjaxUlContainer.append('<span id="ulURLWrapper"><label for="ulURL">' + this.i18n.ulFillInURL + '</label><br><input type="text" id="ulURL" style="width: 99%;" value="http"/><br><br></span>')
.append('<span id="ulNewFileNameWrapper"><label for="ulNewFileName">' + this.i18n.ulFillInFileName + '</label><br><input type="text" id="ulNewFileName" style="width: 99%;" value="name.ext"/><br><br></span>')
.append('<span id="ulDescriptionWrapper"><label for="ulDescription">' + this.i18n.ulFillInDescription + '</label><br><textarea id="ulDescription" style="width: 99%;">{{Information ...</textarea><br><br></span>');
}
},
ulUpload: function() {
var request = {
action: "upload",
filename: $('#ulNewFileName').val(),
comment: "Uploaded using [[User:Rillke/AjaxUpload.js|AjaxUpload]] script in Alpha-Mode",
text: $('#ulDescription').val(),
url: $('#ulURL').val(),
token: this.editToken
};
this.doAPICall(request, 'ulUploadCB');
},
ulUploadCB: function () {
alert('Your request has been sent to the server. It may take some time to fetch this image. Please do not retry inbetween 5 min.');
},
doAPICall: function (params, callback) {
var o = this;
params.format = 'json';
$.ajax({
url: this.apiURL,
cache: false,
dataType: 'json',
data: params,
type: 'POST',
success: function (result, status, x) {
if (!result) return o.fail("Receive empty API response:\n" + x.responseText);
// In case we get the mysterious 231 unknown error, just try again
if (result.error && result.error.info.indexOf('231') != -1) return setTimeout(function () {
o.doAPICall(params, callback);
}, 500);
if (result.error) return o.fail("API request failed (" + result.error.code + "): " + result.error.info);
if (result.edit && result.edit.spamblacklist) {
return o.fail("The edit failed because " + result.edit.spamblacklist + " is on the Spam Blacklist");
}
try {
o[callback](result);
} catch (e) {
return o.fail(e);
}
},
error: function (x, status, error) {
return o.fail("API request returned code " + x.status + " " + status + "Error code is " + error);
}
});
},
fail: function (err) {
mw.notify("During the execution of AxUserMsg, the following error occured: " + err);
},
apiURL: mw.config.get('wgServer') + mw.util.wikiScript( 'api' ),
i18n: {
submitButtonLabel: "Upload",
cancelButtonLabel: "Cancel",
ulFillInURL: "URL to image",
ulFillInFileName: "New File - Name",
ulFillInDescription: "Description for the new file",
},
};
mediaWiki.loader.using('jquery.ui', function () {
AjaxUplaod.obtainEditToken();
$(document).ready(function () {
AjaxUplaod.ulDlg();
});
});
} // end if (guard) </nowiki>