Jump to content

User:Paranomia/commonsextra.js

From Wikipedia, the free encyclopedia
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.
// <nowiki>
// CommonsHelper Helper (CH²) by Krimpet
// Released into the public domain
// wikEd compatibility patch by Paranomia

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.innerHTML = "<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>";
}

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()\">" + 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 : "") + "}}";
  if (typeof(wikEdUseWikEd) != 'undefined') {
  if (wikEdUseWikEd == true) {
  WikEdUpdateTextarea();
  }
  }
  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;
  if (typeof(wikEdUseWikEd) != 'undefined') {
    if (wikEdUseWikEd == true) {
        WikEdUpdateFrame();
    }
  }
  document.editform.wpSummary.value = ch2_string_EditSummary;
  ch2.innerHTML = "<b>" + ch2_string_Done + "</b>";
}

function ch2Init() {
  if (typeof ch2I18N != "undefined")
    ch2I18N();
  var pageTitle = document.getElementsByTagName('h1')[0].firstChild.nodeValue;
  if (pageTitle.substring(0, ch2_edittitle_Prefix.length) != ch2_edittitle_Prefix ||
      pageTitle.substring(pageTitle.length - ch2_edittitle_Postfix.length) != ch2_edittitle_Postfix)
    return;
  
  imageTitle = pageTitle.substring(ch2_edittitle_Prefix.length, pageTitle.length - ch2_edittitle_Postfix.length)

  if (/*document.editform.wpTextbox1.value.indexOf("NowCommons") == -1 &&
      document.editform.wpTextbox1.value.indexOf("subst:ncd") == -1 &&*/
      document.getElementById("wikiPreview").innerHTML.length == 0) {
    hostDiv = document.getElementById("toolbar");
    if (hostDiv == null) hostDiv = document.getElementById("siteSub");
    hostDiv.innerHTML = "<br><br><div id=\"CH2div\"></div><br>" + hostDiv.innerHTML;
    ch2 = document.getElementById("CH2div");
    ch2.innerHTML = "<button onClick=\"ch2Form();\">" + ch2_string_MoveToCommons + "</button>";
  }
}
addOnloadHook(ch2Init);
// </nowiki>