User:Nardog/PasteToCommons.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
This user script seems to have a documentation page at User:Nardog/PasteToCommons. |
(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 [[w:${pn}]]`,
license: '{{Wikipedia-screenshot|1=en}}'
}, 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);
});
});
}());