User:Mikevoir/BetterUpload.js

$(function {	(window.dev = window.dev || {}).BetterUpload = window.dev.BetterUpload || {		'default': '==Licensing==\n'	};   // Load dependencies and cache    importScriptPage('User:Mikevoir/lib.js', 'community');	var lib;	var api = new mw.Api;	var PRELOAD_BY_NAME = { None: '' };	var config = mw.config.get(['wgAction', 'wgCanonicalSpecialPageName']);	// Main class	var betterUpload = {		init: function {			// Disable default form submit			document.querySelector('.mw-htmlform-submit').setAttribute('disabled', 'disabled');			document.querySelector('.mw-htmlform-field-HTMLTextAreaField > .mw-label > label').innerHTML = 'Page content:';			// Add custom form submit			var submit = document.createElement('input');			submit.classList.add('mw-htmlform-submit');			submit.setAttribute('name', 'wpBetterUpload');			submit.setAttribute('type', 'Submit');			submit.setAttribute('value', 'Upload file with preload'); document.querySelector('.mw-htmlform-submit-buttons').append(submit); submit.addEventListener('click', betterUpload.attemptUpload); document.querySelector('.mw-htmlform-field-HTMLTextAreaField > .mw-input > textarea').addEventListener('change', betterUpload.renderPreview); document.querySelector('.mw-htmlform-field-HTMLTextAreaField textarea').value = window.dev.BetterUpload.default || ''; document.querySelector('tr.mw-htmlform-field-Licenses').remove; document.querySelector('p.mw-upload-editlicenses').remove; if (Array.isArray(window.dev.BetterUpload) && window.dev.BetterUpload.length>0) { var preloads_row = document.createElement('tr'); preloads_row.innerHTML = ''+ 'Preloads: '+ ' '+					'None selected '; document.querySelector('#mw-htmlform-description tbody').append(preloads_row); var preloads_list = document.querySelector('#mw-htmlform-description tbody select#wpPreload'); window.dev.BetterUpload.forEach(function(setting){					if (setting.name && (setting.preload || setting.header)) {						var option = document.createElement('option');						if (setting.header) {							option.setAttribute('disabled', 'disabled');							option.style.color = 'GrayText';						} else {							option.setAttribute('value', setting.name);							option.setAttribute('title', setting.name);							PRELOAD_BY_NAME[setting.name] = setting.preload;						}						option.innerHTML = setting.description || setting.name;						preloads_list.append(option);					}				}); document.querySelector('#mw-htmlform-description tbody select#wpPreload').addEventListener('change', function(event){					var selected = event.target.value;					if (!selected || selected.length==0) {selected='None';}					if (PRELOAD_BY_NAME[selected]) {						document.querySelector('.mw-htmlform-field-HTMLTextAreaField textarea').value = PRELOAD_BY_NAME[selected];						betterUpload.renderPreview;					} else { alert('Invalid option.'); }				}); }			betterUpload.renderPreview; },		renderPreview: function { var filename = document.querySelector('#wpDestFile').value; var text = document.querySelector('.mw-htmlform-field-HTMLTextAreaField textarea').value; var params = { action: 'parse', text: text, prop: 'text', disablelimitreport: true, contentmodel: 'wikitext' };			if (filename.length>0) { params.title = 'File:' + filename; }			if (text.length>0) { api.get(params).then(function(data){					console.log('data', data);					if (data && data.parse && data.parse.text && data.parse.text['*']) {						if (document.querySelector('#pagePreview')) {document.querySelector('#pagePreview').remove;}						var preview = document.createElement('tr');						document.querySelector('#mw-htmlform-description tbody').append(preview);						preview.id = 'pagePreview';						preview.innerHTML = ' Page Preview '+data.parse.text['*']+' ';					}				}).fail(console.log); }		},		attemptUpload: function { var filename = document.querySelector('#wpDestFile').value; var file = document.querySelector('#wpUploadFile').files[0]; var params = { token: mw.user.tokens.get('csrfToken'), filename: filename, ignorewarnings: '1', format: 'json' };			if (file && filename && filename.length>0) { if (document.querySelector('#wpUploadDescription').value.length>0) { api.get({						action:'query',						prop:'info',						titles: 'File:' + filename					}).then(function(date){						if (data && data.pages && !data.pages[-1]) {							params.text = document.querySelector('#wpUploadDescription').value;						}					}); }				api.upload(file, params).done(window.location.replace(window.location.href.replace(/^(.+\/wiki\/).+$/, '$1')+'File:'+filename)); } else { alert('Missing file or file name.'); } },	};	// Start when API and LIB are loaded mw.loader.using('mediawiki.api').then(function{		mw.hook('userjs._LIB').add(function(_LIB){ lib = _LIB; // Check we're in Special:Upload if (config.wgCanonicalSpecialPageName == 'Upload') { lib.waitFor('.mw-htmlform-field-HTMLTextAreaField > .mw-label > label', function {betterUpload.init;}); }		});	}); });