User:Nixinova/namespace-shortcuts.js

$(function {

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

// Function for generating the URL prefixes using regex var url_prefix = function (shortcut) { return RegExp(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.namespace_replacements in your common.js for (var val of window.namespace_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 for (var val of replacements) { if (location.href.match(regex = url_prefix(val[0] + ':'))) { location.href = location.href.replace(regex, val[1] + ':'); } }

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

});