Light Rising AutoFuel

Adds an 'Add Fuel 10x' button to Light Rising Browser Game

  1. // ==UserScript==
  2. // @name Light Rising AutoFuel
  3. // @namespace http://userscripts.org/users/125692
  4. // @description Adds an 'Add Fuel 10x' button to Light Rising Browser Game
  5. // @grant GM_getValue
  6. // @grant GM_setValue
  7. // @include *lightrising.com*game.cgi
  8. // @version 1.1
  9. // ==/UserScript==
  10. //this script adds a 10x add fuel button to light rising browser game.
  11. //button appears only if player has enough sticks and ap.
  12. //currently requires at least 10 of each for button to appear.
  13. //derived from Simons auto search script for SHARTAK browser game.
  14. (function() {
  15. //alert("ffs");
  16.  
  17. //console.log("AutoFuel Script up and running");
  18. //FIRST SEE IF SCRIPT SHOULD BE RUN
  19. var fuelbutton=document.evaluate( "//input[@value='Add Fuel']", document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null).snapshotItem(0);
  20. if(!fuelbutton){
  21. return;//stop if the fuelbuttons not there.
  22. };
  23. //console.log("Have Fuel button");
  24.  
  25.  
  26. //THEN DO SCRIPT
  27. //constants
  28. var MAXNUMFUELS=10;
  29.  
  30.  
  31. if (!this.GM_getValue || (this.GM_getValue.toString && this.GM_getValue.toString().indexOf("not supported")>-1)) {
  32. this.GM_getValue=function (key,def) {
  33. return localStorage[key] || def;
  34. };
  35. this.GM_setValue=function (key,value) {
  36. return localStorage[key]=value;
  37. };
  38. this.GM_deleteValue=function (key) {
  39. return delete localStorage[key];
  40. };
  41. }
  42.  
  43. //variables
  44. var resourcelvl=-1;//current pages resource lvl (assuming we have fuel results)
  45. var fuelresult;//currentpages fuel result (assuming we have fuel results)
  46. var arewefueling;//boolean are we?
  47.  
  48.  
  49.  
  50. //functions
  51. function outputfuelresults(texttoadd){
  52. //texttoadd='ddd';
  53. var gib=document.getElementsByClassName("gamebox infobox")[0];
  54. var fueltextelement=gib.getElementsByTagName('b')[0];
  55. var divwewant=gib.firstElementChild;
  56. var rawtext=GM_getValue('GMallfuelresults','');
  57. GM_setValue('GMallfuelresults','');
  58. //var rawtext='text\nline1\nline2\nline3\nline4';
  59. var arraytext=rawtext.split('\n');
  60. arraytext[0]=texttoadd;//this can be over written as its not a fuel result
  61. var textline= document.createElement("p")//where we will place the results
  62. for (i=0;i<arraytext.length;i++){//populating results
  63. textline.innerHTML=textline.innerHTML+arraytext[i]+'<br>';
  64. }
  65. divwewant.insertBefore(textline,fueltextelement.nextSibling );
  66. }
  67.  
  68. function cleanupforexit(){
  69. GM_setValue('GMarewefueling',false);//not fueling
  70. GM_setValue('GMallfuelresults','');//clear old results
  71. GM_setValue('GMnumoffuelsleft',0);
  72. return 1
  73. }
  74.  
  75. //SCRIPT START
  76. var numberoffuels=MAXNUMFUELS;//this is the actual number of refuel attempts
  77. //we have to store another GM value. number of attempts if we do this. too hard for now
  78. //get number of sticks we have.
  79. var ginvbox=document.getElementsByClassName("gamebox invbox")[0];
  80. //we are looking for <div title="a sturdy wooden stick">
  81. var stickdiv=document.evaluate( "//div[@title='a sturdy wooden stick']", ginvbox, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null).snapshotItem(0);
  82. if(!stickdiv){//no sticks. whats the point. abort!
  83. return;
  84. }
  85. //otherwise we set number of time to repeat to min of 10 or number of sticks.(unless one stick
  86. var numofsticks=Number(stickdiv.textContent.match(/(\d+)/)[1]);
  87. /*if (numofsticks==1){
  88. return;
  89. }
  90. else{
  91. numberoffuels=Math.min(numofsticks,MAXNUMFUELS);
  92. }*/
  93. //setupthebutton
  94. //grab ap as we make the button invisible if not enough ap to do whole fuel
  95. var gsb=document.getElementsByClassName("gamebox statsbox")[0];
  96. var fueltextelement=gsb.getElementsByTagName('b');
  97. var aptext=fueltextelement[1];//second bold thing is ap.
  98. var test=aptext.innerHTML.match(/(\d+)[^0-9]*(\d+)/) // extract the first 2 numbers
  99. var availableap=Number(test[1]);
  100. //console.log("running ap=",availableap);
  101. //make new autofuel button by cloning fuel button
  102. fuel10buttonform=fuelbutton.parentNode.cloneNode(true);//make a new copy of the fuel button
  103. fuelbutton.parentNode.parentNode.insertBefore(fuel10buttonform,fuelbutton.parentNode.nextSibling);
  104. fuel10button=fuel10buttonform.firstElementChild;
  105. fuel10button.setAttribute("value", "Add Fuel "+numberoffuels+ "x");
  106. if (numberoffuels>availableap||numberoffuels>numofsticks){//we don't have enough ap or sticks so hide button
  107. fuel10button.style.display='none';
  108. }
  109. fuel10buttonform.id="AutoFuelForm";//id so we can easy find it later
  110. fuel10button.addEventListener("click", function(event) {GM_setValue('GMarewefueling',true);
  111. GM_setValue('GMnumoffuelsleft',numberoffuels);},true);
  112.  
  113. //console.log("button set up");
  114. var arewefueling=GM_getValue('GMarewefueling',false);
  115. //console.log("are we fueling=",arewefueling);
  116.  
  117. if(arewefueling){
  118. //console.log("fueling");
  119. //well then there aught to be results to gather.
  120. var gib=document.getElementsByClassName("gamebox infobox")[0];
  121. var fueltextelement=gib.getElementsByTagName('b');
  122. if (fueltextelement.length>0){//foundsomething
  123. //console.log("found fuel text");
  124. var fueltext=fueltextelement[0].innerHTML;
  125. var foundtext=fueltext.match(/([^.]+.)/);//finds the first sentence
  126. //console.log("found fuel text:",foundtext[1]);
  127. if (GM_getValue("GMallfuelresults",false)) {
  128. GM_setValue("GMallfuelresults", GM_getValue("GMallfuelresults","GM_getValue Error") +
  129. "\n" + foundtext[1]);
  130. }
  131. else{
  132. GM_setValue("GMallfuelresults", "\n" + foundtext[1]);
  133. }
  134. //console.log("attempted to store fuel results");
  135. var numoffuelsleft=Number(GM_getValue('GMnumoffuelsleft',0));//so we only get it once
  136. var outstring="";
  137. var fueled=(numberoffuels-numoffuelsleft+1);
  138. //console.log("testing reasons to stop. numoffuelsleft=",numoffuelsleft);
  139. if (gib.innerHTML.match(/too hot to approach/)){//stop fueling as too hot
  140. //console.log("too hot");
  141. if (fueled==1){//we tried to add fuel but it was too hot on the first attempt
  142. outstring="You tried to add a lot of fuel but couldn't even begin as the fire is too hot.";
  143. }
  144. else{
  145. outstring="You added fuel " + ((fueled-1)==1?'once':(fueled-1)+' times') + " before stopping as the fire became too hot.";
  146. }//outstring=outstring.replace(/1 times/,'once');
  147. outputfuelresults(outstring);
  148. return cleanupforexit();//quit script
  149. }
  150. else if (numoffuelsleft==1){//time to stop on 1 because fuel then reduce counter
  151. //console.log("done full ammount");
  152. //alert("2");
  153. outputfuelresults("You added fuel " + (numberoffuels-numoffuelsleft+1) + " times.");
  154. return cleanupforexit();//quit script
  155. }
  156. else if (gib.innerHTML.match(/have any sticks/)){
  157. //alert("3");
  158. //console.log("out of sticks");
  159. outputfuelresults("You added fuel " + (fueled==1?'once':fueled+' times') + " before stopping as you have run out of fuel.");
  160. return cleanupforexit();//quit script
  161. }
  162. else{//we should fuel again!
  163. //console.log("TRY REFUELING");
  164. GM_setValue('GMnumoffuelsleft',numoffuelsleft-1)//decrement fuelcounter
  165. document.getElementById('AutoFuelForm').submit();//fuel
  166. }
  167. }
  168. }
  169.  
  170. //console.log("at the end");
  171. //EOF
  172. })();