User:Perhelion/JustReplace+.js
Jump to navigation
Jump to search
Note: After saving, you have to bypass your browser's cache to see the changes. Internet Explorer: press Ctrl-F5, Mozilla: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Opera/Konqueror: press F5, Safari: hold down Shift + Alt while clicking Reload, Chrome: hold down Shift while clicking Reload.
Documentation for this user script can be added at User:Perhelion/JustReplace+. |
- Report page listing warnings and errors.
/** Commons: [[Category:User scripts|JustReplace]] <nowiki>
* @author: Perhelion
* @revision: ~~~~~
* @required modules: User:Perhelion/justReplace.js
**/
/* eslint-env es6 */
/* eslint indent:["error","tab",{"outerIIFEBody":0}] */
/* global mw, importScript*/
(function () {
'use strict';
let pn = window.pn || mw.config.get('wgPageName'),
dupe = false,
AQD = window.AjaxQuickDelete,
$fileField,
$reasonField,
ti,
bSVG,
file = '';
if (window.bSVG === undefined) {
ti = window.ti || mw.config.get('wgTitle');
bSVG = (!ti.slice(-3).toUpperCase().indexOf('SVG')) || false;
}
function toggleBtn(target) {
if (!target.prop('disabled')) return true;
return target.prop('disabled', false).removeClass('ui-button-red').addClass('ui-button-green');
}
function cleanupReason(/* e*/) {
let fileEXT = AQD.getFileExtension($fileField.val());
// Clear up invalid EXT reason
if (fileEXT === AQD.getFileExtension(pn)) {
$reasonField.val(function (i, v) {
return v.replace(/\.?\w{3,4} ?(→|to) ?\.?\w{3,4}$/, '');
});
}
return true;
}
function chkExist(e, field, $submitBtn) {
let target = 'File:' + field.val().replace(/^ *File:([^|]+)(?:\|.+)?$/ig, '$1');
if (target === pn) return true;
e.preventDefault();
e.stopPropagation();
$submitBtn.attr('disabled', 'disabled').removeClass('ui-button-green').addClass('ui-button-red');
new mw.Api().get({
titles: target, redirects: 1
}).done(function (page) {
const query = page.query;
if (query && (page = query.pages)) {
for (let id in page) {
if (id in page && parseInt(id) > 0) {
// console.log( target, "exists" );
if ('redirects' in query) {
target = page[id].title;
field.val(target);
mw.notify('Automatically redirect resolved.', { title: 'Title changed!', type: 'warn' });
}
$('#AjaxQuestion2').prop('checked', true);
toggleBtn($submitBtn);
cleanupReason();
if (e.target.type !== 'text')
return $submitBtn.triggerHandler('click'); // fire
} else {
mw.notify(target + ' not exists!', {
title: 'Fail!',
type: 'error'
});
return false;
}
}
}
}).fail(function (XHR, status, err) {
mw.notify(XHR + ', ' + status + ', ' + err, { type: 'warn' });
});
}
// Second click event
function prefillTarget(e) { // hack for JustReplace
// try prefill the replace field
e.preventDefault();
// first check for redirect
if (mw.config.get('wgIsRedirect')) {
file = mw.util.$content.find('ul.redirectText a').eq(0).attr('title') || '';
} else {
// second check for auto dupe
if ((file = $('#mw-imagepage-section-duplicates ul a').eq(0).attr('title') || '')) dupe = 1;
if (!file) // check for Superseding version available
file = $('#field-superseded').attr('title') || file;
if (!bSVG) // check for Vector version available
file = $('#field-vva').attr('title') || file;
// check for dupe
if (!file) { // check cats
mw.config.get('wgCategories').some(function (v) {
if (v === 'Duplicate') {
file = $('#AjaxDupeDestination').text();
dupe = true;
return true;
}
});
}
}
AQD.possibleDestination = file;
}
// JustReplace();
function prefillReason() {
$reasonField.val(function (i, v) {
// alert(AQD.mimeFileExtension);
let fileEXT = AQD.getFileExtension($fileField.val());
if (mw.config.get('wgIsRedirect')) {
$('#AjaxQuestion4').prop('checked', false);
if (fileEXT !== AQD.getFileExtension(pn)) dupe = 1;
else if (!dupe) v = '[[Special:MyLanguage/COM:FR|Renamed]] ' + v.replace('[[Special:MyLanguage/COM:FR|Renamed]] ', '');
}
if (dupe) v = '[[COM:DUPE|Dupe]] ' + v.replace(/\s*\[\[COM:DUPE\|Dupe\]\]\s*/i, ''); // gets prefix c: automatic
if (fileEXT && fileEXT !== AQD.getFileExtension(pn)) {
// Change the EXT, not the same
return v.replace(/([ →.])(\w{3,4})$/, '$1' + fileEXT.toUpperCase());
}
return v;
});
}
function fillPrompt() {
$fileField = $('#AjaxQuestion0');
$reasonField = $('#AjaxQuestion1');
let $submitBtn = $fileField.closest('.ui-dialog').find('.ui-dialog-buttonset button.ui-button-green');
function checkFile(e) {
if ($('#AjaxQuestion2').prop('checked')) return true;
return chkExist(e, $fileField, $submitBtn);
}
/* $fileField.val(function (i, v) { // Make default to SVG if no destination found
return (!file) ? v.slice(0, -3) + 'svg' : file;
});*/
prefillReason();
$fileField
.on('focus', function () {
toggleBtn($submitBtn);
})
.on('change', prefillReason)
.on('blur', checkFile);
$reasonField
.one('focus', prefillReason)
.on('blur', cleanupReason);
$submitBtn.on('mousedown', checkFile);
}
// importScript('MediaWiki:ExCommons.js'); // - copy files to individual Wikis before deleting them on Commons
mw.hook('script-loaded.justreplace-prompt').remove(fillPrompt).add(fillPrompt);
function init($a) {
mw.loader.using([], function () {
setTimeout(function () {
AQD = window.AjaxQuickDelete;
mw.libs.JustReplace.prefillTarget = prefillTarget;
$a = ($a && $a.length) ? $a : $('#ca-justreplace').find('a');
if ($a.length)
$a.on('click', mw.libs.JustReplace.prefillTarget).text('JustReplace+');
else
mw.notify('JustReplace + not ready!', { type: 'warn' });
}, 500);
});
}
$(function () {
var $a = $('#ca-justreplace').find('a');
if (typeof mw.libs.JustReplace === 'function' && $a.length) {
init($a);
} else {
delete mw.libs.JustReplace;
importScript('User:Perhelion/justReplace.js');
mw.hook('script-loaded.justreplace').remove(init).add(init);
}
});
}());
// </nowiki> EOF