Jump to content

User:Living Concrete/sandboxreset.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.
/* Script works with Template:X* and Wikipedia:Sandbox, as well as the introduction sandboxes. */
/* Based off of [[User:VoA/monobook.js]]. */
addOnloadHook(function() {
  if(wgPageName == "Wikipedia:Sandbox" || (wgPageName.lastIndexOf("Wikipedia:Tutorial") != -1 && wgPageName.lastIndexOf("/sandbox") != -1))
  {
    mw.util.addPortletLink('p-cactions', "javascript:sandboxReset(0)", "reset", "ca-sand", "Reset the Sandbox", "");
    mw.util.addPortletLink('p-cactions', "javascript:sandboxReset(1)", "rake", "ca-sand", "Rake in Sandbox Header", "");

    if(document.URL.lastIndexOf("&sandboxautoreset=true") == -1)
      return;

    var form = document.editform;
    if(document.URL.lastIndexOf("&rake=yes") == -1) {
      if(wgPageName.lastIndexOf("Wikipedia:Tutorial") != -1 && wgPageName.lastIndexOf("/sandbox") != -1)
        form.wpTextbox1.value = "{{Please leave this line alone (tutorial sandbox heading)}}\n" + 
"<!-- Hello! Feel free to try your formatting and editing skills below this line. As this page is for editing experiments, this page will automatically be cleaned every 12 hours. -->";
      else
        form.wpTextbox1.value = "{{Please leave this line alone (sandbox heading)}}\n" + 
"<!-- Hello! Feel free to try your formatting and editing skills below this line. As this page is for editing experiments, this page will automatically be cleaned every 12 hours. -->";
      form.wpSummary.value = "[[User:Ember of Light/sandboxreset.js|JavaScript assisted edit]]: Resetting the sandbox.";
      form.wpSave.click();
    }
    else if(document.URL.lastIndexOf("&rake=no") == -1) {
      if(wgPageName.lastIndexOf("Wikipedia:Tutorial") != -1 && wgPageName.lastIndexOf("/sandbox") != -1) {
        if(form.wpTextbox1.value.lastIndexOf("{{Please leave this line alone (tutorial sandbox heading)}}") != -1) {
          alert("Heading already exists on page. No need for a rake.");
          return;
        }
      } else {
        if(form.wpTextbox1.value.lastIndexOf("{{Please leave this line alone (sandbox heading)}}") != -1) {
          alert("Heading already exists on page. No need for a rake.");
          return;
        }
      }

      if(wgPageName.lastIndexOf("Wikipedia:Tutorial") != -1 && wgPageName.lastIndexOf("/sandbox") != -1)
        form.wpTextbox1.value = "{{Please leave this line alone (tutorial sandbox heading)}}\n" + 
"<!-- Hello! Feel free to try your formatting and editing skills below this line. As this page is for editing experiments, this page will automatically be cleaned every 12 hours. -->\n" + form.wpTextbox1.value;
      else
        form.wpTextbox1.value = "{{Please leave this line alone (sandbox heading)}}\n" + 
"<!-- Hello! Feel free to try your formatting and editing skills below this line. As this page is for editing experiments, this page will automatically be cleaned every 12 hours. -->\n" + form.wpTextbox1.value;
      form.wpSummary.value = "[[User:Ember of Light/sandboxreset.js|JavaScript assisted edit]]: Rake in sandbox header.";
      form.wpSave.click();
    }
  }
  else if(wgPageName == "Template:X1" || wgPageName == "Template:X2" || wgPageName == "Template:X3" || wgPageName == "Template:X4" || wgPageName == "Template:X5" || wgPageName == "Template:X6" || wgPageName == "Template:X7" || wgPageName == "Template:X8" || wgPageName == "Template:X9" || wgPageName == "Template:Template_sandbox") {
    mw.util.addPortletLink('p-cactions', "javascript:sandboxReset(0)", "reset", "ca-sand", "Reset the Sandbox", "");
    mw.util.addPortletLink('p-cactions', "javascript:sandboxReset(1)", "rake", "ca-sand", "Rake in Sandbox Header", "");
    if(document.URL.lastIndexOf("&sandboxautoreset=true") == -1)
      return;

    var form = document.editform;
    if(document.URL.lastIndexOf("&rake=yes") == -1) {
      form.wpTextbox1.value = "This sandbox is itself a template.  This sandbox is for experimenting with templates.\n" + "{{Please leave this line alone (template sandbox heading)}}\n\n" + "If you defined parameters such as <tt><nowiki>{{Template sandbox|First|Second|name=\"Named\"}}</nowiki></tt>:\n" + ";First:{{{1}}}\n" + ";Second:{{{2}}}\n" + ";Name:{{{name}}}\n\n" + "----\n" + "<!-- Hello!  Feel free to try your formatting and editing skills below this line.  As this page is for editing experiments, this page will automatically be cleaned every 12 hours. -->";
      form.wpSummary.value = "[[User:Ember of Light/sandboxreset.js|JavaScript assisted edit]]: Resetting the sandbox.";
      form.wpSave.click();
    }
    else if(document.URL.lastIndexOf("&rake=no") == -1) {
      if(form.wpTextbox1.value.lastIndexOf("{{Please leave this line alone (template sandbox heading)}}") != -1) {
        alert("Heading already exists on page. No need for a rake.");
        return;
      }

      form.wpTextbox1.value = "This sandbox is itself a template.  This sandbox is for experimenting with templates.\n" + "{{Please leave this line alone (template sandbox heading)}}\n\n" + "If you defined parameters such as <tt><nowiki>{{Template sandbox|First|Second|name=\"Named\"}}</nowiki></tt>:\n" + ";First:{{{1}}}\n" + ";Second:{{{2}}}\n" + ";Name:{{{name}}}\n\n" + "----\n" + "<!-- Hello!  Feel free to try your formatting and editing skills below this line.  As this page is for editing experiments, this page will automatically be cleaned every 12 hours. -->\n" + form.wpTextbox1.value;
      form.wpSummary.value = "[[User:Ember of Light/sandboxreset.js|JavaScript assisted edit]]: Rake in sandbox header.";
      form.wpSave.click();
    }
  }
});

function sandboxReset(num) {
  if(num == 0)
    location.assign("/w/index.php?title=" + wgPageName + "&action=edit&sandboxautoreset=true&rake=no");
  else
    location.assign("/w/index.php?title=" + wgPageName + "&action=edit&sandboxautoreset=true&rake=yes");
}