BuyPitaCrap

By Rick Scott Last update Apr 11, 2009 — Installed 51 times. Daily Installs: 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 1, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0

There are 3 previous versions of this script.

// ==UserScript==
// @name           BuyPitaCrap
// @namespace      DoorbellsDingDong
// @description    Lets you buy crap that's usually a PITA to buy (like hippy fruits and knob seltzer)
// @include        *.kingdomofloathing.com/compactmenu*
// @include        *127.0.0.1*/compactmenu*
// @include        *.kingdomofloathing.com/topmenu*
// @include        *127.0.0.1*/topmenu*
// @version        0.1
// ==/UserScript==

var sEquipmentHtml = "";
var sBackupOutfitId = "";
var aAllCells = document.getElementsByTagName("td");
var eMessageSpan = document.createElement("span");
eMessageSpan.id = "MessageSpan";
var eQuantityOptionsSpan = document.createElement("span");
eQuantityOptionsSpan.id = "QuantityOptionsSpan";
var eBuyCrapDdl = document.createElement("select");

ShowProgress("Initializing");

for(var i=0; i<aAllCells.length; ++i)
{
  if(aAllCells[i].innerHTML != null && aAllCells[i].innerHTML.indexOf("http://asymmetric.net") > 0)
  {
    aAllCells[i].innerHTML = "";
    eBuyCrapDdl.name = "BuyCrapDdl";
    eBuyCrapDdl.options [0] = new Option(" - Buy Crap - ","-1");
    eBuyCrapDdl.options [1] = new Option("KG-Seltzer (mp)","Seltzer_5_g_344");
    eBuyCrapDdl.options [2] = new Option("KG-Nasal Spray (meat)","NasalSpray_5_g_1515");
    eBuyCrapDdl.options [3] = new Option("KG-Eyedrops (items)","Eyedrops_5_g_1514");
    eBuyCrapDdl.options [4] = new Option("KG-Pet-Buffing Spray","BuffingSpray_5_g_1512");
    
    eBuyCrapDdl.options [5] = new Option("Hippy-Herbs","Herbs_2_h_203");
    eBuyCrapDdl.options [6] = new Option("Hippy-Grapefruit","Grapefruit_2_h_243");
    eBuyCrapDdl.options [7] = new Option("Hippy-Lemon","Lemon_2_h_332");
    eBuyCrapDdl.options [8] = new Option("Hippy-Olive","Olive_2_h_245");
    eBuyCrapDdl.options [9] = new Option("Hippy-Orange","Orange_2_h_242");
    eBuyCrapDdl.options [10] = new Option("Hippy-Strawberry","Strawberry_2_h_786");
    eBuyCrapDdl.options [11] = new Option("Hippy-Tomato","Tomato_2_h_246");

    eBuyCrapDdl.options [12] = new Option("Market-Ben-Gal Balm","BenGalBalm_0_m_2595");
    eBuyCrapDdl.options [13] = new Option("Market-Hair Spray","HairSpray_0_m_744");
    eBuyCrapDdl.options [14] = new Option("Market-Chewing Gum","ChewingGumOnAString_0_m_23");
    eBuyCrapDdl.options [15] = new Option("Market-Fortune Cookie","FortuneCookie_0_m_61");
    eBuyCrapDdl.options [16] = new Option("Market-Soda Water","SodaWater_0_m_1003");
    eBuyCrapDdl.options [17] = new Option("Market-Hermit Permit","HermitPermit_0_m_42");

    eBuyCrapDdl.addEventListener("change", ShowQuantityOptions, true);
    aAllCells[i].appendChild(eBuyCrapDdl);
    aAllCells[i].appendChild(eQuantityOptionsSpan);
    aAllCells[i].appendChild(eMessageSpan);
    break;
  }
}

ShowProgress("");


var sItemName = "";
var sOutfitId = "";
var sStore = "";
var sItemId = "";
var iQuantity = 0;

