User:Krimpet/CH2.js

From Wikipedia, the free encyclopedia

Note: After saving, you have to bypass your browser's cache to see the changes. Firefox/Mozilla/Safari: hold down Shift while clicking Reload (or press Ctrl-Shift-R), Internet Explorer: press Ctrl-F5, Opera/Konqueror: press F5.

// CommonsHelper Helper (CH²) by Krimpet
// Released into the public domain

var ch2 = null;
var ch2frame = null, ch2framedoc = null;
var newName = "";
var imageTitleRe=/[^:]*:\/\/en\.wikipedia\.org\/(wiki\/|w\/index\.php\?title=)([^&?]*)/;
var imageTitle = unescape(imageTitleRe.exec(decodeURI(location.href))[2].substring("Image:".length));

function ch2Form() {
  ch2.innerHTML = "<b>New Name? (leave blank to keep original): <input id=\"newnamet\"><br /><input id=\"csense\" type=checkbox value=1 /><b>Use WikiSense to suggest categories?</b><br /><button onClick=\"ch2Start();\">Start CommonsHelper</button>";
}

function ch2Start() {
  newName = document.getElementById("newnamet").value;
  var sense = document.getElementById("csense").checked;
  ch2.innerHTML = "<iframe name=\"CH2iframe\" id=\"CH2iframe\" height=\"300\" width=\"100%\"></iframe><br/><button onClick=\"ch2Finalize()\">Click when finished uploading</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\' method=\'post\'>Language code: <input type=text name=language value=\'en\' 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=\'wikipedia\' 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 = "{{subst:ncd" + (newName != "" ? "|Image:" + newName : "") + "}}";
  editBox = document.editform.wpTextbox1;
  editBox.value = editBox.value.replace("\{\{Copy to Wikimedia Commons\}\}", ncd);
  editBox.value = editBox.value.replace("\{\{Move to Wikimedia Commons\}\}", ncd);
  editBox.value = editBox.value.replace("\{\{Move to Commons\}\}", ncd);
  editBox.value = editBox.value.replace("\{\{Move to commons\}\}", ncd);
  editBox.value = editBox.value.replace("\{\{To Commons\}\}", ncd);
  editBox.value = editBox.value.replace("\{\{to Commons\}\}", ncd);
  if (editBox.value.indexOf(ncd) == -1)
    editBox.value += "\n" + ncd;
  document.editform.wpSummary.value = "[[Wikipedia:Moving images to the Commons|Moved image to the Wikimedia Commons]] using [[User:Krimpet/CommonsHelper Helper|CH²]]";
  ch2.innerHTML = "<b>Done! Review everything below to ensure it is correct, and save this page.</b>";
}

function ch2Init() {
  imgString = "Editing Image:";
  if (document.getElementsByTagName('h1')[0].firstChild.nodeValue.substring(0, imgString.length) == imgString) {
    if (document.editform.wpTextbox1.value.indexOf("NowCommons") == -1 &&
        document.editform.wpTextbox1.value.indexOf("subst:ncd") == -1 &&
        document.getElementById("wikiPreview").innerHTML.length == 0) {
      document.getElementById("siteSub").innerHTML +=  "<br><br><div id=\"CH2div\"></div>";
      ch2 = document.getElementById("CH2div");
      ch2.innerHTML = "<button onClick=\"ch2Form();\">Move image to Commons</button>";
    }
  }
}
addOnloadHook(ch2Init);