User:Nardog/PasteToCommons.js

(function pasteToCommons {	let clicked, dialog;	document.addEventListener('paste', e => { if (e.target.closest(':read-write')) return; let item = [...e.clipboardData.items].find(i => i.type === 'image/png'); if (!item) return; e.preventDefault; let file = item.getAsFile; if (dialog) { dialog.open(file); return; }		if (clicked) return; clicked = true; mw.notify('Loading PasteToCommons...', {			autoHideSeconds: 'long',			tag: 'pastetocommons'		}); mw.loader.using([			'mediawiki.Upload.Dialog',			'mediawiki.ForeignStructuredUpload.BookletLayout'		], => {			dialog = new mw.Upload.Dialog({ bookletClass: mw.ForeignStructuredUpload.BookletLayout, booklet: { target: 'shared' } });			dialog.initialize = function {				this.constructor.prototype.initialize.call(this);				this.uploadBooklet.sourceWidget = new OO.ui.TextInputWidget({ required: true });				this.uploadBooklet.authorWidget = new OO.ui.TextInputWidget({ required: true });				this.uploadBooklet.licenseWidget = new OO.ui.TextInputWidget({ required: true });				this.uploadBooklet.infoForm.items[1].addItems([ new OO.ui.FieldLayout(this.uploadBooklet.sourceWidget, {						label: 'Source',						align: 'top'					}), new OO.ui.FieldLayout(this.uploadBooklet.authorWidget, {						label: 'Author',						align: 'top'					}), new OO.ui.FieldLayout(this.uploadBooklet.licenseWidget, {						label: 'License',						align: 'top'					}) ], 2);				this.uploadBooklet.initialize = function {					return this.constructor.prototype.initialize.call(this).then( => { this.setFile(dialog.file); this.ownWorkCheckbox.setSelected(true); this.uploadForm.items[0].items.slice(1).forEach(layout => {							layout.toggle(false);						}); mw.requestIdleCallback( => {							let notif = $('.mw-notification-tag-pastetocommons')								.data('mw-notification');							if (notif) notif.close;						}); });				};				this.uploadBooklet.createUpload = function {					let upload = this.constructor.prototype.createUpload.call(this);					upload.getSource =  => this.sourceWidget.getValue.trim;					upload.getUser =  => this.authorWidget.getValue.trim;					upload.getLicense =  => this.licenseWidget.getValue.trim;					return upload;				};				this.uploadBooklet.uploadFile = function  {					let deferred = this.constructor.prototype.uploadFile.call(this);					let pn = mw.config.get('wgPageName').replace(/_/g, ' ');					let defaults = Object.assign({ filename: `${location.hostname} ${pn} ${this.dateWidget.getValue}`, description: 'Screenshot of the English Wikipedia', source: 'Screenshot', author: `Wikipedia Authors, see the history of ${pn}`, license: '' }, window.pastetocommonsDefaults);					this.filenameWidget.setValue(defaults.filename);					this.descriptionWidget.setValue(defaults.description);					this.sourceWidget.setValue(defaults.source);					this.authorWidget.setValue(defaults.author);					this.licenseWidget.setValue(defaults.license);					return deferred;				};			};			dialog.getSetupProcess = function (data) {				this.file = data;				return this.constructor.prototype.getSetupProcess.call(this, data);			};			let winMan = new OO.ui.WindowManager;			winMan.addWindows([dialog]);			winMan.$element.appendTo(OO.ui.getTeleportTarget);			dialog.open(file);		}); }); });