function ShowQuantityOptions()
{
  var e = document.getElementsByName("BuyCrapDdl")[0];
  var sSelectedItem = e.options[e.selectedIndex].value;
  if(sSelectedItem == "-1")
  {
    eQuantityOptionsSpan.innerHTML = "";
    return;
  }
  asParts = sSelectedItem.split("_");
  sItemName = asParts[0];
  sOutfitId = asParts[1];
  sStore = asParts[2];
  sItemId = asParts[3];
  
  if(sOutfitId == "0")
  {
    ShowProgress("Visiting the store");
    ShowQuantityOptions2(null);
  }
  else
  {
    ShowProgress("Determining available outfits");
    GM_xmlhttpRequest({method:"GET", url:"http://" + window.location.host + "/inventory.php?which=2&ajax=1", onload:ShowQuantityOptions2});
  }
}

function ShowQuantityOptions2(response)
{
  if(response != null)
  {
    if(response.readyState != 4)
      return;
    sEquipmentHtml = response.responseText;
  }

  eQuantityOptionsSpan.innerHTML = "";
  eQuantityOptionsSpan.style.margin = "5px";
  
  // stores g and h should never happen with an outfit ID of 0
  if(sStore == "g" && sEquipmentHtml.indexOf("Knob Goblin Elite Guard Uniform") < 1)
  {
    eQuantityOptionsSpan.innerHTML = "No KGE outfit";
    ShowProgress("");
    return;
  }

  if(sStore == "h" && sEquipmentHtml.indexOf("Filthy Hippy Disguise") < 1 && sEquipmentHtml.indexOf("War Hippy Fatigues") < 1)
  {
    eQuantityOptionsSpan.innerHTML = "No Filthy Hippy Disguise";
    ShowProgress("");
    return;
  }

  var eBuyOne = document.createElement("a");
  eBuyOne.name = "BuyOnePieceOfCrap";
  eBuyOne.href = "javascript:";
  eBuyOne.innerHTML = "[x1]";
  eBuyOne.style.margin = "3px";
  eBuyOne.addEventListener("click", BuyOneCrap, true);
  eQuantityOptionsSpan.appendChild(eBuyOne);

  var eBuyThree = document.createElement("a");
  eBuyThree.name = "BuyThreePiecesOfCrap";
  eBuyThree.href = "javascript:";
  eBuyThree.innerHTML = "[x3]";
  eBuyThree.style.margin = "3px";
  eBuyThree.addEventListener("click", BuyThreeCraps, true);
  eQuantityOptionsSpan.appendChild(eBuyThree);

  var eBuyTen = document.createElement("a");
  eBuyTen.name = "BuyTenPiecesOfCrap";
  eBuyTen.href = "javascript:";
  eBuyTen.innerHTML = "[x10]";
  eBuyTen.style.margin = "3px";
  eBuyTen.addEventListener("click", BuyTenCraps, true);
  eQuantityOptionsSpan.appendChild(eBuyTen);

  var eBuyThirty = document.createElement("a");
  eBuyThirty.name = "BuyThirtyPiecesOfCrap";
  eBuyThirty.href = "javascript:";
  eBuyThirty.innerHTML = "[x30]";
  eBuyThirty.style.margin = "3px";
  eBuyThirty.addEventListener("click", BuyThirtyCraps, true);
  eQuantityOptionsSpan.appendChild(eBuyThirty);

  ShowProgress("");
}

function BuyOneCrap()
{
  iQuantity = 1;
  BuyCrap();
}

function BuyThreeCraps()
{
  iQuantity = 3;
  BuyCrap();
}

function BuyTenCraps()
{
  iQuantity = 10;
  BuyCrap();
}

function BuyThirtyCraps()
{
  iQuantity = 30;
  BuyCrap();
}


function BuyCrap()
{
  eQuantityOptionsSpan.innerHTML = "";
  
  // if no special outfit is required, just buy the item and skip to the end
  if(sOutfitId == "0")
  {
    ShowProgress("Buying your crap");
  
    GM_xmlhttpRequest
    ({
      method:"POST",
      url:"http://" + window.location.host + "/store.php",
      headers:{"Content-type":"application/x-www-form-urlencoded"},
      onload:BuyCrap7,
      data:encodeURI("phash=" + GetPassword() + "&whichstore=" + sStore + "&buying=Yep.&whichitem=" + sItemId + "&howmany=" + iQuantity)
    });
  }
  else
  {
    ShowProgress("Creating Backup Outfit");
    GM_xmlhttpRequest({method:"GET", url:"http://" + window.location.host + "/inv_equip.php?action=customoutfit&outfitname=BackupDoorbell&ajax=1", onload:BuyCrap2});
  }
}


