User:Magnus Manske/InstantAppendLink.js

From Wikimedia Commons, the free media repository
Jump to navigation Jump to search
Note: After saving, you have to bypass your browser's cache to see the changes. Internet Explorer: press Ctrl-F5, Mozilla: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Opera/Konqueror: press F5, Safari: hold down Shift + Alt while clicking Reload, Chrome: hold down Shift while clicking Reload.
//<pre><nowiki>

// __________________________________________________________________________
// BEGIN Instant append link stuff

addOnloadHook ( check_instant_append_link ) ;

// This is the function to call, the others are just internal handlers
// page : Page name
// append : Text to append
// linktitle : Text between <a> and </a>
// pre : 0 to append text, 1 to put it at the top of the page
function get_instant_append_link ( page , append , linktitle , pre ) {
  append = encodeURIComponent ( append ) ;
  append = encodeURIComponent ( append ) ;

  var id = encodeURIComponent ( encodeURIComponent ( page ) + "_" + append + "_" + linktitle ) ;
  id = id.split("'").join("_");
  pre = pre == 1 ? "1" : "0" ;

  // Ugly but quick to hack...
  var ret = "<a href='javascript:run_instant_append_link(\"" ;
  ret += id + "\",\"" ;
  ret += encodeURIComponent ( page ) + "\",\"" ;
  ret += encodeURIComponent ( append ) + "\",\"" ;
  ret += pre + "\"" + ");'>" ;
  ret += linktitle + "</a>" ;

  var span = document.createElement ( "span" ) ;
  span.id = id ;
  span.style.padding = "5px" ;
  span.innerHTML = ret ;
  return span ;
}


function check_instant_append_link () {
  if ( wgAction != "edit" ) return ; // Not an edit page, so no business...
  var ial = instant_append_link_get_param ( "ial" ) ;
  if ( !ial ) return ;

  ial = decodeURIComponent ( ial ) ;
  var ial_pre = instant_append_link_get_param ( "ial_pre" ) ;
  if ( ial_pre == 1 ) document.editform.wpTextbox1.value = ial + document.editform.wpTextbox1.value ;
  else document.editform.wpTextbox1.value += ial ;

  ial = ial.split("\n").join(" ");
  document.editform.wpSummary.value = "Adding \"" + ial + "\" (using [[MediaWiki:GalleryDetails.js|GalleryDetails.js]])" ;
  document.editform.wpSave.click();
  setTimeout("window.close();",5000);
}

function instant_append_link_get_param(paramName, h) {
  if (typeof h == 'undefined' ) h = document.location.href;
  var cmdRe=RegExp('[&?]'+paramName+'=([^&]*)');
  var m=cmdRe.exec(h);
  if (m) {
    try {
      return decodeURI(m[1]);
    } catch (someError) {}
  }
  return null;
}

function run_instant_append_link ( id , page , append , pre ) {
  id = encodeURIComponent ( id ) ;
  append = decodeURIComponent ( append ) ;
  var span = document.getElementById ( id ) ;
  while ( span.firstChild ) span.removeChild ( span.firstChild ) ;
  var url = mw.config.get('wgServer') + mw.config.get('wgScript') + "?title=" + page ;
  url += "&action=edit" ;
  url += "&ial=" + append ;
  if ( pre == 1 ) url += "&ial_pre=1" ;
  var iframe = document.createElement ( "iframe" ) ;
  iframe.src = url ;
  iframe.style.width = "100px" ;
  iframe.style.height = "40px" ;
  span.appendChild ( iframe ) ;
}

// __________________________________________________________________________
// END Instant append link stuff

//</nowiki></pre>