Shintolin AutoSearch

Adds Search 10x button

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