User:Nixinova/namespace-shortcuts.js

(function {

/** Generate the prefixes using regex: * @param shortcut: the shortcut code to replace * @param replace: determines whether the regex should be what to look for ('false') or what to replace ('true') */ var prefix = function (shortcut, replace) { return new RegExp((replace ? '' : '(\\/|=)') + shortcut + '(:|%3A)', 'i'); }

/** 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'], ['FT', 'File_talk'], ['TP', 'Template'], ['TP?T', 'Template_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'], ['SP?T', 'Special_talk'], ['MC?DT', 'Minecraft_Dungeons_talk'], ['MC?ET', 'Minecraft_Earth_talk'] ];

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