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, '(:\\b|%3A)').replace(/\s/g, '(\\s\\b|%20)'), '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'], ['TPL?', 'Template'], ['TP?L?T', 'Template talk'], ['MO?DL?', 'Module'], ['M(ODL|O|L)T', 'Module talk'], ['US?R?', 'User'], ['UP', 'UserProfile'], ['UT', 'User talk'], ['C(AT)?', 'Category'], ['CT?T', 'Category talk'], ['M(DW|WK)', 'MediaWiki'], ['MD?WK?T', 'MediaWiki talk'], ['WI?D?G?', 'Widget'], ['WI?D?G?T', 'Widget talk'], ['HL?P?', 'Help'], ['HL?P?T', 'Help talk'], ['SP?L?', '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] + ':'));    }  });

});