Colony Helper

Add prev/next links to main colony view page + Mall to buy button

当前为 2015-11-13 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name Colony Helper
  3. // @description Add prev/next links to main colony view page + Mall to buy button
  4. // @namespace bitbucket.org/Odahviing
  5. // @version 1.3
  6. // @include *.war-facts.com/view_colony.php*
  7. // ==/UserScript==
  8.  
  9. // Version history:
  10. // 1.0 Initial Basic Version
  11. // 1.1 Add warning in colony page for lack in malls
  12. // 1.2 Add the ability to buy malls from mail page (inner settings for mall)
  13. // 1.3 Some Bug Fix
  14.  
  15. var checkMall = true;
  16. var mallId = 11694;
  17. var effMall = 230.08;
  18.  
  19. function getQueryString(colonyURL){
  20. var indexPoint = colonyURL.indexOf('?');
  21. if (indexPoint != -1)
  22. colonyURL = colonyURL.substring(indexPoint+1, colonyURL.length);
  23. return colonyURL.replace("colony=", "");
  24. }
  25.  
  26. function getPrevAndNext(currentColonyNumber){
  27. var allColoniesFrame = document.getElementById("colonylist");
  28. var allColonies = allColoniesFrame.getElementsByClassName("colmenu_name");
  29. if (allColonies.length == 1)
  30. return [-1, -1];
  31. for (var index = 0; index < allColonies.length; index ++)
  32. {
  33. var currentValue = getQueryString(allColonies[index].href);
  34. if (currentColonyNumber == currentValue)
  35. {
  36. if (index == 0)
  37. return [-1, getQueryString(allColonies[index+1].href)];
  38.  
  39. if (index == allColonies.length -1)
  40. return [getQueryString(allColonies[index-1].href), -1];
  41. return [getQueryString(allColonies[index-1].href), getQueryString(allColonies[index+1].href)];
  42. }
  43. }
  44. }
  45.  
  46. function buyMalls(people, malled)
  47. {
  48. var baseParams = "build=1&type=3&subtype=8";
  49. var colonyId = document.URL.substring(document.URL.indexOf('?') + 1);
  50. colonyId = colonyId.replace("colony=","");
  51. var missing = people - malled;
  52. var toBuy = Math.ceil(missing / effMall * 40);
  53. baseParams = baseParams + "&buildid=" + mallId + "&colony=" + colonyId + "&amount=" + toBuy;
  54. xhttp = new XMLHttpRequest();
  55. xhttp.open("POST", "build_facility.php", true);
  56. xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
  57. xhttp.send(baseParams);
  58. location.reload();
  59. }
  60.  
  61. function main()
  62. {
  63. var currentColonyNumber = getQueryString(document.URL);
  64. var values = getPrevAndNext(currentColonyNumber);
  65. var colonyText = document.getElementsByClassName("heading bold pagetitle");
  66. if (values[0] != -1)
  67. colonyText[0].innerHTML = "<a href='view_colony.php?colony=" + values[0] + "'><font color='yellow'>Prev&nbsp;&nbsp;&nbsp;&nbsp;</font></a>" + colonyText[0].innerHTML;
  68. if (values[1] != -1)
  69. colonyText[0].innerHTML = colonyText[0].innerHTML + "<a href='view_colony.php?colony=" + values[1] + "'><font color='yellow'>&nbsp;&nbsp;&nbsp;&nbsp;Next</font></a>";
  70. if (checkMall == true)
  71. {
  72. var allBoxes = document.getElementsByClassName('light padding5 tbborder');
  73. var pop = allBoxes[0].innerHTML.split(' ');
  74. var finalPop = parseInt(pop[0].replace("Population:", "").trim());
  75. var malled = allBoxes[5].innerHTML.split(' ');
  76. var finalMalled = parseInt(malled[1].replace("people:","").trim());
  77. if (finalPop * 1.1 > finalMalled)
  78. {
  79. allBoxes[5].innerHTML = "<font color='red'>" + allBoxes[5].innerHTML + "</font><input type='button' id='mallbutton' value='buy'>";
  80. var mainbutton = document.getElementById('mallbutton');
  81. mainbutton.addEventListener("click", function(){buyMalls(finalPop * 1.1, finalMalled)}, false);
  82. }
  83. }
  84. }
  85.  
  86. main();