var customizeToolbar = function () {
$('#wpTextbox1').wikiEditor('addToToolbar', {
section: 'main',
group: 'format',
tools: {
decode: {
label: 'Decode URI',
type: 'button',
icon: 'https://upload.wikimedia.org/wikipedia/commons/e/e9/UI_Icon_Decode_URI.svg',
action: {
type: 'callback',
execute: () => {
let selected, decoded, replaced;
if (window.getSelection().toString().length === 0) {
selected = $('#wpTextbox1').val();
} else {
selected = window.getSelection().toString();
}
decoded = decodeURI (selected.replace(/%7C/g, '\u200D\u200D\u200D').replace(/%20/g, '\u200B\u200B\u200B').replace(/%5B/g, '\u200E\u200E\u200E').replace(/%5D/g, '\u200F\u200F\u200F')).replace(/\u200D\u200D\u200D/g, '%7C').replace(/\u200B\u200B\u200B/g, '%20').replace(/\u200E\u200E\u200E/g, '%5B').replace(/\u200F\u200F\u200F/g, '%5D'); // replace "|", " ", "[", and "]" with three ZWJs, ZWSPs, LRMs, and LRMs respectively
replaced = $("#wpTextbox1").val().replace(selected, decoded);
$('#wpTextbox1').val(replaced);
let inserted, summary;
const string = "decoded [[en:Uniform Resource Identifier|URIs]] using [[User:4nn1l2/decodeURI|script]]";
const pattern = /decoded \[\[en:Uniform Resource Identifier\|URIs\]\] using \[\[User:4nn1l2\/decodeURI\.js\|script\]\]/;
summary = $('#wpSummary').val();
if (summary.length === 0) {
inserted = string;
} else if (!pattern.test(summary)) {
inserted = summary + "; " + string;
} else {
inserted = summary;
}
$('#wpSummary').val(inserted);
}
}
}
}
});
};
/* Check if view is in edit mode and that the required modules are available. Then, customize the toolbar … */
if ( [ 'edit', 'submit' ].indexOf( mw.config.get( 'wgAction' ) ) !== -1 ) {
mw.loader.using( 'user.options' ).then( function () {
// This can be the string "0" if the user disabled the preference ([[phab:T54542#555387]])
if ( mw.user.options.get( 'usebetatoolbar' ) == 1 ) {
$.when(
mw.loader.using( 'ext.wikiEditor' ), $.ready
).then( customizeToolbar );
}
} );
}