User:Mikevoir/BetterUpload.js

$(function {	(window.dev = window.dev || {}).BetterUpload = window.dev.BetterUpload || {		'default': '==Licensing==\n',		preloads:[		{			name: 'Quests',			header: true		}]	};	// Double load protection	if (window.dev.BetterUpload._LOADED) { return; }	else { window.dev.BetterUpload._LOADED = true; }   // Load dependencies and cache	var api = new mw.Api;	var PRELOAD_BY_NAME = { None: '' };	var config = mw.config.get(['wgAction', 'wgCanonicalSpecialPageName']);	importScriptPage('User:Mikevoir/BetterUpload.js/settings.js', 'community'); // load settings	// Main class	var betterUpload = {		init: function {			console.log('attempt init');			// Disable default form submit			document.querySelector('.mw-htmlform-submit').remove;			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', 'button'); 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; // betterUpload.genPreloads; betterUpload.renderPreview; mw.hook('userjs.BetterUpload').add(betterUpload.genPreloads); },		genPreloads: function { console.log(window.dev.BetterUpload); if (Array.isArray(window.dev.BetterUpload.preloads) && window.dev.BetterUpload.preloads.length>0) { if (document.querySelector('#mw-htmlform-description tbody tr.mw-htmlform-field-HTMLTextAreaField + .wpPreloadRow')) { document.querySelector('#mw-htmlform-description tbody tr.mw-htmlform-field-HTMLTextAreaField + .wpPreloadRow').remove; }				var preloads_row = document.createElement('tr'); preloads_row.classList.add('wpPreloadRow'); preloads_row.innerHTML = ''+ 'Preloads: '+ ' '+					''+ ''+ 'None selected '+ ' '+					' ';				document.querySelector('#mw-htmlform-description tbody .mw-htmlform-field-HTMLTextAreaField').after(preloads_row); var preloads_list = document.querySelector('#mw-htmlform-description tbody select#wpPreload'); window.dev.BetterUpload.preloads.forEach(function(setting, num){					if (setting.name && (setting.preload || setting.header)) {						var option = document.createElement('option');						if (setting.header) {							option.setAttribute('disabled', 'disabled');							option.style.color = 'GrayText';						} else if (setting.preload) {							option.setAttribute('value', setting.name);							option.setAttribute('title', setting.name);						}						option.innerHTML = setting.description || setting.name;						option.setAttribute('numref', num);						preloads_list.append(option);					}				}); document.querySelector('#mw-htmlform-description tbody select#wpPreload').addEventListener('change', function(event){					var num = $('#mw-htmlform-description tbody select#wpPreload option:selected')[0].numref;					var settings = window.dev.BetterUpload.preloads[num];					console.log(event.target);					console.log(num);					console.log(settings);					if (settings && settings.preload) {						var preload = settings.preload;						document.querySelector('.mw-htmlform-field-HTMLTextAreaField textarea').value=preload;						if (settings.fillin && preload.find('\$\(1\)\$')) {							var fillin_row = document.createElement('tr');							fillin_row.innerHTML = 								''+									'Values: '+								' '+								''+									''+										'None selected '+									' '+ ' ';							var fillin_list = document.querySelector('#mw-htmlform-description tbody select#wpFillin'); settings.fillin.forEach(function(fillin, index){								var option = document.createElement('option');								var name = fillin.name || setting.values.join(', ');								if (setting.header) {										option.setAttribute('disabled', 'disabled');										option.style.color = 'GrayText';								} else if (setting.preload) {									option.setAttribute('value', name);									option.setAttribute('title', name);								}								option.innerHTML = name;								option.setAttribute('numref', index);								fillin_list.append(option);							}); }						document.querySelector('.mw-htmlform-field-HTMLTextAreaField textarea').value = preload; betterUpload.renderPreview; } else { alert('Invalid option.'); } });			}		},		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){ 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',                text: document.querySelector('#wpUploadDescription').value            };			if (file && filename && filename.length>0) {				api.upload(file, params).done(function{ setTimeout(function{window.location.replace(window.location.href.replace(/^(.+\/wiki\/).+$/, '$1')+'File:'+filename);},1000); });			} else { alert('Missing file or file name.'); }		},	};	// Start when API and LIB are loaded	mw.loader.using('mediawiki.api').then(function{ // Check we're in Special:Upload if (config.wgCanonicalSpecialPageName == 'Upload') { if (document.querySelector('.mw-htmlform-field-HTMLTextAreaField > .mw-label > label')) { setTimeout(betterUpload.init); } else { // set up the mutation observer var observer = new MutationObserver(function (mutations, me) {					// mutations is an array of mutations that occurred					// me is the MutationObserver instance					var targetNode = document.querySelector('.mw-htmlform-field-HTMLTextAreaField > .mw-label > label');					if (targetNode) {						setTimeout(betterUpload.init);						me.disconnect; // stop observing						return;					}				}); // start observing observer.observe(document, {				 childList: true,				  subtree: true				}); }		}	}); });