Light Rising AutoSearch

Adds 'Search 10x' button

  1. // ==UserScript==
  2. // @name Light Rising AutoSearch
  3. // @namespace http://userscripts.org/users/125692
  4. // @description Adds 'Search 10x' button
  5. // @include *lightrising.com*game.cgi
  6. // @version 0.0.1.20140406170310
  7. // ==/UserScript==
  8.  
  9. (function() {
  10.  
  11. //constants
  12. var numberofsearches=10;//number of times to search for
  13. var SRpc=0;//picked clean
  14. var SRvlr=1;//very limited resources
  15. var SRlr=2;//limited resources
  16. var SRmr=3;//moderate resources
  17. var SRar=4;//abundant resources
  18.  
  19.  
  20. //variables
  21. var resourcelvl=-1;//current pages resource lvl (assuming we have search results)
  22. var searchresult;//currentpages search result (assuming we have search results)
  23. var arewesearching;//boolean are we?
  24.  
  25.  
  26. //functions
  27. function outputsearchresults(texttoadd){
  28. //texttoadd='ddd';
  29. var gib=document.getElementsByClassName("gamebox infobox")[0];
  30. var searchtextelement=gib.getElementsByTagName('b')[0];
  31. var divwewant=gib.firstElementChild;
  32. var rawtext=GM_getValue('GMallsearchresults','');
  33. GM_setValue('GMallsearchresults','');
  34. //var rawtext='text\nline1\nline2\nline3\nline4';
  35. var arraytext=rawtext.split('\n');
  36. arraytext[0]=texttoadd;//this can be over written as its not a search result
  37. var textline= document.createElement("p")//where we will place the results
  38. for (i=0;i<arraytext.length;i++){//populating results
  39. textline.innerHTML=textline.innerHTML+arraytext[i]+'<br>';
  40. }
  41. divwewant.insertBefore(textline,searchtextelement.nextSibling );
  42. }
  43.  
  44. function getsearchrate(){
  45. //return SRar
  46. //first if the tweaks script has run before this script its easier.
  47. if(document.getElementById('searchrate')){//we have an id!
  48. return parseInt(document.getElementById('searchrate').getAttribute('value'))//return as int the search rate
  49. }
  50. else{//we have to actually search the text for the search rate
  51. var gib=document.getElementsByClassName("gamebox infobox")[0];
  52. var gibhtml=gib.innerHTML;
  53. if(gibhtml.match(/This area appears to have been picked clean./)){return SRpc;}
  54. else if(gibhtml.match(/very limited resources/)){return SRvlr;}
  55. else if(gibhtml.match(/limited resources/)){return SRlr;}
  56. else if(gibhtml.match(/moderate resources/)){return SRmr;}
  57. else if(gibhtml.match(/abundant resources/)){return SRar;}
  58. else if(gibhtml.match(/There appears to be nothing to find here./)){return SRpc;}
  59. else return SRar;//we don't know so assume lots
  60. }
  61. }
  62.  
  63. function cleanupforexit(){
  64. GM_setValue('GMarewesearching',false);//not searching
  65. GM_setValue('GMallsearchresults','');//clear old results
  66. GM_setValue('GMnumofsearchesleft',0);
  67. return 1
  68. }
  69.  
  70. //SCRIPT START
  71.  
  72. //setupthebutton
  73. //grab ap as we make the button invisible if not enough ap to do whole search
  74. var gsb=document.getElementsByClassName("gamebox statsbox")[0];
  75. var searchtextelement=gsb.getElementsByTagName('b');
  76. var a=searchtextelement[1];//second bold thing is ap.
  77. var test=a.innerHTML.match(/(\d+)[^0-9]*(\d+)/) // extract the first 2 numbers
  78. var availableap=test[1];
  79. //make new autosearch button by cloning search button
  80. var searchbutton=document.evaluate( "//input[@value='Search']", document, null, XPathResult.ORDERED_NODE_SNAPSHOT_TYPE, null).snapshotItem(0);
  81. search10buttonform=searchbutton.parentNode.cloneNode(true);//make a new copy of the search button
  82. searchbutton.parentNode.parentNode.insertBefore(search10buttonform,searchbutton.parentNode.nextSibling);
  83. search10button=search10buttonform.firstElementChild;
  84. search10button.setAttribute("value", "Search "+numberofsearches+ "x");
  85. if (numberofsearches>availableap){//we don't have enough ap so hide button
  86. search10button.style.display='none';
  87. }
  88. search10buttonform.id="AutoSearchForm";//id so we can easy find it later
  89. search10button.addEventListener("click", function(event) {GM_setValue('GMarewesearching',true);
  90. GM_setValue('GMnumofsearchesleft',numberofsearches);},true);
  91.  
  92.  
  93.  
  94. //cleanupforexit();
  95. //first we must check if this is the first time through
  96. //alert(GM_getValue('GMarewesearching',false));
  97. var arewesearching=GM_getValue('GMarewesearching',false);
  98. if(arewesearching){//we are searching
  99. //alert("we be searching");
  100. //well then there aught to be results to gather.
  101. var gib=document.getElementsByClassName("gamebox infobox")[0];
  102. var searchtextelement=gib.getElementsByTagName('b');
  103. if (searchtextelement){//foundsomething
  104. var searchtext=searchtextelement[0].innerHTML
  105. //var foundtext=searchtext.match(/([^.]+.)(.*)/);
  106. var foundtext=searchtext.match(/([^.]+.)/);//finds the first sentence
  107. //alert(foundtext[1]+" and "+foundtext[2]);
  108. if (GM_getValue("GMallsearchresults")) {
  109. GM_setValue("GMallsearchresults", GM_getValue("GMallsearchresults") + "\n" + foundtext[1]);
  110. }
  111. else{
  112. GM_setValue("GMallsearchresults", "\n" + foundtext[1]);
  113. }
  114. var rawtext=GM_getValue('GMallsearchresults','');
  115. /*if (foundtext[2].match(/picked clean/)||foundtext[2].match(/There appears to be nothing to find here/)){
  116. //this area has been picked clean and we know it so stop ffs
  117. //we are done so set search to off and output results and then quit saying nothing more to be found.
  118. outputsearchresults("You stopped searching as you realise there's nothing more to be found here");
  119. return cleanupforexit();//quit script
  120. }*/
  121. //alert(GM_getValue('GMallsearchresults',"fail"));
  122. //var text=GM_getValue('GMallsearchresults',"fail");
  123. //alert(text.replace(/\n/),':');
  124. var searchrate=getsearchrate();//so we only get it once
  125. var numofsearchesleft=Number(GM_getValue('GMnumofsearchesleft',0));//so we only get it once
  126. if (numofsearchesleft==numberofsearches){//first search result. Store the search rate.
  127. GM_setValue('GMsearchrate',getsearchrate());
  128. }
  129. //alert('searchrate='+searchrate+" numofsearchesleft="+numofsearchesleft+"oldserachrate="+GM_getValue('GMsearchrate',-1));
  130. var lastsearchrate=GM_getValue('GMsearchrate',-1);//get the last search rate(also is curent if first search)
  131. //alert(searchrate<=0||numofsearchesleft==0||searchrate<lastsearchrate);
  132. /*if (searchrate<=0||numofsearchesleft==1||searchrate<lastsearchrate){//stop searching
  133. outputsearchresults("You searched " + (numberofsearches-numofsearchesleft-1) + "times");
  134. return cleanupforexit();//quit script
  135. }*/
  136. if (searchrate<=0){//stop searching as nothing left to find
  137. //alert("1");
  138. var searched=(numberofsearches-numofsearchesleft+1);
  139. var outstring="You searched " + (searched==1?'once':searched+' times') + " before stopping as there is nothing to be found here.";
  140. //outstring=outstring.replace(/1 times/,'once');
  141. outputsearchresults(outstring);
  142. return cleanupforexit();//quit script
  143. }
  144. else if(numofsearchesleft==1){//time to stop on 1 because search then reduce counter
  145. //alert("2");
  146. outputsearchresults("You searched " + (numberofsearches-numofsearchesleft+1) + " times.");
  147. return cleanupforexit();//quit script
  148. }
  149. else if(searchrate<lastsearchrate){
  150. //alert("3");
  151. var searched=(numberofsearches-numofsearchesleft+1);
  152. outputsearchresults("You searched " + (searched==1?'once':searched+' times') + " before stopping as the odds of finding something have worsened.");
  153. return cleanupforexit();//quit script
  154. }
  155. else{//we should search again!
  156. GM_setValue('GMsearchrate',searchrate);//remember the search rate
  157. GM_setValue('GMnumofsearchesleft',numofsearchesleft-1)//decrement searchcounter
  158. document.getElementById('AutoSearchForm').submit();//search
  159. }
  160. }
  161. }
  162. //else{}
  163.  
  164.  
  165.  
  166.  
  167.  
  168.  
  169.  
  170.  
  171.  
  172.  
  173.  
  174.  
  175. //EOF
  176. })();