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.
// CommonsHelper Helper (CH²) by Krimpet
// Released into the public domain

var ch2_wplanguage = "en";
var ch2_wpproject = "wikipedia";
var ch2_chlanguage = "en";
var ch2_edittitle_Prefix = "Editing Image:";
var ch2_edittitle_Postfix = "";
var ch2_string_SpecifyNewName = "New Name? (leave blank to keep original):";
var ch2_string_UseWikiSense = "Use WikiSense to suggest categories?";
var ch2_string_StartCommonsHelper = "Start CommonsHelper";
var ch2_string_FinishedUploading = "Click when finished uploading";
var ch2_string_Done = "Done! Review everything below to ensure it is correct, and save this page.";
var ch2_string_MoveToCommons = "Move image to Commons";
var ch2_string_EditSummary = "[[Wikipedia:Moving images to the Commons|Moved image to the Wikimedia Commons]] using [[User:Krimpet/CommonsHelper Helper|CH²]]";
var ch2_template_ncd = "subst:ncd";
var ch2_templates_copy = new Array(
  "Copy to Wikimedia Commons",
  "Move to Wikimedia Commons",
  "Move to Commons",
  "Move to commons",
  "To Commons",
  "to Commons"
);
var ch2_templates_embed = new Array(
  "PD-USGov-HHS-CDC",
  "PD-USGov-USDA-ARS"
);

var ch2 = null;
var ch2frame = null, ch2framedoc = null;
var newName = "";
var imageTitle = "";

function ch2Form() {
  ch2 = document.createElement('div');
  ch2.setAttribute("style", "margin-bottom: 2em;");
  ch2.innerHTML = "<h2>"+ch2_string_MoveToCommons+"</h2><p><b>" + ch2_string_SpecifyNewName + " <input id=\"newnamet\"><br /><input id=\"csense\" type=checkbox value=1 /><b>" + ch2_string_UseWikiSense + "</b><br /><button onClick=\"ch2Start();\">" + ch2_string_StartCommonsHelper + "</button></p>";
  document.getElementById('wikiPreview').appendChild(ch2);
}

function ch2Start() {
  newName = document.getElementById("newnamet").value;
  var sense = document.getElementById("csense").checked;
  ch2.innerHTML = "<iframe name=\"CH2iframe\" id=\"CH2iframe\" height=\"300px\" width=\"100%\"></iframe><br/><button onClick=\"ch2Finalize()\">" + ch2_string_FinishedUploading + "</button>";
  ch2frame = document.getElementById("CH2iframe");
  ch2framedoc = ch2frame.contentDocument;
  if (ch2framedoc == undefined || ch2framedoc == null)
    ch2framedoc = ch2frame.contentWindow.document;
  ch2framedoc.open();
  ch2framedoc.write("Enter a wikipedia language code and image name, and it will generate an image description to copy-and-paste for the commons upload form.</small><br/><form action=\'http://tools.wikimedia.de/~magnus/commonshelper.php?interface=" + ch2_chlanguage + "\' method=\'post\'>Language code: <input type=text name=language value=\'" + ch2_wplanguage + "\' cols=4/><br/>Image name: <input type=text name=image value=\'" + imageTitle + "\' cols=30/><br/>New name: <input type=text name=newname value=\'" + newName + "\' cols=30/> (for direct upload only; leave blank to keep original name)<br/>Project: <input type=text name=project value=\'" + ch2_wpproject + "\' cols=30/> (may also be wikibooks or wikisource)<br/><input type=checkbox name=commonsense value=1 " + (sense ? "checked" : "") + " />Use WikiSense to suggest categories<br/><input type=submit name=doit value=\'Get text\'/></form><script>document.forms[0].doit.click();</script>");
  ch2framedoc.close();
}

function ch2Finalize() {
  ncd = "{{" + ch2_template_ncd + (newName != "" ? "|Image:" + newName : "") + "}}";
  editBox = document.editform.wpTextbox1;

  for (var i = 0; i < ch2_templates_copy.length; i++)
    editBox.value = editBox.value.replace("\{\{" + ch2_templates_copy[i] + "\}\}", ncd);

  for (var i = 0; i < ch2_templates_embed.length; i++)
    editBox.value = editBox.value.replace("\{\{" + ch2_templates_embed[i] + "\}\}", "\{\{" + ch2_templates_embed[i] + "|commons=" + ncd + "\}\}");
  
  if (editBox.value.indexOf(ncd) == -1)
    editBox.value += "\n" + ncd;
  document.editform.wpSummary.value = ch2_string_EditSummary;
  ch2.innerHTML = "<b>" + ch2_string_Done + "</b>";
}

function ch2Init() {
  if (typeof ch2I18N != "undefined")
    ch2I18N();
  
  if (mw.config.get('wgNamespaceNumber') == 6) {
    if (mw.config.get('wgAction') == "edit") {
      if (window.location.href.search("&ch2=yes") != -1) {
        ch2Form();
      } else {
        mw.util.addPortletLink("p-cactions", "javascript:ch2Form()", ch2_string_MoveToCommons, "ca-ch2");
      }
    } else {
      mw.util.addPortletLink("p-cactions", mw.config.get('wgServer')+mw.config.get('wgScript')+"?title="+encodeURIComponent( mw.config.get('wgPageName') )+"&action=edit&ch2=yes", ch2_string_MoveToCommons, "ca-ch2");
    }
  }
}
$(ch2Init);