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.
addOnloadHook(function addtool(location2,link2,name2,id2,comment2){
  // First, lets check the location.
  // Does it equal "p-tb", "toolbox", or "tb"?
  if(location2=="p-tb" || location2=="toolbox" || location2=="tb" || location2=='p-tb' || location2=='toolbox' || location2=='tb') {
    // If so, define the parameter as "p-tb" and then go to step two.
    step2("p-tb",link2,name2,id2,comment2);
  }
  // Ok, so it does not equal any of those.
  // Next, does it equal "p-cactions", "toolbar", or "cactions"?
  else if(location2=="p-cactions" || location2=="toolbar" || location2=="cactions" || location2=='p-cactions' || location2=='toolbar' || location2=='cactions') {
    // If so, define the parameter as "p-cactions" and then go to step two.
    step2("p-cactions",link2,name2,id2,comment2);
  }
  // So it doesn't equal those?
  // Since it doesn't, let us check if it equals "both".
  else if(location2=='both' || location2=="both") {
    // If so, define the parameter as "both" and then go to step two.
    step2("both",link2,name2,id2,comment2);
  }
  // Still nothing? Don't continue.
});

// Next is the check to see if both the "link2" and "name2" parameters are defined.
function step2(location2,link2,name2,id2,comment2) {
  // Are both parameters even defined?
  if(link2 && name2) {
    // If so, continue to step three.
    step3(location2,link2,name2,id2,comment2);
  }
  // If they are not defined, don't continue.
};

// Next, the check to see if "id2" is defined.
function step3(location2,link2,name2,id2,comment2) {
  // Is the "id2" parameter defined?
  if(id2) {
    // If so, does it equal "noid"?
    if(id2=="noid" || id2=='noid') {
      // If so, define as such and continue to step four.
      step4(location2,link2,name2,"noid",comment2);
    }
    // It doesnt? Continue to step four with the parameter specified.
    else {
      step4(location2,link2,name2,id2,comment2);
    }
  }
  // Not defined? Set to "noid" and send to step4().
  else {
    step4(location2,link2,name2,"noid",comment2);
  }
};

// This checks if the "comment2" parameter exists.
function step4(location2,link2,name2,id2,comment2) {
  // Is it defined?
  if(comment2) {
    // If so, send to finalcheck() for final parsing.
    finalcheck(location2,link2,name2,id2,comment2);
  }
  // If it is not defined, set to "name2" and send to finalcheck().
  else {
    finalcheck(location2,link2,name2,id2,name2);
  }
};

// This is the final processing, after all steps are done.
function finalcheck(location2,link2,name2,id2,comment2) {
  // What is "location2"?
  if(location2=="p-tb") {
    // What is "id2"?
    if(id2=="noid") {
      // Add it to the toolbox, with id2 being "ca-" plus "name2" plus "-tb".
      mw.util.addPortletLink("p-tb", link2, name2, "ca-" + name2 + "-tb", comment2, "");
    }
    else {
      mw.util.addPortletLink("p-tb", link2, name2, id2, comment2, "");
    }
  }
  if(location2=="p-cactions") {
    // What is "id2"?
    if(id2=="noid") {
      // Add it to the cactions bar, with id2 being "ca-" plus "name2".
      mw.util.addPortletLink("p-cactions", link2, name2, "ca-" + name2, comment2, "");
    }
    else {
      mw.util.addPortletLink("p-cactions", link2, name2, id2, comment2, "");
    }
  }
  if(location2=="both") {
    // What is "id2"?
    if(id2=="noid") {
      // Add it to both sections.
      mw.util.addPortletLink("p-tb", link2, name2, "ca-" + name2 + "-tb", comment2, "");
      mw.util.addPortletLink("p-cactions", link2, name2, "ca-" + name2, comment2, "");
    }
    else {
      mw.util.addPortletLink("p-tb", link2, name2, id2, comment2, "");
      mw.util.addPortletLink("p-cactions", link2, name2, id2, comment2, "");
    }
  }
};