function BuyCrap2(response)
{
  if(response.readyState != 4)
    return;
  ShowProgress("Reticulating Splines"); // sim city joke...actually getting the inventory equipment page for scraping
  GM_xmlhttpRequest({method:"GET", url:"http://" + window.location.host + "/inventory.php?which=2&ajax=1", onload:BuyCrap3});
}

function BuyCrap3(response)
{
  if(response.readyState != 4)
    return;
  
  sEquipmentHtml = response.responseText;
  sBackupOutfitId = StringBetween(sEquipmentHtml.substr(sEquipmentHtml.indexOf("BackupDoorbell") - 10), "=", ">");

  ShowProgress("Saving current Outfit");
  GM_xmlhttpRequest({method:"GET", url:"http://" + window.location.host + "/inv_equip.php?action=customoutfit&outfitname=BackupDoorbell&ajax=1", onload:BuyCrap4});
}

function BuyCrap4(response)
{
  if(response.readyState != 4)
    return;

  ShowProgress("Putting on Outfit # " + sOutfitId);
  GM_xmlhttpRequest({method:"GET", url:"http://" + window.location.host + "/inv_equip.php?action=outfit&whichoutfit=" + sOutfitId, onload:BuyCrap5 });
}

function BuyCrap5(response)
{
  if(response != null && response.readyState != 4)
    return;

  ShowProgress("Buying your crap");
  
  GM_xmlhttpRequest
  ({
    method:"POST",
    url:"http://" + window.location.host + "/store.php",
    headers:{"Content-type":"application/x-www-form-urlencoded"},
    onload:BuyCrap6,
    data:encodeURI("phash=" + GetPassword() + "&whichstore=" + sStore + "&buying=Yep.&whichitem=" + sItemId + "&howmany=" + iQuantity)
  });
}

function BuyCrap6(response)
{
  if(response.readyState != 4)
    return;
  ShowProgress("Put your clothes back on!");
  GM_xmlhttpRequest({method:"GET", url:"http://" + window.location.host + "/inv_equip.php?action=outfit&whichoutfit=" + sBackupOutfitId, onload:BuyCrap7 });
}

function BuyCrap7(response)
{
  if(response.readyState != 4)
    return;

  document.getElementsByName("BuyCrapDdl")[0].selectedIndex = 0;
  
  eQuantityOptionsSpan.style.margin = "5px";
  
  var eText = document.createTextNode("Bought " + iQuantity + "x" + sItemName);
  eQuantityOptionsSpan.appendChild(eText);
  
  if(sStore == "g" || sStore == "m")
  {
    if(sItemName.indexOf("ChewingGum") >= 0)
    {
      var eSewer = document.createElement("a");
      eSewer.name = "SewerForCrap";
      eSewer.href = "javascript:window.top.document.getElementsByName('mainpane')[0].contentDocument.location.href='sewer.php';location.reload();";
      eSewer.innerHTML = "[sewer]";
      eSewer.style.margin = "3px";
      eQuantityOptionsSpan.appendChild(eSewer);
    }
    else if(sItemName.indexOf("HermitPermit") >= 0)
    {
      var eHermit = document.createElement("a");
      eHermit.name = "GetHermitsCrap";
      eHermit.href = "javascript:window.top.document.getElementsByName('mainpane')[0].contentDocument.location.href='hermit.php';location.reload();";
      eHermit.innerHTML = "[hermit]";
      eHermit.style.margin = "3px";
      eQuantityOptionsSpan.appendChild(eHermit);
    }
    else
    {
      var eUseOne = document.createElement("a");
      eUseOne.name = "UseOnePieceOfCrap";
      eUseOne.href = "javascript:";
      eUseOne.innerHTML = "[use 1]";
      eUseOne.style.margin = "3px";
      eUseOne.addEventListener("click", UseOnePieceOfCrap, true);
      eQuantityOptionsSpan.appendChild(eUseOne);

      if(iQuantity > 1)
      {
        var eUseAll = document.createElement("a");
        eUseAll.name = "UseAllPiecesOfCrap";
        eUseAll.href = "javascript:";
        eUseAll.innerHTML = "[use " + iQuantity + "]";
        eUseAll.style.margin = "3px";
        eUseAll.addEventListener("click", UseAllPiecesOfCrap, true);
        eQuantityOptionsSpan.appendChild(eUseAll);
      }
    }
  }
  else if(sStore == "h")
  {
    var eCook = document.createElement("a");
    eCook.name = "CookCrap";
    eCook.href = "javascript:window.top.document.getElementsByName('mainpane')[0].contentDocument.location.href='craft.php?mode=cook';location.reload();";
    eCook.innerHTML = "[cook]";
    eCook.style.margin = "3px";
    eQuantityOptionsSpan.appendChild(eCook);

    if(sItemName.indexOf("Herbs") < 0)
    {
      var eMix = document.createElement("a");
      eMix.name = "MixCrap";
      eMix.href = "javascript:window.top.document.getElementsByName('mainpane')[0].contentDocument.location.href='craft.php?mode=cocktail';location.reload();";
      eMix.innerHTML = "[mix]";
      eMix.style.margin = "3px";
      eQuantityOptionsSpan.appendChild(eMix);
    }
  }

  ShowProgress("");
}

