Jump to content

User:Topbanana/RLRL SR Utility.js

From Wikipedia, the free encyclopedia
This is an old revision of this page, as edited by Topbanana (talk | contribs) at 16:31, 29 May 2010. The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.
Note: After saving, you have to bypass your browser's cache to see the changes. Google Chrome, Firefox, Microsoft Edge and Safari: Hold down the ⇧ Shift key and click the Reload toolbar button. For details and instructions about other browsers, see Wikipedia:Bypass your cache.
// Create some search-and-replace tools - this uses a slightly modified version
// of [[User:Gerbrant/edit/regexReplace.js]])
importScript('User:Topbanana/regexReplace.js' );

function RLRL_SR_Utility()
{
   // 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 )
{
document.forms.editform.wpTextbox1.value = "otherparams";
         return;
}

      // Check the edit box is empty
      if( document.forms.editform.wpTextbox1.value.length > 0 )
      {
document.forms.editform.wpTextbox1.value = "emtpy";
         return;
      }
 
      // Correct the parameters provided
      parRedirect = unescape( decodeURIComponent( parRedirect.replace( /_/g, " " ) ) );

      // Okay, populate the edit box and summary
      document.forms.editform.wpTextbox1.value = "#REDIRECT [[" + parDirect + "]]";
      document.forms.editform.wpSummary.value =
          'Link repair:  Redirecting page to ' + parRedirect + '  - [[Wikipedia:WikiProject Red Link Recovery|You can help!]]';

      return;
   }

   // 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 ^ $ \ / ( ) | ? + * [ ] { } , . 
   // (incomplete)
   parRegex = parSearch.replace( /\(/g, "\\(" ).replace( /\)/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 + '  - [[Wikipedia:WikiProject Red Link Recovery|You can help!]]';
};
 
addOnloadHook( RLRL_SR_Utility );