User:HardBoiledEggs/RLRL SR Utility.js
Appearance
Code that you insert on this page could contain malicious content capable of compromising your account. If you import a script from another page with "importScript", "mw.loader.load", "iusc", or "lusc", take note that this causes you to dynamically load a remote script, which could be changed by others. Editors are responsible for all edits and actions they perform, including by scripts. User scripts are not centrally supported and may malfunction or become inoperable due to software changes. A guide to help you find broken scripts is available. If you are unsure whether code you are adding to this page is safe, you can ask at the appropriate village pump. This code will be executed when previewing this page. |
Documentation for this user script can be added at User:HardBoiledEggs/RLRL SR Utility. |
// Duplicated with permission from [[User:Topbanana/RLRL SR Utility.js]]
// Create some search-and-replace tools - this uses a slightly modified version
// of [[User:Gerbrant/edit/regexReplace.js]])
mw.loader.load('https://en.wikipedia.org/w/index.php?title=User:Topbanana/regexReplace.js&action=raw&ctype=text/javascript');
function RLRL_SR_Utility()
{
strAdvert = ' - [[w:en:WP:RLR|You can help!]]';
// If we're not editing, do nothing
if ( !document.getElementById("editform") )
return;
// Check for and extract 'RLRLS' (search) and 'RLRLR' (replace) parameters
// Method based on 'gup' (http://www.netlobo.com/url_query_string_javascript.html)
param_match = new RegExp( '[\\?&]RLRLS=([^&#]*)' ).exec( window.location.href );
parSearch = param_match ? param_match[1] : '';
param_match = new RegExp( '[\\?&]RLRLR=([^&#]*)' ).exec( window.location.href );
parReplace = param_match ? param_match[1] : '';
param_match = new RegExp( '[\\?&]RLRLRD=([^&#]*)' ).exec( window.location.href );
parRedirect = param_match ? param_match[1] : '';
// First check if we are being asked to create a redirect
if( parRedirect.length > 0 )
{
// Check we have no other parameters
if( parSearch.length > 0 || parReplace.length > 0 )
return;
// Check the edit box is empty
if( document.forms.editform.wpTextbox1.value.length > 0 )
return;
// Correct the parameters provided
parRedirect = unescape( decodeURIComponent( parRedirect.replace( /_/g, " " ) ) );
// Okay, populate the edit box and summary
document.forms.editform.wpTextbox1.value = '#REDIRECT [[' + parRedirect+ ']]';
document.forms.editform.wpSummary.value =
'Link repair: Redirecting page to ' + parRedirect + strAdvert;
document.forms.editform.wpWatchthis.checked = false;
return;
}
// If we are in a diff, add a save button at the top of the page for easy access
if ($("#wikiDiff").length == 1) {
$("<button>").text("Save").click(function(){$("#wpSave").click()}).prependTo("#wikiDiff");
}
// Okay, not a redirect. Check if we are being asked to search-and-replace
// If we've not got a useful set of parameters, give up
if( parSearch.length == 0 || parReplace.length == 0 )
return;
// Correct the parameters provided
parSearch = unescape( decodeURIComponent( parSearch.replace( /_/g, " " ) ) );
parReplace = unescape( decodeURIComponent( parReplace.replace( /_/g, " " ) ) );
// Escape any characters with special meanings in regular expressions
parRegex = parSearch.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
// Find and manipulate the search and replace controls
createRegexControls();
document.getElementById('tbRegex_SearchTextBox').value = parRegex;
document.getElementById('tbRegex_FindButton').click();
document.getElementById('tbRegex_ReplaceTextBox').value = parReplace;
// Populate the edit summary
document.forms.editform.wpSummary.value =
'Link repair: ' + parSearch + ' -> ' + parReplace + strAdvert;
document.forms.editform.wpMinoredit.checked = true;
// Run suggested replace
$("#tbRegex_ReplaceTextBox").next().next().click();
// Show changes
$("#wpDiff").click();
};
$( RLRL_SR_Utility );