User:Magnus Manske/fixdisambig.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:Magnus Manske/fixdisambig. |
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 ) ;
}