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.
addOnloadHook ( init_fixdisambig ) ;

fixdisambig_server = "" ;
fixdisambig_real_target = "" ;

function fixdisambig_get_selection ()
{
  if (window.getSelection) return window.getSelection();
  else if (document.getSelection) return document.getSelection();
  else if (document.selection) return document.selection.createRange().text;
  return "" ;
}

function fixdisambig_get_query(name) {
  var value=null;
  var query=window.location.search;
  if(query != "") {
    var kk=query.indexOf(name+"=");
    if(kk >= 0) {
      kk=kk+name.length+1;
      var ll=query.indexOf("&", kk);
      if(ll < 0)ll=query.length;
      value=query.substring(kk, ll);
      for(kk=0; kk<value.length; kk++) {
        if(value.charAt(kk) == '+') {
          value=value.substring(0, kk)+" "+value.substring(kk+1,value.length);
        }
      }
      value=unescape(value);
    }
  }
  return value;
}

function fixdisambig_get_referrer_for_disambig () {
  var catlinks = document.getElementById ( "catlinks" ) ;
  if ( !catlinks ) return "" ;
  var as = catlinks.getElementsByTagName ( "a" ) ;
  var is_disambig = 0 ;
  for ( var ai = 0 ; ai < as.length ; ai++ ) {
    var a = as[ai] ;
    var h = a.getAttribute('href').split('/Category:').pop() ;
    h = h.split('_').join(' ') ;
    if ( h != "Disambiguation" &&
         h != "Disambiguation pages" &&
         h != "Human name disambiguation pages" &&
         h != "Lists of ambiguous numbers" &&
         h != "Lists of ambiguous human names"
         ) continue ;
    is_disambig = 1 ;
    break ;
  }
  if ( !is_disambig ) return "" ;

  fixdisambig_real_target = mw.config.get('wgPageName') ;
  var sitesub = document.getElementById('contentSub') ;
  if ( sitesub ) {
    var a = sitesub.getElementsByTagName('a') ;
    if ( a.length > 0 ) {
      a = a[0] ;
      fixdisambig_real_target = a.getAttribute ( 'title' ) ;
    }
  }

  var r = document.referrer ;
  if ( r.substr ( 0 , 29 ) == "http://en.wikipedia.org/wiki/" ) {
    fixdisambig_server = r.substr ( 0 , 29 ) ;
    r = r.substr ( 29 ) ;
  } else if ( r.substr ( 0 , 47 ) == "https://secure.wikimedia.org/wikipedia/en/wiki/" ) {
    fixdisambig_server = r.substr ( 0 , 47 ) ;
    r = r.substr ( 47 ) ;
  }
  else r = "" ;
  return r ;
}

function fix_disambig_replace ( rtext , rwhat , rwith , rend ) {
  return rtext.split ( rwhat + rend ) . join ( rwith + rend ) ;
}

function lcfirst ( s ) {
  return s.substr(0,1).toLowerCase() + s.substr(1) ;
}

function do_fix_disambig ( fd , rwith ) {
  rwith = "[[" + rwith ;
  rwith = rwith.split("_").join(" ") ;
  fd = "[[" + fd ;
  var fd2 = fd.split("_").join(" ") ;

  document.editform.wpMinoredit.checked = true ;
  document.editform.wpSummary.value = "Replacing disambiguation link to " + fd2 + 
                                      "]] with direct link to " + rwith + "]] (using [[User:Magnus Manske/fixdisambig.js|FixDisambig.js]])" ;
  
  var text = document.editform.wpTextbox1.value ;
  text = fix_disambig_replace ( text , fd , rwith , "|" ) ;
  text = fix_disambig_replace ( text , fd , rwith+"|" , "]]" ) ;
  text = fix_disambig_replace ( text , fd2 , rwith , "|" ) ;
  text = fix_disambig_replace ( text , fd2 , rwith+"|" , "]]" ) ;

  fd = "[[" + lcfirst ( fd.substr(2) ) ;
  fd2 = "[[" + lcfirst ( fd2.substr(2) ) ;
  rwith = "[[" + lcfirst ( rwith.substr(2) ) ;

  text = fix_disambig_replace ( text , fd , rwith , "|" ) ;
  text = fix_disambig_replace ( text , fd , rwith+"|" , "]]" ) ;
  text = fix_disambig_replace ( text , fd2 , rwith , "|" ) ;
  text = fix_disambig_replace ( text , fd2 , rwith+"|" , "]]" ) ;

  document.editform.wpTextbox1.value = text ;

  document.getElementById("wpDiff").click() ; // Run diff
}

function init_fixdisambig () {
  var fd = fixdisambig_get_query ( "fix_disambig" ) ;
  var rwith = fixdisambig_get_query ( "fix_disambig_with" ) ;
  if ( fd && rwith && fd != "" && rwith != "" ) {
    do_fix_disambig ( fd , rwith ) ;
    return ;
  }
  var r = fixdisambig_get_referrer_for_disambig () ;
//  if ( mw.config.get('wgUserName') == "Magnus Manske" ) alert ("!");
  if ( r ==  "" ) return ;

  var out = '<li id="t-permalink">' ;
  out += "<a href='javascript:run_fix_disambig();'>Fix disambig</a>" ;
  out += "</li>" ;

  var tools = document.getElementById("p-tb") ;
  tools = tools.getElementsByTagName("ul")[0] ;
  tools.innerHTML += out ; // Yeah, I know...
}

function run_fix_disambig () {
  var sel = fixdisambig_get_selection () ;
  if ( sel == "" ) {
    var r2 = mw.config.get('wgPageName').split("_").join(" ") + " ()" ;
    sel = prompt ( "Please enter the new target (you could have skipped this by selecting the new title in the page text):" , r2 ) ;
    if ( !sel || sel == "" || sel == r2 ) return ;
  }
  var r = fixdisambig_get_referrer_for_disambig () ;
  document.location = fixdisambig_server + r + "?action=edit&fix_disambig=" +
                      escape ( fixdisambig_real_target ) + "&fix_disambig_with=" + escape ( sel ) ;
}