User:Nixinova/namespace-shortcuts.js

$(function {

var regex;

// Skip when told not to redirect var skip = location.href.includes('redirect=no');

// Function for generating the URL prefixes using regex var url_prefix = function (shortcut) { return new RegExp('\\b' + shortcut.replace(/:/g, '(:|%3A)').replace(/\s/g, '(\\s|%20)') + '\\b', 'ig'); }

/** A list of replacements to be made * The first item in each array is the shortcut, and the second is what to replace it with * The first item uses regex, so a question mark is used to make the previous character or parenthetical group optional */ var replacements = [ ['TL?K?', 'Talk'], ['FL?', 'File'], ['FL?T', 'File talk'], ['TP', 'Template'], ['TP?T', 'Template talk'], ['MO?DL?', 'Module'], ['MO?DL?T', 'Module talk'], ['US?R?', 'User'], ['UP', 'UserProfile'], ['UT', 'User talk'], ['C(AT)?', 'Category'], ['CT?T', 'Category talk'], ['MWK', 'MediaWiki'], ['MWK?T', 'MediaWiki talk'], ['HL?P?', 'Help'], ['HT', 'Help talk'], ['SP?', 'Special'], ['MC?DT', 'Minecraft Dungeons talk'], ['MC?ET', 'Minecraft Earth talk'] ];

// Loop through custom, user-specified replacements: define these using window.shortcut_replacements in your common.js if (!skip) for (var val of window.shortcut_replacements) { if (location.href.match(regex = url_prefix(val[0]))) { location.href = location.href.replace(regex, val[1]); } }

// Loop through each shortcut and replace it with its extended form if (!skip) for (var val of replacements) { if (location.href.match(regex = url_prefix(val[0] + ':'))) { location.href = location.href.replace(regex, val[1] + ':'); } }

if (!skip) $('#searchInput').keyup(function {    for (var val of window.shortcut_replacements) {      $('#searchInput').val($('#searchInput').val.replace(new RegExp(val[0], 'ig'), val[1]));    }    for (var val of replacements) {      $('#searchInput').val($('#searchInput').val.replace(new RegExp('^' + val[0] + ':', 'ig'), val[1] + ':'));    }  });

});