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)'), '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'], ['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 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] + ':'));    }  });

});