function UseOnePieceOfCrap()
{
  GM_xmlhttpRequest
  ({
    method:"GET",
    url:"http://" + window.location.host + "/inv_use.php?pwd=" + GetPassword() + "&which=1&whichitem=" + sItemId,
    headers:{"Content-type":"application/x-www-form-urlencoded"},
    onload:CrapHasBeenUsed,
  });
}

function UseAllPiecesOfCrap()
{
  GM_xmlhttpRequest
  ({
    method:"POST",
    url:"http://" + window.location.host + "/multiuse.php",
    headers:{"Content-type":"application/x-www-form-urlencoded"},
    onload:CrapHasBeenUsed,
    data:encodeURI("action=useitem&pwd=" + GetPassword() + "&quantity=" + iQuantity + "&whichitem=" + sItemId)
  });
}

function CrapHasBeenUsed(response)
{
  if(response.readyState != 4)
    return;
  eQuantityOptionsSpan.innerHTML = "";
  ShowProgress("You have used " + iQuantity + "x" + sItemName);
  window.top.document.getElementsByName('charpane')[0].contentDocument.location.reload();
  setTimeout("document.getElementById('MessageSpan').innerHTML = ''", 5000);
}


function ShowProgress(sMessage)
{
  eMessageSpan.innerHTML = "&nbsp;&nbsp;" + sMessage;
}






// This function attempts to get the "PWDHASH" from the charpane, which is useful for chat functions and other stuff.
// Note that this function CANNOT BE CALLED during page initialization because the charpane isn't loaded at that point. The best you can do is setTimeout(GetPassword, 500) or something.
function GetPassword()
{
  var html = window.top.document.getElementsByName('charpane')[0].contentDocument.documentElement.innerHTML;
  return StringBetween(html.substr(html.indexOf("pwdhash"), 100), "\"", "\"");
}


// This function attempts to get the Player ID from the menupane, which is useful for chat functions and other stuff.
// Note that this function CANNOT BE CALLED during page initialization because the charpane isn't loaded at that point. The best you can do is setTimeout(GetPlayerId, 500) or something.
function GetPlayerId()
{
  var html = window.top.document.getElementsByName('menupane')[0].contentDocument.documentElement.innerHTML;
  return StringBetween(html.substr(html.indexOf("donatepopup.php?pid"), 100), "=", "\"");
}


// This function returns the string between 2 given characters.
// The first param, sInput, is the string to search
// The second param, sStart, is the start of the string you want. This string will NOT be included in the result
// The third param, sEnd, is the end of the string you want. This string will also NOT be included in the result
// Note: The function will only look for the first occurence of sStart, but will start at the END of that string to find sEnd.
// Example: StringBetween("ABC(2) DEF(3) GHI(4)", "DEF(", ")"); will return "3"
function StringBetween(sInput, sStart, sEnd)
{
  if(sInput == null)
    return null;
    
  var iStart = sInput.indexOf(sStart);
  if(iStart < 0)
    return null;
  
  iStart += sStart.length;

  if(sEnd != null)
  {
    var iEnd = sInput.indexOf(sEnd, iStart);
    if(iEnd < 0)
      return null;
    
    return sInput.substring(iStart, iEnd);
  }
  else
    return sInput.substring(iStart);
}