User:Jayprakash12345/ImageImporter.js
Note: After publishing, you may have to bypass your browser's cache to see the changes.
- Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
- Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
- Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
- Opera: Press Ctrl-F5.
/*
@Author [[User:Jayprakash12345]]
@OwnBy [[meta:Indic-TechCom]]
*/
$( document ).ready( function() {
var requires = [
'jquery.ui'
];
mw.loader.using( requires ).done(function () {
console.log( " Started")
// Add a portlet link.
var link = mw.util.addPortletLink(
'p-views',
'#',
'Import Image'
);
// Trigger for portlet link
$(link).click( function( e ) {
e.preventDefault();
ImgImporter();
});
imgImt = function() {
URL = 'https://en.wikipedia.org/w/api.php';
var ImportingMsg = $('<li>').text('Importing....');
$( '#divimgimport' ).append( '<br/><br/>', ImportingMsg );
fileName = $( '#wikiUrlInput' ).val();
param = {
action: 'query',
format: 'json',
prop: 'imageinfo|revisions',
titles: 'File:'+ fileName,
iiprop: 'url',
iilocalonly: 1,
rvprop: 'content',
rvslots: '*',
origin: '*'
};
$.ajax( {
url: URL,
data: param,
dataType: 'json'
} ).done( function(result) {
uploadUrl = result.query.pages[Object.keys(result.query.pages)[0]].imageinfo[0].url
content = result.query.pages[Object.keys(result.query.pages)[0]].revisions[0].slots.main['*']
var Api = new mw.Api();
Api.postWithToken( 'csrf', {
action: 'upload',
filename: 'File:' + fileName,
url: uploadUrl,
text: content,
ignorewarnings: true
}).always( function () {
}).fail(function( repsn ){
$( ImportingMsg ).remove();
mw.notify( 'There are some error.' + repsn, { type: 'error' } );
setTimeout(function(){ location.reload(true); }, 3000);
}).done( function( obj ) {
$( ImportingMsg ).remove();
if ( obj.upload.result === "Success" ){
mw.notify( 'File successfull uploaded.', { type: 'info' } );
t = new mw.Title( 'File:'+ fileName )
setTimeout(function(){ location.href = mw.config.get( 'wgServer' ) + t.getUrl() }, 2000);
}
});
});
}
ImgImporter = function () {
var ImgImportDialog = $('<div id="divimgimport"></div>' ).attr('title', 'Image Importer' );
$( ImgImportDialog ).append( '<label for="wikiUrlInput">Enter Image Name:</label>', '<br/><br/>' );
$( ImgImportDialog ).append( '<input id="wikiUrlInput" type="text"></input>', '<br/>' );
$( ImgImportDialog ).append( '<small>Please don\'t give File: prefix.</small>', '<br/>' );
$( 'body' ).append( ImgImportDialog );
$( ImgImportDialog ).dialog({
height: 300,
width: 400,
modal: true,
buttons: {
Submit: {
text: 'Import',
click: imgImt
}
}
});
}
});
});