User:Mikevoir/BetterUpload.js

$(function {   // Load dependencies and cache    importScriptPage('User:Mikevoir/lib.js', 'community');	var lib;	var api = new mw.Api;	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');			submit.addEventListener('click', betterUpload.attemptUpload);			document.querySelector('.mw-htmlform-submit-buttons').append(submit); document.querySelector('.mw-htmlform-field-HTMLTextAreaField > .mw-input > textarea').addEventListener('change', function(event){				var filename = document.querySelector('#wpDestFile').value;				var text = document.querySelector('.mw-htmlform-field-HTMLTextAreaField textarea').value;				if (filename.length>0 && text.length>0) {					api.post({ title: 'File:' + filename, action: 'parse', text: document.querySelector('.mw-htmlform-field-HTMLTextAreaField textarea').value, prop: 'text', disablelimitreport: true, contentmodel: 'wikitext' }).then(function(data){ if (data && data.parse && data.parse.text && data.parse.text['*']) { console.log(data); if (document.querySelector('.mw-htmlform-description #pagePreview')) {document.querySelector('.mw-htmlform-description #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['*']+' '; }					});				}			});			document.querySelector('.mw-htmlform-field-HTMLTextAreaField textarea').value = '==Licensing==\n'; document.querySelector('tr.mw-htmlform-field-Licenses').remove; document.querySelector('p.mw-upload-editlicenses').remove; },		attemptUpload: function { var filename = document.querySelector('#wpDestFile').value; if (filename && filename.length>0) { api.get({					action:'query',					prop:'info',					titles: 'File:' + filename				}).then(function(date){					if (data && data.pages && !data.pages[-1]) {					} else {					}				}); }		},	};	// 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;}); }		});	}); });