WF Explorer Next Planet Button

Adds a next planet button for probes which launches fleet to next planet. For www.war-facts.com .

当前为 2015-05-08 提交的版本,查看 最新版本

  1. // ==UserScript==
  2. // @name WF Explorer Next Planet Button
  3. // @namespace https://greasyfork.org/en/users/10321-nikitas
  4. // @description Adds a next planet button for probes which launches fleet to next planet. For www.war-facts.com .
  5. // @include http://*.war-facts.com/fleet.php*
  6. // @version 4
  7. // @grant none
  8. // ==/UserScript==
  9.  
  10. //Configuration Options:
  11.  
  12. //Change this if you do not want Auto Launch when Next Button is Pressed, or if you don't want to Auto Select Next Explorer
  13. unsafeWindow.AutoLaunch = true;
  14. unsafeWindow.AutoSelectNextExplorer = true;
  15.  
  16.  
  17. // Change this if you wish to exclude Fleets whose names contain the excludeString from being auto Selected as next explorer.
  18. unsafeWindow.useExcludeString = false;
  19. // Change this to what you would like to put into a fleet's name in order to exclude it from being auto selected.
  20. unsafeWindow.excludeString = "#NotAuto#";
  21.  
  22.  
  23. // Change this if you wish to auto Select Fleets as next explorer ONLY if their names contain the includeString.
  24. unsafeWindow.useIncludeString = false;
  25. // Change this to what you would like to put into a fleet's name in order to include it into being auto selected.
  26. unsafeWindow.includeString = "#Auto#";
  27.  
  28.  
  29.  
  30.  
  31. //Script
  32.  
  33. $(window).load(function(){
  34.  
  35. var classificationNode = document.getElementById('fleetClass');
  36. var isExplorer = document.evaluate("//text()[contains(.,'Explorer') or contains(.,'Sentry') or contains(.,'Probe Rush')]", classificationNode, null, XPathResult.BOOLEAN_TYPE, null).booleanValue;
  37.  
  38.  
  39. if (isExplorer){
  40.  
  41. var info = document.getElementById('navData').getElementsByTagName('div')[4];
  42. var infoSpan = info.getElementsByTagName('span')[0];
  43.  
  44. // not containing world in the span.
  45. var isAtSystemEntrance = ! (document.evaluate("//text()[contains(.,'World:')]", infoSpan, null, XPathResult.BOOLEAN_TYPE, null).booleanValue);
  46.  
  47.  
  48.  
  49. //alert("Is explorer = " + isExplorer);
  50. //alert("Is at system Entrance = " + isAtSystemEntrance);
  51. var currentPlanet = info.getElementsByTagName('a')[0].innerHTML;
  52.  
  53.  
  54. //alert(currentPlanet);
  55.  
  56. var optionGroup = document.getElementById('target1').getElementsByTagName('optgroup')[0].getElementsByTagName('option');
  57.  
  58. var i = 0 , found = false, optionsLength = optionGroup.length;
  59. var nextPlanetOption, finishedSystem = false;
  60.  
  61.  
  62.  
  63. //if I am at system entrance
  64. if (isAtSystemEntrance){
  65. found = true;
  66. nextPlanetOption = optionGroup[0].value;
  67. }
  68.  
  69.  
  70. //if PlanetLess system
  71. if (optionsLength == 0 ) {
  72. found = true;
  73. finishedSystem = true;
  74. }
  75.  
  76.  
  77. // If I am at a planet, Find next planet through the local target option list
  78.  
  79. while ( (i < optionsLength) && (found == false) ) {
  80.  
  81. if (optionGroup[i].innerHTML == currentPlanet ){
  82. found = true;
  83.  
  84. if ( i == optionsLength -1 ){
  85. finishedSystem = true;
  86. } else {
  87. nextPlanetOption = optionGroup[i+1].value;
  88. }
  89. }
  90.  
  91. i++;
  92.  
  93. }
  94.  
  95.  
  96. // alert("finished system = " + finishedSystem);
  97. // alert("next planet = " + nextPlanetOption);
  98.  
  99.  
  100. // alert("Reached finished System");
  101.  
  102. if (finishedSystem) {
  103. // alert("Inside finished System");
  104. document.getElementById('missionData').innerHTML += '<input class = "greenbutton darkbutton" type="button" id="nextPlanetButton" value = "Done" onclick="openStarMap()" />';
  105. document.getElementById('nextPlanetButton').addEventListener('click', openStarMap, false);
  106. } else {
  107. // alert("Inside NOT finished System");
  108. document.getElementById('missionData').innerHTML += '<input class = "greenbutton darkbutton" type="button" id="nextPlanetButton" value = "Next Planet" onclick="selectNextPlanet()" />';
  109. document.getElementById('nextPlanetButton').addEventListener('click', selectNextPlanet, false);
  110.  
  111. }
  112.  
  113. }
  114.  
  115.  
  116.  
  117. function selectNextPlanet(){
  118. jQuery('#target1').val(nextPlanetOption).trigger ('change');
  119.  
  120. if (unsafeWindow.AutoLaunch){
  121. unsafeWindow.hasLaunched = false;
  122. window.setTimeout(launchFleet,100);
  123. }
  124.  
  125. }
  126.  
  127. function openStarMap(){
  128. var starMapLink;
  129. if (isAtSystemEntrance){
  130. starMapLink = document.getElementById('navData').getElementsByTagName('div')[10].getElementsByTagName('a')[0].href;
  131. } else {
  132. starMapLink = document.getElementById('navData').getElementsByTagName('div')[11].getElementsByTagName('a')[0].href;
  133. }
  134.  
  135. starMapLink = starMapLink.substring(19, starMapLink.length - 3 ); //Keep only the link, throw away the functions
  136.  
  137. // mapWin is war-facts.com function to open javascript map
  138. mapWin(starMapLink);
  139. }
  140.  
  141.  
  142.  
  143. function launchFleet(){
  144. var launchButton = document.getElementById('mButton').getElementsByTagName('input')[0];
  145.  
  146. if (launchButton) { //So Button has been created
  147. getMission('launch'); //Launch Fleet
  148. unsafeWindow.hasLaunched = true;
  149.  
  150. if (unsafeWindow.AutoSelectNextExplorer){
  151. var explorerList = document.getElementById('fc_Explorer').children;
  152. var index = 0;
  153. var explorerListLength = explorerList.length;
  154.  
  155.  
  156. while(index < explorerListLength){
  157.  
  158.  
  159.  
  160. if (explorerList[index].children[0].style.color == "rgb(242, 242, 242)"){
  161. var link = explorerList[index].children[0].href;
  162. var name = explorerList[index].children[0].innerHTML;
  163. if ( ( ( ! unsafeWindow.useIncludeString) || ( name.indexOf( unsafeWindow.includeString ) > -1 ) ) //If not using include string or String is in name
  164. && ( ( ! unsafeWindow.useExcludeString) || ( name.indexOf( unsafeWindow.excludeString) == -1 ) ) //If not using exclude string or String is NOT in name
  165. )
  166. {
  167. if (link != window.location.href){ //Make sure we are not chosing ourselve as this fleet is still "white"
  168. index = explorerListLength; //To make sure if load doesn't happen immediately it stops running through fleet list
  169. window.open(link, "_self");
  170.  
  171. }
  172. }
  173. }
  174.  
  175. index++ ;
  176. }
  177. }
  178.  
  179. } else {
  180. window.setTimeout(launchFleet,100);
  181. }
  182.  
  183.  
  184.  
  185.  
  186. }
  187.  
  188.  
  189. });
  190.  
  191.  
  192.  
  193.  
  194.  
  195.  
  196.  
  197.  
  198. /*---
  199. From https://gist.github.com/BrockA/2625891
  200. waitForKeyElements(): A utility function, for Greasemonkey scripts,
  201. that detects and handles AJAXed content.
  202.  
  203. Usage example:
  204.  
  205. waitForKeyElements (
  206. "div.comments"
  207. , commentCallbackFunction
  208. );
  209.  
  210. //--- Page-specific function to do what we want when the node is found.
  211. function commentCallbackFunction (jNode) {
  212. jNode.text ("This comment changed by waitForKeyElements().");
  213. }
  214.  
  215. IMPORTANT: This function requires your script to have loaded jQuery.
  216. */
  217. function waitForKeyElements (
  218. selectorTxt, /* Required: The jQuery selector string that
  219. specifies the desired element(s).
  220. */
  221. actionFunction, /* Required: The code to run when elements are
  222. found. It is passed a jNode to the matched
  223. element.
  224. */
  225. bWaitOnce, /* Optional: If false, will continue to scan for
  226. new elements even after the first match is
  227. found.
  228. */
  229. iframeSelector /* Optional: If set, identifies the iframe to
  230. search.
  231. */
  232. ) {
  233. var targetNodes, btargetsFound;
  234.  
  235. if (typeof iframeSelector == "undefined")
  236. targetNodes = $(selectorTxt);
  237. else
  238. targetNodes = $(iframeSelector).contents ()
  239. .find (selectorTxt);
  240.  
  241. if (targetNodes && targetNodes.length > 0) {
  242. btargetsFound = true;
  243. /*--- Found target node(s). Go through each and act if they
  244. are new.
  245. */
  246. targetNodes.each ( function () {
  247. var jThis = $(this);
  248. var alreadyFound = jThis.data ('alreadyFound') || false;
  249.  
  250. if (!alreadyFound) {
  251. //--- Call the payload function.
  252. var cancelFound = actionFunction (jThis);
  253. if (cancelFound)
  254. btargetsFound = false;
  255. else
  256. jThis.data ('alreadyFound', true);
  257. }
  258. } );
  259. }
  260. else {
  261. btargetsFound = false;
  262. }
  263.  
  264. //--- Get the timer-control variable for this selector.
  265. var controlObj = waitForKeyElements.controlObj || {};
  266. var controlKey = selectorTxt.replace (/[^\w]/g, "_");
  267. var timeControl = controlObj [controlKey];
  268.  
  269. //--- Now set or clear the timer as appropriate.
  270. if (btargetsFound && bWaitOnce && timeControl) {
  271. //--- The only condition where we need to clear the timer.
  272. clearInterval (timeControl);
  273. delete controlObj [controlKey]
  274. }
  275. else {
  276. //--- Set a timer, if needed.
  277. if ( ! timeControl) {
  278. timeControl = setInterval ( function () {
  279. waitForKeyElements ( selectorTxt,
  280. actionFunction,
  281. bWaitOnce,
  282. iframeSelector
  283. );
  284. },
  285. 300
  286. );
  287. controlObj [controlKey] = timeControl;
  288. }
  289. }
  290. waitForKeyElements.controlObj = controlObj;
  291. }
  292.  
  293.  
  294.  
  295.