User:Est. 2021/Script/AutoCleanup/Functions.js
Appearance
< User:Est. 2021 | Script
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 code will be executed when previewing this page.
Documentation for this user script can be added at User:Est. 2021/Script/AutoCleanup/Functions.
/*
Subpage of [[User:Est. 2021/Script/AutoCleanup.js]]
*/
// <syntaxhighlight lang="js">
$(function() {
function clean_wikitext(text) {
const replacements = [
// Wikilinks
[/(?<!\[)\[https?:\/\/en\.wikipedia\.org\/wiki\/([^ \]]*)(?: ([^\]]*))?\]/gs, function(match, p1, p2) {
p1 = decodeURIComponent(p1);
p1 = p1.replace(/_/g, ' ');
if (p2 == undefined) {
return `[[${p1}]]`;
} else {
return `[[${p1}|${p2}]]`;
} }], // format wikilinks
[/\[\[([^\]]*)\]\]/gs, function(match, p1) {
p1 = decodeURIComponent(p1);
p1 = p1.replace(/_/g, ' ');
return `[[${p1}]]`;
}], // remove "_" from wikilinks #OWN
// Images (cleanup)
[/((Image:|File:)([^\.\n])*(\.\w*))/g, function(match, p1) {
p1 = decodeURIComponent(p1);
p1 = p1.replace(/_/g, ' ');
return `${p1}`;
}], // remove "_" from file names #OWN
[/(\| *(?:image\d*|image_\w*|cover|flag|insignia|logo|photo|seal|shield) *= *)(?:\[\[)(?:(?:Image|File):)?([^\.\n]*\.\w*)(?:\]\])/g, function(match, p1, p2) {
p2 = decodeURIComponent(p2);
p2 = p2.replace(/_/g, ' ');
return `${p1}${p2}`;
}], // if "|image=[[File:name]]" is used, remove unneeded "[[File:]]" #OWN
[/(\| *(?:image\d*|image_\w*|cover|flag|insignia|logo|photo|seal|shield) *= *)(?:(?:Image|File):)?([^\.\n]*\.\w*)/g, function(match, p1, p2) {
p2 = decodeURIComponent(p2);
p2 = p2.replace(/_/g, ' ');
return `${p1}${p2}`;
}], // if "|image=File:name" is used, remove unneeded "File:" #OWN
// Lowercase #OWN
[/\{\{(ill|lang|(note|ref)list|infobox former country)/gs, function(match, p1) {
p1 = decodeURIComponent(p1);
p1 = p1.replace(/_/g, ' ');
return `{{${p1.toLowerCase()}`;
}],
];
for (const replacement of replacements) {
text = text.replace(...replacement);
}
return text;
}
function process_summary(summary) {
const ac_summary = 'minor fixes';
if (summary) {
if (/^\/\*.*?\*\/ $/.test(summary)) { // auto summary
return summary + ac_summary;
} else {
return summary + '; ' + ac_summary;
}
} else {
return ac_summary;
}
}
function main() {
let text_box = $('#wpTextbox1');
let old_text = text_box.textSelection('getContents');
let new_text = clean_wikitext(old_text);
if (old_text != new_text) {
text_box.textSelection('setContents', new_text);
if (mw.util.getParamValue('section') != 'new') { // not section title instead of summary
let summary_box = $('#wpSummary');
summary_box.val(
process_summary(summary_box.val())
)}
mw.notify(
'Done',
{
tag: 'tol-autocleanup-f',
title: 'AC Functions',
type: 'success'
}
);
}
}
function add_button() {
try {
if ($('editform').length) {
mw.loader.using(['oojs-ui', 'oojs-ui-widgets'], function() {
let button = new OO.ui.ButtonWidget({label: 'AC Functions'});
button.on('click', main);
$('.editButtons').append(button.$element);
});
}
} catch(error) {
alert(error);
}
}
//$(add_button);
if (mw.config.get('wgPageContentModel') === 'wikitext' && mw.config.get('wgAction') === 'edit' && mw.util.getParamValue( "AC/Fdisable" ) != "1" ) {
mw.util.addPortletLink( "p-cactions", window.location.href + '&AC/Fdisable=1', "Disable AC Functions", "pt-disableAC/F" );
$(main);
}
});
// </syntaxhighlight>