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.
//Thanks to [[User:2D]] for his [[User:2D/commonsmover.js|commonsmover script]] and [[User:Legoktm]] for his [[User:Legoktm/com2.js|Commons scripts]], upon which parts of this are based.
//See [[User:Drilnoth/filescripts.js/doc]] for details

//Import freesearch.js, which is stored at Commons.
if(mw.config.get('wgCanonicalNamespace') == "" || mw.config.get('wgCanonicalNamespace') == "Category"){
 mw.loader.load('https://commons.wikimedia.org/w/index.php?title=User:Drilnoth/freesearch.js&action=raw&ctype=text/javascript');
}

if(queryString('nowcommons')) addOnloadHook(function() { nowCommons(); })
function nowCommons() {
  var txt = document.getElementById('wpTextbox1');
  if(!txt) return;
  txt.value = txt.value.replace(/\{\{[\s\t\n\r]*(mtc|ctwc|cwc|copy[ -_]to[ -_]wikimedia[ -_]commons|needs[ -_]commons[ -_]category|tocommons|movetocommons|move[ -_]to[ -_]commons|to[ -_]commons|move[ -_]to[ -_]wikimedia[ -_]commons|copy[ -_]to[ -_]commons|move-to-commons|commons[ -_]ok)[\s\t\n\r]*\}\}/ig,'');
  txt.value = '{' + '{' + 'subst:ncd}}\n\n' + txt.value;
  document.getElementById('wpSummary').value += 'Semi-automated image tagging using [[User:Drilnoth/filescripts.js/doc|FileScripts]]';
  document.getElementById('wpSave').click();
}

if(queryString('wronglicense')) addOnloadHook(function() { wrongLicense(); })
function wrongLicense() {
  var txt = document.getElementById('wpTextbox1');
  if(!txt) return;
  txt.value = '{' + '{' + 'Wrong-license}}\n\n' + txt.value;
  document.getElementById('wpSummary').value += 'Semi-automated image tagging using [[User:Drilnoth/filescripts.js/doc|FileScripts]]';
  document.getElementById('wpSave').click();
}

importScript("User:Drilnoth/delresized/tag.js");

if(queryString('notifycommons')) addOnloadHook(function() { notifyCommons(); })
function notifyCommons() {
  var txt = document.getElementById('wpTextbox1');
  if(!txt) return;
  txt.value = txt.value + '==Uploading free images to Wikimedia Commons==' + '\n' + '{' + '{' + 'subst:un-commons}}' + ' ~' + '~' + '~' + '~';
  document.getElementById('wpSummary').value += '/* Uploading free images to Wikimedia Commons */ new section (using [[User:Drilnoth/filescripts.js/doc|FileScripts]])';
  document.getElementById('wpSave').click();
}

function startFileScripts(){
     if(mw.config.get('wgCanonicalNamespace') == "User_talk"){
          var url = mw.config.get('wgServer') + mw.config.get('wgScript') + '?title=' + encodeURIComponent(mw.config.get('wgPageName')) + '&action=edit';
          mw.util.addPortletLink('p-cactions', url + '&notifycommons=true','notify about commons','ca-notifycommons','Tell user about uploading images to Wikimedia Commons');
     } else if(mw.config.get('wgCanonicalNamespace') == "File"){
          mw.util.addPortletLink('p-tb','http://toolserver.org/~commonshelper2/?file=' + encodeURIComponent(mw.config.get('wgTitle')) + '&use_tusc=checked&remove_existing_categories=checked', 'Move to Commons', '', 'Move image to Commons using CommonsHelper');
          mw.util.addPortletLink('p-tb', 'http://toolserver.org/~commonshelper2/?file=' + encodeURIComponent(mw.config.get('wgTitle')) + '&target_file=This file was uploaded accidentally please delete it.jpg&remove_existing_categories=checked', 'Commons description', '', 'Generate a description as if moving image to Commons (useful when completing an incomplete move');
          mw.util.addPortletLink('p-tb', mw.config.get('wgServer') + mw.config.get('wgScript') + '?title=' + encodeURIComponent(mw.config.get('wgPageName')) + '&action=edit&nowcommons=true', 'Now Commons', '', 'Image is now available at Commons');
          mw.util.addPortletLink('p-tb', mw.config.get('wgServer') + mw.config.get('wgScript') + '?title= '+ encodeURIComponent(mw.config.get('wgPageName')) + '&action=edit&reducesupplied=true', 'Old revs deleted', '', 'Old, oversized revisions of fair-use image have been deleted');
     }
}

function queryString(p) {
  var re = RegExp('[&?]' + p + '=([^&]*)');
  var matches;
  if (matches = re.exec(document.location)) {
    try {
      return decodeURI(matches[1]);
    } catch (e) {
    }
  }
  return null;
}
 
$(startFileScripts);